HDU 2642 Stars

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.

思路:二维树状数组

#include <bits/stdc++.h>
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lbt(x) (x&(-x))
#define IOS ios::sync_with_stdio(false)
#define DEBUG cout<<endl<<"DEBUG"<<endl;
using namespace std;
int w[1010][1010],n,a,b,c,d;char x;bool v[1010][1010];
void add(int x,int y,int p){
    for(int i=x;i<1010;i+=lbt(i))
        for(int j=y;j<1010;j+=lbt(j))
            w[i][j]+=p;
}
int sum(int x,int y){
    int i,sum=0;
    i=y;
    while(x){
        y=i;
        while(y){
            sum+=w[x][y];
            y-=lbt(y);
        }
        x-=lbt(x);
    }
    return sum;
}
int main(){IOS;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x;
        if(x=='B'){
            cin>>a>>b;
            a++,b++;
            if(!v[a][b])v[a][b]=1,add(a,b,1);
            else continue;
        }
        else if(x=='D'){
            cin>>a>>b;
            a++,b++;
            if(v[a][b])v[a][b]=0,add(a,b,-1);
            else continue;
        }
        else{
            cin>>a>>b>>c>>d;
            if(a>b)
                swap(a,b);
            if(c>d)
                swap(c,d);
            a++,b++,c++,d++;
            cout<<(sum(b,d)+sum(a-1,c-1)-sum(a-1,d)-sum(b,c-1))<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/85267575