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

【题意】1000*1000个格子,每个格子里放一本书,可以增加,减少,移动,求给出矩阵内的书的数量

【思路】二维树状数组,要注意的是,从坐标0,0开始,为了避免while(0),都加一进行更新,减少的时候要比较当前的数量是否大于要减少的数量

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int N=+;
int n;
int sum[N][N];
int lowbit(int x)
{
return x&(-x);
} void update(int x,int y,int a)
{
while(x<N)
{
int t=y;
while(t<N)
{
sum[x][t]+=a;
t+=lowbit(t);
}
x+=lowbit(x);
}
}
int get_sum(int x,int y)
{
int res=;
while(x)
{
int t=y;
while(t)
{
res+=sum[x][t];
t-=lowbit(t);
}
x-=lowbit(x);
}
return res;
}
int get_a(int x,int y)
{
return get_sum(x,y)-get_sum(x-,y)-get_sum(x,y-)+get_sum(x-,y-);
} int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",cas++);
scanf("%d",&n);
memset(sum,,sizeof(sum));
for(int i=; i<=N; i++)
{
for(int j=; j<=N; j++)
{
update(i,j,);
}
} for(int i=; i<=n; i++)
{
char s[];
int x,y,a,b,z; scanf("%s ",s);
if(s[]=='A'||s[]=='D')
{
scanf("%d%d%d",&x,&y,&a);
if(s[]=='D') a=-min(a,get_a(x+,y+));//判断现在的数量是否多于要减少的数量,如果不是,就全搬走就可。
update(x+,y+,a);
}
else if(s[]=='M')
{
scanf("%d%d%d%d%d",&x,&y,&a,&b,&z);
z=min(z,get_a(x+,y+));
update(x+,y+,-z);
update(a+,b+,z);
}
else
{
scanf("%d%d%d%d",&x,&y,&a,&b);
if(a<x) swap(a,x);
if(b<y) swap(b,y);
int tmp=get_sum(a+,b+)-get_sum(a+,y)-get_sum(x,b+)+get_sum(x,y);
printf("%d\n",tmp);
}
}
} return ;
}

See you~_树状数组的更多相关文章

  1. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  2. BZOJ_3653_谈笑风生_树状数组

    BZOJ_3653_谈笑风生_树状数组 Description 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ...

  3. BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树

    BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...

  4. BZOJ_2141_排队_树状数组+分块

    BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...

  5. BZOJ_3132_上帝造题的七分钟_树状数组

    BZOJ_3132_上帝造题的七分钟_树状数组 Description “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b), ...

  6. nyoj116_士兵杀敌(二)_树状数组

    士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...

  7. [bzoj2527][Poi2011]Meteors_整体二分_树状数组

    Meteors bzoj-2527 Poi-2011 题目大意:题目链接. 注释:略. 想法: 首先答案可以离线,且具有单调性. 这里的单调性就是随着时间的推移,每个国家收集的陨石数增加. 不难想到整 ...

  8. [bzoj2738]矩阵乘法_整体二分_树状数组

    矩阵乘法 bzoj-2738 题目大意:给定一个$n*n$的矩阵.每次给定一个矩阵求矩阵$k$小值. 注释:$1\le n\le 500$,$1\le q\le 6\cdot 10^4$. 想法: 新 ...

  9. [bzoj3289]Mato的文件管理_莫队_树状数组

    Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...

随机推荐

  1. C#winform调整控件的位置

    现在有三个控件并排放置 第二个控件有隐藏功能 隐藏后第一个控件和第三个控件的距离要缩小,于是就要改变第三个控件的位置 尝试用Location.X属性去设置,但是被告知此非变量 于是只能另外想办法 搜到 ...

  2. C#精髓 第四讲 GridView 72般绝技

    http://blog.csdn.net/21aspnet/article/details/1540301

  3. react参考项目001

    https://github.com/chen2009277025/webpack-ant-design-demo https://github.com/cobish/cobish.github.io ...

  4. Can't connect to MySQL server on '127.0.0.1' (111)

    [root@localhost ~]# service mysqld statusmysqld 已停 (1)查看MySQL 服务是否已经开启: service mysqld  status (2)启动 ...

  5. 1526. Martian Plates

    http://acm.timus.ru/problem.aspx?space=1&num=1526 题目大意: 可以从n个碟子中任选一个放在桌子上(不断往上放),也可以把桌子上最顶端的盘子拿走 ...

  6. Swift函数

    函数 函数 介绍 // func // 在Swift中,一个个的方法就是函数 // 1.定义函数的关键字是func // 在定义函数的时候,不管有没有参数都加括号,参数写在括号中 // 在定义函数时, ...

  7. Switch图形练习

    //package IfAndSwitchs;import java.util.Scanner; public class Mianji { public static void main(Strin ...

  8. iOS权限问题

    判断相机权限: NSString *mediaType = AVMediaTypeVideo; AVAuthorizationStatus authStatus = [AVCaptureDevice ...

  9. CodeForces 474B E(Contest #1)

    题意: 给你一个数n,代表n段区间,接下来有n个数(a1,a2,...an)代表每段区间的长度,第一段区间为[1,a1],第二段区间为[a1+1,a1+a2],...第i段区间为[ai-1+1,ai- ...

  10. CentOS 5系统安装Django、Apache 、mod_wsgi部署Python环境教程

    Django,是一款针对Python环境的WEB开发框架,能够帮助我们构架快捷.简单的WEB框架设置,Django框架非常适合开发内容应用环境,所以在本文中,麦子将整理基于Centos系统部署安装Dj ...