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.

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.

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.

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

HDU 2008-4 Programming Contest

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 }
时间: 2024-10-27 12:58:04

HDU1892 See you~的相关文章

See you~(hdu1892)

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4768    Accepted Submission(s): 1521 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno

hdu-1892 See you~---二维树状数组运用

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 题目大意: 题目大意:有很多方格,每个方格对应的坐标为(I,J),刚开始时每个格子里有1本书,然后让你统计一片区域有多少本书,还可以增加书和减少,移动书. 解题思路: 直接二维数组数组模拟 注意: 每个下标+1,从(1, 1)开始 求区域和的时候给出的x1 y1 和x2 y2不是标准的正对角线,需要转化 1 #include<cstdio> 2 #include<cstring>

See you~ HDU1892

一开始还离散化弄了好久  离散化细节弄得好差 这题用二维树状数组做很快  因为树状数组下标不为0  所以所有下标要加一处理 还有就是算矩阵的时候要处理两个坐标的大小关系 个人感觉树状数组用for语句写更加简洁 #include<bits/stdc++.h> using namespace std; int c[1010][1010]; int n; int lowbit(int i) { return i&-i; } void update(int x,int y,int v) { f