See you~_树状数组
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.
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.
For each "S" query, just print out the total number of books in that area.
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
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~_树状数组的更多相关文章
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- BZOJ_3653_谈笑风生_树状数组
BZOJ_3653_谈笑风生_树状数组 Description 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ...
- BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树
BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...
- BZOJ_2141_排队_树状数组+分块
BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...
- BZOJ_3132_上帝造题的七分钟_树状数组
BZOJ_3132_上帝造题的七分钟_树状数组 Description “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b), ...
- nyoj116_士兵杀敌(二)_树状数组
士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常 ...
- [bzoj2527][Poi2011]Meteors_整体二分_树状数组
Meteors bzoj-2527 Poi-2011 题目大意:题目链接. 注释:略. 想法: 首先答案可以离线,且具有单调性. 这里的单调性就是随着时间的推移,每个国家收集的陨石数增加. 不难想到整 ...
- [bzoj2738]矩阵乘法_整体二分_树状数组
矩阵乘法 bzoj-2738 题目大意:给定一个$n*n$的矩阵.每次给定一个矩阵求矩阵$k$小值. 注释:$1\le n\le 500$,$1\le q\le 6\cdot 10^4$. 想法: 新 ...
- [bzoj3289]Mato的文件管理_莫队_树状数组
Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...
随机推荐
- 关于RSA加密算法的长度限制问题
RSA是常用的非对称加密算法.近来有学生在项目中使用System.Security类库中的RSA加密算法时,出现了“不正确的长度”,这实际上是因为待加密的数据超长所致..net Framework中提 ...
- java异常处理的设计
有一句这样话:一个衡量Java设计师水平和开发团队纪律性的好方法就是读读他们应用程序里的异常处理代码. 本文主要讨论开发Java程序时,如何设计异常处理的代码,如何时抛异常,捕获到了怎么处理,而不是讲 ...
- apache和nginx
虽然nginx使用较少 还是写写文章,记录下 nginx是异步非阻塞,apache是阻塞的. apache动态页面比nginx好. 由于nginx的高并发性(使用epoll模型),所以出来静态页面性能 ...
- angular初步认识一
最近比较流行MVC前端框架开发,最近研究了一个框架AngularJS框架 不说那么多,先上例子,我是个代码控 <!DOCTYPE html> <html lang="en& ...
- elasticsearch相关文章
http://blog.csdn.net/laigood12345/article/category/1113868
- as3延迟处理
查找关键字“flashplayer 弹性跑道” 每当一帧即将走完,FlashPlayer就要做些总结性工作(一次性地汇总变化),把这一帧当中发生的变化拿出来展示(渲染)一下. 如果它处理的事情少,工作 ...
- wpf学习笔记
1.菜单:普通菜单.上下文菜单(ContextMenu) <Menu HorizontalAlignment="Left" Height="20" Ver ...
- js报错:email() is not a function
email() is not a function 明明是一个函数,但火狐控制台真J.. 由于JSP文件是别人写好直接使用的,所以,来回测试,折腾!最后,没办法,一段一段代码删除测试,才发现.有for ...
- 【1】springmvc4 + servlet3 零配置全注解入门项目helloword
自从servlet3.0支持注解,使得javaweb项目使用全注解成为可能. 注解化将成为javaweb的发展方向.包括spring去年推出的spring-boot也是全部使用注解. 代码:https ...
- DOM位置参数
以chrome浏览器测试为准 scrollwidth scrollheight clientwidth clientheight offsetwidth offsetheight 对象的实际宽高 包括 ...