TZOJ 2725 See you~(二维树状数组单点更新区间查询)
描述
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.
输入
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.
输出
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.
样例输入
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
样例输出
Case 1:
1
3
Case 2:
1
4
题意
1000*1000的矩阵上每个格点放了1本书
Q个操作
1.查询[X1,Y1]-[X2,Y2]总共放了几本书
2.在[X1,Y1]增加n1本书
3.在[X1,Y1]移除n1本书,若不够则全移走
4.把[X1,Y1]上的n1本书移到[X2,Y2]上,若不够则全移到[X2,Y2]
题解
二维树状数组单点修改,区间查询
1.区间查询分成4块,([1,1]-[X1,Y1])+([1,1]-[X2,X2])-([1,1]-[X2+1,Y1])-([1,1]-[X1,Y2+1])
2.直接单点更新
3.区间查询单点,这里X2=X1,Y2=Y1,查出来的与n1比个小即为需要移走的数
4.同三
这里操作1,并没有严格[X1,Y1]<[X2,Y2],所以需要交换
代码
#include<bits/stdc++.h>
using namespace std; const int N=;
const int n=; struct BIT2{
int sum[N][N];
void init()
{
memset(sum,,sizeof sum);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
update(i,j,);
}
int lowbit(int x){return x&(-x);}
int update(int x,int y,int w)
{
x++,y++;
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
sum[i][j]+=w;
}
int query(int x,int y)
{
x++,y++;
int ans=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
ans+=sum[i][j];
return ans;
}
}T;
int main()
{
int t,q,x1,y1,x2,y2,n1,o=;
char op[];
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",o++);
T.init();
scanf("%d",&q);
for(int i=;i<q;i++)
{
scanf("%s",op);
if(op[]=='S')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
int ans=T.query(x2,y2)+T.query(x1-,y1-)-T.query(x2,y1-)-T.query(x1-,y2);
printf("%d\n",ans);
}
if(op[]=='A')
{
scanf("%d%d%d",&x1,&y1,&n1);
T.update(x1,y1,n1);
}
if(op[]=='D')
{
scanf("%d%d%d",&x1,&y1,&n1);
int ans=T.query(x1,y1)+T.query(x1-,y1-)-T.query(x1,y1-)-T.query(x1-,y1);
T.update(x1,y1,-min(n1,ans));
}
if(op[]=='M')
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&n1);
int ans=T.query(x1,y1)+T.query(x1-,y1-)-T.query(x1,y1-)-T.query(x1-,y1);
T.update(x1,y1,-min(n1,ans));
T.update(x2,y2,min(n1,ans));
}
}
}
return ;
}
TZOJ 2725 See you~(二维树状数组单点更新区间查询)的更多相关文章
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- hdu2642二维树状数组单点更新+区间查询
http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意:一个星空,二维的.上面有1000*1000的格点,每个格点上有星星在闪烁.一开始时星星全部暗淡着 ...
- 【2018年全国多校算法寒假训练营练习比赛(第五场)-E】情人节的电灯泡(二维树状数组单点更新+区间查询)
试题链接:https://www.nowcoder.com/acm/contest/77/E 题目描述 情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡 ...
- SPOJ - MATSUM 二维树状数组单点更新
忘记了单点更新时要在树状数组中减去原值..wa了一发 /* 矩形求和,单点更改 */ #include<iostream> #include<cstring> #include ...
- hdu2642二维树状数组单点更新
碰到这种题一定要注意坐标是不是有序的,也要注意坐标是不是有0的,有的话需要+1处理 #include<bits/stdc++.h> using namespace std; #define ...
- 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询
题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...
- 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询
题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
随机推荐
- 深度学习原理与框架-神经网络架构 1.神经网络构架 2.激活函数(sigmoid和relu) 3.图片预处理(减去均值和除标准差) 4.dropout(防止过拟合操作)
神经网络构架:主要时表示神经网络的组成,即中间隐藏层的结构 对图片进行说明:我们可以看出图中的层数分布: input layer表示输入层,维度(N_num, input_dim) N_num表示输 ...
- day40-socket编程
一.socket介绍 看socket之前,先来回顾一下五层通讯流程: 但实际上从传输层开始以及以下,都是操作系统帮咱们完成的 Socket又称为套接字,它是应用层与TCP/IP协议族通信的中间软件抽象 ...
- python 阿狸的进阶之路(6)
常用模块 json # 序列化 #将内存的数据存到硬盘中,中间的格式,可以被多种语言识别,跨平台交互数据 #json 可以将字典之类的数据类型存到字典中 import json dic = {&quo ...
- js(鼠标键盘拖动事件)
拖动事件是h5(HTML5的) 1:draggable(true) 2:拖动源 ondragstart ,ondragend 3:目的地 ondraglenter,ondragover,ondragl ...
- ArcGIS模型构建器案例教程-批量复制工作空间所有要素类
ArcGIS模型构建器案例教程-批量复制工作空间所有要素类 目的:批量复制工作空间所有要素类 工具名称:WorkspaceCopyFeatureClasses 使用方法:输入工作空间,指定输出工作空间 ...
- 如何设置java环境变量
以安装目录是E:\Program Files\Java\jDK1.7.0为例:
- puppet自动化运维
Puppet实现自动化运维 一.案例分析 1.案例概述: 随着服务器数量的增多,系统管理员任务量也逐渐增加,这时就需要简洁的.强大的框架来完成系统管理任务为实现这一目的,我们将引入一批工具,这批工具是 ...
- pandas.read_csv用法(转)
的数据结构DataFrame,几乎可以对数据进行任何你想要的操作. 由于现实世界中数据源的格式非常多,pandas也支持了不同数据格式的导入方法,本文介绍pandas如何从csv文件中导入数据. 从上 ...
- Linux部署项目
1 安装jdk 第一步:获取Linux系统中jdk安装包和tomcat安装包(后面要用,所以上传两个) 第二步:使用secureCRT客户端工具连到服务器 第三步:使用命令创建一个目录,作为软件的安装 ...
- slot 插槽的使用
在vue 中父组件中的子组件在子组件中添加内容(html标签.文本内容),在子组件中加入slot这样页面中就会呈现出在父组件填写的内容,例如: 父组件中hello是子组件,在子组件中插入slot这样子 ...