二维树状数组(4种操作)
来源:互联网 发布:window32 api编程 编辑:程序博客网 时间:2024/06/05 19:25
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.
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
23S 1 1 1 1A 1 1 2S 1 1 1 13S 1 1 1 1A 1 1 2S 1 1 1 2
Sample Output
Case 1:13Case 2:14
题意:
在这里我们有4种操作,add, sum,delete,move, 在这里我们有一个注意的问题。就是我们要把整体点的坐标向右上位置移动一个单位。
#if 0#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int MAXN = 1010;int a[MAXN][MAXN];int lowbit(int x){ return x & (-x);}void add(int x, int y, int v){ for(int i = x; i <= MAXN; i += lowbit(i)) for(int j = y; j <= MAXN; j += lowbit(j)) a[i][j] += v;}int sum(int x, int y){ int s = 0; for(int i = x; i > 0; i -= lowbit(i)) for(int j = y; j > 0; j -= lowbit(j)) s += a[i][j]; return s;}int main(){ int T, n, x1, x2, y1, y2, v, cas=0; char ch[3]; scanf("%d", &T); while(T--) { memset(a, 0, sizeof(a)); for(int i = 1; i < MAXN; ++i) for(int j = 1; j < MAXN; ++j) add(i, j, 1); scanf("%d", &n); printf("Case %d:\n", ++cas); while(n--) { scanf("%s", ch); if(ch[0] == 'A') { scanf("%d%d%d", &x1, &y1, &v); add(x1+1, y1+1, v); } else if(ch[0] == 'S') { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); ++x1; ++x2; ++y1; ++y2; if(x1 > x2) swap(x1, x2); if(y1 > y2) swap(y1, y2); int temp; temp=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1); printf("%d\n",temp); } else if(ch[0] == 'D') { scanf("%d%d%d",&x1,&y1,&v); ++x1; ++y1; int temp; temp=sum(x1,y1)-sum(x1-1,y1)-sum(x1,y1-1)+sum(x1-1,y1-1); if(v > temp) v = temp; add(x1, y1, -v); } else if(ch[0] == 'M') { scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v); ++x1; ++x2; ++y1; ++y2; int temp; temp=sum(x1,y1)-sum(x1-1,y1)-sum(x1,y1-1)+sum(x1-1,y1-1); if(v > temp) v = temp; add(x1, y1, -v); add(x2, y2, v); } } } return 0;}#endif
阅读全文
0 0
- 二维树状数组(4种操作)
- 树状数组(二维)
- 小细节!!!!(二维树状数组的相关操作)
- poj1195(二维树状数组)
- poj2155(二维树状数组)
- poj1195 (二维树状数组)
- HLJUOJ1118(二维树状数组)
- POJ_1195 (二维树状数组)
- Poj1195(二维树状数组)
- POJ2155Matrix(二维树状数组)
- hdu5571Triple(二维树状数组)
- 1016(二维树状数组)
- 树状数组模板+poj1195(二维树状数组)
- 树状数组专题--Problem B(二维树状数组)
- 树状数组升级版(二维树状数组)
- poj 2155 Matrix(二维树状数组)
- poj 2155(二维树状数组)
- hdu 1892(二维树状数组模板)
- **Leetcode 123 Best Time to Buy and Sell Stock III
- 利用AD13设计PCB的问题总结11-20
- 使用百度map画扇形覆盖物
- Hbase 建表基本命令总结
- WebView 使用详解
- 二维树状数组(4种操作)
- 我是一个线程
- 常见算法
- 洛谷 P1007 独木桥
- 【JZOJ5330】【NOIP提高组模拟】密码(库默尔定理、数位DP)
- Java基础部分(一)
- nativescript 工程如何clean?
- 单例模式的测试
- 训练日记-16