HDU 2642(树状数组模板二维)

Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)

Total Submission(s): 1350    Accepted Submission(s): 554

Problem Description

Yifenfei is a romantic guy and he likes to count the stars in the sky.

To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will be given as "B x y" where ‘B‘ represent bright
and x represent the X coordinate and y represent the Y coordinate means the star at (x,y) is bright,And the ‘D‘ in "D x y" mean the star at(x,y) is dim.When get a query as "Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the region
correspond X1,X2,Y1,Y2.

There is only one case.

Input

The first line contain a M(M <= 100000), then M line followed.

each line start with a operational character.

if the character is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.

if the character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000) followed.

Output

For each query,output the number of bright stars in one line.

Sample Input

5

B 581 145

B 581 145

Q 0 600 0 200

D 581 145

Q 0 600 0 200

Sample Output

1

0

//标准的模板题二维

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

const int N=1010;

int c[N][N];
bool vis[N][N];

int lowbit(int x)
{
    return x&(-x);
}

void add(int x,int y,int val)
{
    int i=y;
    while(x<=N)
    {
        y=i;
        while(y<=N)
        {
            c[x][y]+=val;
            y+=lowbit(y);
        }
        x+=lowbit(x);
    }
}

int sum(int x,int y)
{
    int i,sum=0;
    i=y;
    while(x)
    {
        y=i;
        while(y)
        {
            sum+=c[x][y];
            y-=lowbit(y);
        }
        x-=lowbit(x);
    }
    return sum;
}

int main()
{
    int n,x1,x2,y1,y2;
    scanf("%d",&n);
    memset(c,0,sizeof(c));
    memset(vis,0,sizeof(vis));
    while(n--)
    {
        getchar();
        char c;
        scanf("%c ",&c);
        if(c=='Q')
        {
            scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
            x1++,x2++,y1++,y2++;
            if(x1>x2)swap(x1,x2);
            if(y1>y2)swap(y1,y2);
            printf("%d\n",(sum(x2,y2)+sum(x1-1,y1-1)-sum(x1-1,y2)-sum(x2,y1-1)));
        }
        else if(c=='B')
        {
            scanf("%d%d",&x1,&y1);
            if(vis[x1+1][y1+1]==1)
                continue;
            else
                add(x1+1,y1+1,1);
            vis[x1+1][y1+1]=1;
        }
        else if(c=='D')
        {
            scanf("%d%d",&x1,&y1);
            if(vis[x1+1][y1+1]==0)
                continue;
            else
                add(x1+1,y1+1,-1);
            vis[x1+1][y1+1]=0;
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 04:11:01

HDU 2642(树状数组模板二维)的相关文章

poj 1195 二维树状数组 及二维树状数组模板

http://poj.org/problem?id=1195 求矩阵和的时候,下标弄错WA了一次... 求矩形(x1,y1) (x2,y2)的sum |sum=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1) 二维树状数组讲解:http://blog.csdn.net/u011026968/article/details/38532117 二维树状数组模板: /*========================================

[BZOJ4785][ZJOI2017]树状数组(概率+二维线段树)

4785: [Zjoi2017]树状数组 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 297  Solved: 195[Submit][Status][Discuss] Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道 基础的树状数组题.给出一个长度为 n 的数组 A,初始值都为 0,接下来进行 m 次操作,操作有两种: 1 x,表示将 Ax 变成 (Ax + 1)

HDU 2642 树状数组

点击打开链接 题意:给个二维矩阵,矩阵有0或者1两个值,然后有三个操作,Q问区间和,剩下两个是更新点的值 思路:更新点的值直接更新就行了,然后询问区间和的时候就处理一下,每次问的是X1,Y1到X2,Y2的区间和,而树状数组的和是从1,1开始的,所以总的减去多于的在加上多减去的就OK了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include

【树状数组】树状数组一维二维模板

以下模板都是点更新,区间查询,如果是区间更新点查询,只需将利用lowbit的循环方向倒过来 一维: inline int lowbit(int x) { return x & -x; } void add(int x, int val) { for(int i = x; i <= n; i += lowbit(i)) C[i] += val; } int sum(int x) { int ret = 0; for(int i = x; i > 0; i -= lowbit(i)) re

poj 2155- Matrix (树状数组,二维,更新区间,查询单点)

Matrix Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j]

树状数组_二维

class TreeArraryTwo{ public : const static int cmaxn = 1e3+200; int date[cmaxn][cmaxn]; int xsz, ysz; void init(int size_x, int size_y) { xsz = size_x; ysz = size_y; memset(date, 0, sizeof(date)); } inline int lowbit(int idx) { return idx & -idx; } v

poj2481树状数组解二维偏序

按区间r降序排列,r相同按照l升序排列,两个区间相同时特判一下即可 /* 给定n个闭区间[l,r],如果对区间[li,ri],[lj,rj]来说, 有区间j严格包含(两个边界不能同时重合)在区间i内,那么区间i大于区间j 问区间有多少个区间大于区间i 读入所有的区间,按r降序,l升序 然后按顺序访问每个区间, */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>

免费的馅饼 HYSBZ - 2131 (树状数组维护二维偏序)

题目链接:https://cn.vjudge.net/problem/HYSBZ-2131 题目大意:中文题目 具体思路:对于任意的两个位置,posA和posB,我们可以如下推导. |posA-posB|<=2*(tA-tB) 2*tB-2*tA<=posA-posB<=2*tB-2*tA. 2*tA+posA>=2*tB+posB  || 2*tA -pos A > = 2*tB-posB.(注意这个地方,只要有一个满足就可以了) 然后我们对2*tA+posA进行排序,每一

二维树状数组模板(区间修改+区间查询)

二维树状数组模板(区间修改+区间查询) 例题:JOIOI上帝造题的七分钟 一共两种操作: \(L\ x_1\ y_1\ x_2\ y_2\ d\):把\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素加\(d\). \(k\ x_1\ y_1\ x_2\ y_2\):查询\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素的和. 代码如下: #include<bits/stdc++.h> #define RG register #define IL i