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. Jmeter性能测试入门(链接收藏)

    Jmeter性能测试入门: http://www.cnblogs.com/TankXiao/p/4045439.html

  2. Ubuntu无法识别显示器情况下,高分辨率的设置

    安装ubuntu后,出现无法识别显示器,从而造成无法设置高分辨率. 界面显示似老年机般,5.3的视力+强迫症,臣妾的内心是十分拒绝的,捣鼓了半天终于搞定,这里记录下方法. (一)使用xrandr命令, ...

  3. 文顶顶iOS开发博客链接整理及部分项目源代码下载

    文顶顶iOS开发博客链接整理及部分项目源代码下载   网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...

  4. PHP正则表达式替换站点关键字链接后空白的问题解决

    标题这样不知道合适不合适.具体的情况是这样的:网站要增加关键字链接功能,然后需要对文章的内容进行正则表达式匹配并替换,然后使用了preg_replace函数.替换的程序代码如下: function R ...

  5. 【图像处理】【SEED-VPM】4.串口调试信息

    —————————————————————————————————————————————————————————————————————— 串口返回正确的信息 Booting PSP Boot Lo ...

  6. 破解Google Gmail的https新思路

    最近,Google针对gmail被攻击事件,全面默认启用了始终以https访问Gmail的方式了.但是,对于可以动用整个国家力量的黑客来说,从网络通讯数据中(在此不讨论对用户电脑种木马破解https的 ...

  7. UAC在注册表中的对应位置

    UAC在注册表中的对应位置 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System 相关键值设置: U ...

  8. MicroERP如何配置网络应用

    概述:        MicroERP支持多种数据库运行,其中单机版数据库格式为Access,网络版数据库可以SQLServer.Oracle.MySQL等.以下分别以Access和SQLServer ...

  9. Shell脚本8种字符串截取方法总结

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. 代码如下: echo ${va ...

  10. myeclipse激活法,可以试一试

    我的myeclipse2014也是这样激活: 第一步:输入任意用户名 第二步:点击Systemid... 按钮,自动生成本机器的systemid. 第三步: 点菜单Tools->RebuildK ...