HDUOJ---2642Stars(二维树状数组)

Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 975    Accepted Submission(s): 420


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
 

 

Author
teddy
 

 

Source
 
树状数组,用到二维,当时总的的来说,还算简单。。。
题目要求求矩形里星星中的个数//
给定某两个坐标对角坐标,球该矩形的星星个数..
对于x1,x2,y1,y2.。我们不知道其大小,所以需要进行比较
得到大小之后,我们就可以求该巨型的了,像下面的图一样..
 
HDUOJ---2642Stars(二维树状数组)_第1张图片
由此贴出代码吧:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define maxn  1005
 5 #define lowbit(x) ((x)&(-x))
 6 int aa[maxn][maxn];
 7 bool bb[maxn][maxn];
 8 
 9 void ope(int x,int y,int val)
10 {
11     int j;
12     if(val==1)
13     {
14         if(bb[x][y])    return ;
15         bb[x][y]=true;
16     }
17     else
18     {
19       if(bb[x][y]==false)
20            return ;
21       bb[x][y]=false;
22     }
23     while(x<maxn){
24        j=y;
25      while(j<maxn){
26        aa[x][j]+=val;
27        j+=lowbit(j);
28      }
29       x+=lowbit(x);
30     }
31 }
32 int sum(int x,int y)
33 {
34   int ans=0 ,j;
35   while(x>0){
36      j=y;
37     while(j>0){
38      ans+=aa[x][j];
39      j-=lowbit(j);
40     }
41      x-=lowbit(x);
42   }
43    return ans;
44 }
45 struct node
46 {
47     int x;
48     int y;
49 };
50 int main()
51 {
52     int test,res;
53     char str[2];
54     node a,b;
55     memset(aa,0,sizeof(aa));
56     memset(bb,0,sizeof(bb));
57     scanf("%d",&test);
58     while(test--)
59     {
60         scanf("%s",str);
61         if(str[0]=='Q')
62         {
63             scanf("%d%d%d%d",&a.x,&b.x,&a.y,&b.y);
64             if(a.x>b.x){
65                 a.x^=b.x;
66                 b.x^=a.x;
67                 a.x^=b.x;
68             }
69             if(a.y>b.y){
70                 a.y^=b.y;
71                 b.y^=a.y;
72                 a.y^=b.y;
73             }
74             b.x++;
75             b.y++;
76             res=sum(b.x,b.y)-sum(a.x,b.y)+sum(a.x,a.y)-sum(b.x,a.y);
77             printf("%d\n",res);
78         }
79         else
80         {
81           scanf("%d%d",&a.x,&a.y);
82           a.x++;  //ÓÒÒÆһλ
83           a.y++;
84           if(str[0]=='B')
85              ope(a.x,a.y,1);
86           else
87              ope(a.x,a.y,-1);
88         }
89     }
90     return 0;
91 }
View Code

 

 

你可能感兴趣的:(树状数组)