HDU1892 See you~
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4660 Accepted Submission(s): 1479
Problem Description
Now
I am leaving hust acm. In the past two and half years, I learned so
many knowledge about Algorithm and Programming, and I met so many good
friends. I want to say sorry to Mr, Yin, I must leave now ~~>.<~~.
I am very sorry, we could not advanced to the World Finals last year.
When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.
When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.
Input
In
the first line of the input file there is an Integer T(1<=T<=10),
which means the number of test cases in the input file. Then N test
cases are followed.
For each test case, in the first line there is an Integer Q(1<Q<=100,000), means the queries of the case. Then followed by Q queries.
There are 4 kind of queries, sum, add, delete and move.
For example:
S x1 y1 x2 y2 means you should tell me the total books of the rectangle used (x1,y1)-(x2,y2) as the diagonal, including the two points.
A x1 y1 n1 means I put n1 books on the position (x1,y1)
D x1 y1 n1 means I move away n1 books on the position (x1,y1), if less than n1 books at that position, move away all of them.
M x1 y1 x2 y2 n1 means you move n1 books from (x1,y1) to (x2,y2), if less than n1 books at that position, move away all of them.
Make sure that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
For each test case, in the first line there is an Integer Q(1<Q<=100,000), means the queries of the case. Then followed by Q queries.
There are 4 kind of queries, sum, add, delete and move.
For example:
S x1 y1 x2 y2 means you should tell me the total books of the rectangle used (x1,y1)-(x2,y2) as the diagonal, including the two points.
A x1 y1 n1 means I put n1 books on the position (x1,y1)
D x1 y1 n1 means I move away n1 books on the position (x1,y1), if less than n1 books at that position, move away all of them.
M x1 y1 x2 y2 n1 means you move n1 books from (x1,y1) to (x2,y2), if less than n1 books at that position, move away all of them.
Make sure that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
Output
At the beginning of each case, output "Case X:" where X is the index of the test case, then followed by the "S" queries.
For each "S" query, just print out the total number of books in that area.
For each "S" query, just print out the total number of books in that area.
Sample Input
2
3
S 1 1 1 1
A 1 1 2
S 1 1 1 1
3
S 1 1 1 1
A 1 1 2
S 1 1 1 2
Sample Output
Case 1:
1
3
Case 2:
1
4
Author
Sempr|CrazyBird|hust07p43
Source
Recommend
lcy
用二位树状数组模拟单点修改和区间查询。
1 /**/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 const int mxn=1200; 9 int n; 10 int t[mxn][mxn]; 11 inline int lowbit(int x){return x&-x;} 12 void add(int x,int y,int w){ 13 while(x<mxn){ 14 int tmp=y; 15 while(tmp<mxn){ 16 t[x][tmp]+=w; 17 tmp+=lowbit(tmp); 18 } 19 x+=lowbit(x); 20 } 21 } 22 int query(int x,int y){ 23 int res=0; 24 while(x){ 25 int tmp=y; 26 while(tmp){ 27 res+=t[x][tmp]; 28 tmp-=lowbit(tmp); 29 } 30 x-=lowbit(x); 31 } 32 return res; 33 } 34 int ask(int x,int y){ 35 return query(x,y)-query(x-1,y)-query(x,y-1)+query(x-1,y-1); 36 } 37 int main(){ 38 int T; 39 scanf("%d",&T); 40 int i,j; 41 for(int ro=1;ro<=T;ro++){ 42 //init 43 memset(t,0,sizeof t); 44 for(i=1;i<mxn;i++) 45 for(j=1;j<mxn;j++) 46 add(i,j,1); 47 //finish 48 printf("Case %d:\n",ro); 49 scanf("%d",&n); 50 char ch[3]; 51 int x1,y1,x2,y2; 52 int val; 53 while(n--){ 54 scanf("%s",ch); 55 if(ch[0]=='A'||ch[0]=='D'){ 56 scanf("%d%d%d",&x1,&y1,&val); 57 if(ch[0]=='D')val=-(min(val,ask(x1+1,y1+1))); 58 add(x1+1,y1+1,val); 59 continue; 60 } 61 if(ch[0]=='M'){ 62 scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&val); 63 val=min(val,ask(x1+1,y1+1)); 64 add(x1+1,y1+1,-val); 65 add(x2+1,y2+1,val); 66 continue; 67 } 68 else{ 69 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 70 if(x1<x2)swap(x1,x2); 71 if(y1<y2)swap(y1,y2); 72 int ans=query(x1+1,y1+1)-query(x2,y1+1)-query(x1+1,y2)+query(x2,y2); 73 printf("%d\n",ans); 74 } 75 } 76 } 77 return 0; 78 }
本文为博主原创文章,转载请注明出处。