Hdu 2642 two-dimensional tree array single-point Update Interval query Template

Source: Internet
Author: User

Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission (s): 785 Accepted Submission (s): 335

 

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 shoshould 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
Q0 600 0 200
D 581 145
Q0 600 0 200


Sample Output
1
0


Author
Teddy
 


Source
Millions of Qin Guan eventually belong to Chu
 


Recommend
Teddy
 
 
 
 
Question: there is a map. Each vertex is a lamp. After B is input, the input coordinate indicates that the light of the corresponding vertex turns on. Input D. The input coordinate indicates that the light is dimmed. Input Q. Enter the lower left corner of a rectangle and the upper right corner. number of lights
 
Ideas:
Obviously, the two-dimensional tree array cannot be switched on after the light is turned on, and the light cannot be switched off after the light is turned off. Therefore, you can use a flag array to mark whether the light can be switched off.
 
Note that the subscript starts from 1 and starts from 0. Therefore, add 1.
 

#include<stdio.h>#include<string.h>const int N=1003;int c[N+5][N+5],flag[N+5][N+5],n,m;int mmax(int a,int b){    return  a>b?a:b;}int lowbit(int x){return x&(-x);}void update(int x,int y,int delta ){int i, j;for(i=x;i<=N;i+=lowbit(i)){for(j=y; j<=N; j+=lowbit(j)){c[i][j] += delta;}}}int sum( int x, int y ){    int res=0,i,j;    for(i=x;i>0;i-=lowbit(i)){for(j=y; j>0; j-=lowbit(j)){res += c[i][j];}}return res;}int main(){int x1,x2,y1,y2;while(scanf("%d",&m)!=EOF){    memset(c,0,sizeof(c));    memset(flag,0,sizeof(flag));    while(m--)        {            char s[2];            scanf("%s",s);            if(s[0]=='Q')            {                scanf("%d %d %d %d",&x1,&x2,&y1,&y2);                x1++;y1++;x2++;y2++;                if(x1>x2) {int temp=x1;x1=x2;x2=temp;}                if(y1>y2) {int temp=y1;y1=y2;y2=temp;}                int ans=sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1);                printf("%d\n",ans);            }            else  if(s[0]=='B')            {                scanf("%d %d",&x1,&y1);                x1++;y1++;                if(flag[x1][y1]==1) continue;                flag[x1][y1]=1;                update(x1,y1,1);            }            else                {                   scanf("%d %d",&x1,&y1);                   x1++;y1++;                   if(flag[x1][y1]==0) continue;                   flag[x1][y1]=0;                   update(x1,y1,-1);                }        }}return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.