二维树状数组模板(1892)


See you~

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4451    Accepted Submission(s): 1412


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

注意二维树状数组更新和求前缀和,对两个维度分别更新和求前缀和,最关键的在于求区间和的时候,需要一个大矩形减去两个小矩形,然后加上两个小矩形的重叠部分,就是所求区间和了。


/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 10E9+7
#define MAX 500050
/*---------------------Work-----------------------*/
int Q;
int tree[1050][1050]; //树状数组
int cnt[1050][1050]; //记录位置有多少本书
int lowbit(int i)
{
	return i&-i;
}
void update(int x,int y,int num)
{
	int i=y; //临时寄存y值,后面还需要y值
	while(x<=1005) //对两个维度分别更新
	{
		y=i;
		while(y<=1005)
		{
			tree[x][y]+=num;
			y+=lowbit(y);
		}
		x+=lowbit(x);
	}
}
int getsum(int x,int y)
{
	int sum=0,i=y;
	while(x>=1)
	{
		y=i;
		while(y>=1)
		{
			sum+=tree[x][y];
			y-=lowbit(y);
		}
		x-=lowbit(x);
	}
	return sum;
}
void work()
{
	int T; cin>>T;
	for(int Case=1;Case<=T;Case++)
	{
		memset(tree,0,sizeof(tree));
		memset(cnt,0,sizeof(cnt));
		scanf("%d",&Q);
		for(int i=1;i<=1005;i++)
			for(int j=1;j<=1005;j++)
			{
				update(i,j,1);
				cnt[i][j]=1;
			}
		printf("Case %d:\n",Case);
		char ch[5];
		int x1,x2,n1,y1,y2;
		for(int i=1;i<=Q;i++)
		{
			scanf("%s",ch);
			if(ch[0]=='S')
			{
				scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
				x1++,x2++,y1++,y2++; //树状数组不能处理0
				if(x1>x2) swap(x1,x2); //未必左下角+右上角
				if(y1>y2) swap(y1,y2);
				//注意二维树状数组区间和的计算式
				printf("%d\n",getsum(x2,y2)-getsum(x2,y1-1)-getsum(x1-1,y2)+getsum(x1-1,y1-1));
			}
			else if(ch[0]=='A')
			{
				scanf("%d%d%d",&x1,&y1,&n1);
				x1++,y1++;
				update(x1,y1,n1);
				cnt[x1][y1]+=n1;
			}
			else if(ch[0]=='D')
			{
				scanf("%d%d%d",&x1,&y1,&n1);
				x1++,y1++;
				if(n1>cnt[x1][y1]) n1=cnt[x1][y1];
				update(x1,y1,-n1);
				cnt[x1][y1]-=n1;
			}
			else if(ch[0]=='M')
			{
				scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
				x1++,y1++,x2++,y2++;
				if(n1>cnt[x1][y1]) n1=cnt[x1][y1];
				update(x1,y1,-n1);
				update(x2,y2,n1);
				cnt[x1][y1]-=n1;
				cnt[x2][y2]+=n1;
			}
		}
	}
}
/*------------------Main Function------------------*/
int main()
{
	//freopen("test.txt","r",stdin);
	//freopen("cowtour.out","w",stdout);
	//freopen("cowtour.in","r",stdin);
	work();
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值