首先,这个时二维数组数组的用法。。。
直接套模版的说。。。
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.
感觉题目没有讲清楚啊。。。每个格子上面初始是有一本书的
刚刚读题的我还以为是二维的线段树。。。。然而二维的线段数仿佛并不是用来求这玩意儿的。。。。
代码:
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1010;
int save[maxn+10][maxn+10];
int lowbit(int x) {
return x & (-x);
}
void update(int x,int y, int val) {
for(int i = x; i <= maxn; i += lowbit(i)) {
for(int j = y; j <= maxn; j += lowbit(j)) {
save[i][j] += val;
}
}
}
int query(int x, int y) {
int sum = 0;
for(int i = x; i > 0; i -= lowbit(i)) {
for(int j = y; j > 0; j -= lowbit(j)) {
sum += save[i][j];
}
}
return sum;
}
int getSingle(int x, int y) {
return query(x, y) + query(x-1, y-1) - query(x-1, y) - query(x, y-1);
}
int main(){
int t,q,i,j;
scanf("%d",&t);
int rnd=1;
while(t--){
printf("Case %d:\n",rnd);
rnd++;
memset(save, 0, sizeof(save));
for(i = 1; i <= maxn; i++) {
for(j = 1; j <= maxn; j++) {
update(i, j, 1);
}
}
scanf("%d",&q);
char in;
int x1,x2,y1,y2,n;
for(i=1;i<=q;i++){
cin>>in;
if(in=='S'){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2){
swap(x1,x2);
}
if(y1>y2){
swap(y1, y2);
}
printf("%d\n",query(x2+1, y2+1) - query(x1, y2+1) - query(x2+1, y1) + query(x1, y1));
}else if(in == 'A'){
scanf("%d%d%d",&x1,&y1,&n);
update(x1+1, y1+1, n);
}else if(in == 'D'){
scanf("%d%d%d", &x1, &y1, &n);
int now = getSingle(x1+1, y1+1);
n = min(n, now);
update(x1+1, y1+1, -n);
}else{
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n);
int n1 = getSingle(x1+1, y1+1);
n = min(n, n1);
update(x1+1, y1+1, -n);
update(x2+1, y2+1, n);
}
}
}
return 0;
}