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$. 想法: 开始想这个题的大佬们,给 ...
随机推荐
- python解析robot framework的output.xml,并生成html
一.背景 Jenkins自动构建RF脚本,生成的RF特有HTML报告不能正常打开. 需求:用Python解析测试报告的xml数据,放在普通HTML文件中打开 二.output.xml数据 三.用pyh ...
- cocoapods Analyzing dependencies 问题的解决方案
pod install --verbose --no-repo-update pod update --verbose --no-repo-update 修改就ok了
- java selenium (一) selenium 介绍
Selenium 是目前用的最广泛的Web UI 自动化测试框架. 本系列文章,将深入简出来讲解selenium 的用法 文章的末尾处, 有整个系列的链接 阅读目录 selenium 的命名 sele ...
- POJ 3278 Catch That Cow
注:本人英语很渣,题目大意大多来自百度~=0= 题目大意 农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在N (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ...
- CentOS下升级python2.7.10过程记录
1.准备好一份Python2.7.10源码包,下载地址: http://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz 2.安装Python2.7 ...
- Sql:多行合并一行以及多条数据取时间最早的那条
有两个导数据的需求,1.一张表里面每一个订单号可能对应多条数据,每个单号返回时间最早的那条. 2.根据条件查询某个字段并按照逗号,合并在一起. 表类似结构如下: 第一条sql:select c.Id, ...
- CentOS x64上Matlab R2015b的镜像安装方法与卸载
0. 原料 (1). CentOS_x64系统 CentOS 2.6.32-573.el6.x86_64 (2). Matlab R2015b_glnxa64.iso,可以从百度网盘下载到:链接: ...
- 杭电--1162--Eddy's picture--并查集
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- win8.1安装Matlab7.0的兼容问题
Matlab7.0安装完成后打开就立即关闭,此时右键点击MATLAB7.0快捷方式-"用图形处理器运行"-"更改默认图形处理器",将"集成图形&qu ...
- Selenium Webdriver下click失效问题解决
最近在使用Selenium Webdriver(Selenium2.0)进行界面自动化测试的时候发现单击事件无效,通过driver.findElement的方式是可以找到click元素的,但是就是cl ...