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++ map的遍历

    一般使用迭代器遍历比较方便. map<string,int> m; map<string,int>::iterator it; it = m.begin(); while(it ...

  2. SQL server 查询某个表在哪些存储过程(SP)中使用到

    1.查询某个表被哪些存储过程(以下简称 SP)使用到 : select distinct object_name(id) from syscomments where id in (select ob ...

  3. checkbox的check事件

    <input type="checkbox" id="NameFlag" name="originalName" onClick=&q ...

  4. Android Monkey 压力测试 介绍

    Monkey 是Android SDK提供的一个命令行工具, 可以简单,方便地运行在任何版本的Android模拟器和实体设备上. Monkey会发送伪随机的用户事件流,适合对app做压力测试 阅读目录 ...

  5. php简单单例模式

    所谓单例模式,适用于使用一个对象可以完成所有的业务逻辑的类(一般不考虑继承的类) //单例模式 function getInstance($class_name){ //创建一个存储各种需要单例的类的 ...

  6. [原创]在Linux系统Ubuntu14.04上安装部署docker。

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...

  7. mongodb-索引

    说明:创建索引时,列名:int 中的int数字指的是正序或者倒序,如果是1表明是正序,-1表示倒序 1.查询collection上的索引 db.users.getIndexes() 2.查询当前的db ...

  8. 夕甲甲——孔乙己之C++版

    欧欧匹代码的格局,是和别的编程模式不同的:首先要有一个构造函数:基类里只定义了函数的形式,可以随时通过派生增加不同的实现.那些程序员们,每每学会了继承和多态,便可以接一个项目,——这是十年前的事,现在 ...

  9. MVC模式与Android

    MVC模式是软件工程中的一种软件架构,“Model-View-Controller”的缩写,中文翻译为“模型-视图-控制器”. MVC模式将一个交互式应用程序分为3各组件: 1.Model(模型):业 ...

  10. 搬运:Python for Windows——监控Windows某个目录下文件的变化

    https://win32com.goermezer.de/content/view/286/285/ 这个网站真是给力,不多说,代码直接搬运过来,还有我的测试结果,拿走不谢! import os i ...