Bzoj2683 简单题 [CDQ分治]
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 1071 Solved: 428
Description
|
命令 |
参数限制 |
内容 |
|
1 x y A |
1<=x,y<=N,A是正整数 |
将格子x,y里的数字加上A |
|
2 x1 y1 x2 y2 |
1<=x1<= x2<=N 1<=y1<= y2<=N |
输出x1 y1 x2 y2这个矩形内的数字和 |
|
3 |
无 |
终止程序 |
Input
Output
Sample Input
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
5
HINT
Source
CDQ分治
第4遍回顾之前抄的代码的时候,突然顿悟。
个人理解,这种分治方法类似于做矩形面积并时候用到的扫描线法。将每个区间修改操作拆成插入/删除,和每个询问操作一起按横坐标x升序排序。
用一个一维数组记录“当前横坐标”对应的y轴情况,从左往右扫描所有操作,并用差分的方式完成修改(在时间维度上差分),记录答案。
↑该一维数组可以用树状数组优化,扫描操作可以用分治方法优化(每层分治时处理前半部分操作对后半部分查询的影响)。
↑组合起来就成了CDQ分治。
________
PS1: 这时我想起,两三个个月前RLQ说他研究出一种用树状数组乱搞二维大数据的做法,当时没怎么听懂,也没太在意……卧槽,原来是CDQ分治?
CDQ分治要是晚出现两年,就变成RLQ分治了……%%%%%
PS2: 之前抄的LCT也已经回顾了10+遍了,是不是也快要顿悟了呢……
________
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
struct opt{
int flag;
int x,y,w;
int t;
int id;
}a[mxn*],b[mxn*];
int cnt=;
int cmp(opt q,opt e){
if(q.x==e.x){
if(q.y==e.y)return q.flag<e.flag;
return q.y<e.y;
}
return q.x<e.x;
}
int ans[mxn];
int t[mxn*];
void add(int x,int v){while(x<=n){t[x]+=v;x+=x&-x;}return;}
int sum(int x){
int res=;
while(x){res+=t[x];x-=x&-x;}
return res;
}
void solve(int l,int r){
if(l>=r)return;
int i,j,mid=(l+r)>>;
int l1=l,l2=mid+;
for(i=l;i<=r;i++){
if(a[i].flag== && a[i].t<=mid) add(a[i].y,a[i].w);
else if(a[i].flag== && a[i].t>mid) ans[a[i].id]+=sum(a[i].y)*a[i].w;
}
for(i=l;i<=r;i++)
if(a[i].flag== && a[i].t<=mid) add(a[i].y,-a[i].w);
for(i=l;i<=r;i++)
if(a[i].t<=mid)b[l1++]=a[i];
else b[l2++]=a[i];
for(i=l;i<=r;i++)a[i]=b[i];
solve(l,mid);solve(mid+,r);
return;
}
int main(){
n=read();
int i,j,x,y,c,v;
int id=;
while(){
c=read();
if(c==)break;
if(c==){//修改
x=read();y=read();v=read();
a[++cnt].flag=;a[cnt].x=x;a[cnt].y=y;a[cnt].w=v;
a[cnt].t=cnt;
}
else{//查询
x=read();y=read();c=read();v=read();
a[++cnt].flag=;a[cnt].x=x-;a[cnt].y=y-;
a[cnt].w=;a[cnt].t=cnt;a[cnt].id=++id;
a[++cnt].flag=;a[cnt].x=x-;a[cnt].y=v;
a[cnt].w=-;a[cnt].t=cnt;a[cnt].id=id;
a[++cnt].flag=;a[cnt].x=c;a[cnt].y=y-;
a[cnt].w=-;a[cnt].t=cnt;a[cnt].id=id;
a[++cnt].flag=;a[cnt].x=c;a[cnt].y=v;
a[cnt].w=;a[cnt].t=cnt;a[cnt].id=id;
}
}
sort(a+,a+cnt+,cmp);
solve(,cnt);
for(i=;i<=id;i++){
printf("%d\n",ans[i]);
}
return ;
}
Bzoj2683 简单题 [CDQ分治]的更多相关文章
- bzoj2683简单题 cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1803 Solved: 731[Submit][Status][Discuss] ...
- BZOJ2683: 简单题(cdq分治 树状数组)
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2142 Solved: 874[Submit][Status][Discuss] Descripti ...
- 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治
[BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...
- 【bzoj1176】[Balkan2007]Mokia/【bzoj2683】简单题 CDQ分治+树状数组
bzoj1176 题目描述 维护一个W*W的矩阵,初始值均为S(题目描述有误,这里的S没有任何作用!).每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数 ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...
- BZOJ 2683: 简单题 [CDQ分治]
同上题 那你为什么又发一个? 因为我用另一种写法又写了一遍... 不用排序,$CDQ$分治的时候归并排序 快了1000ms... #include <iostream> #include ...
- BZOJ 2683 简单题 cdq分治+树状数组
题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...
- BZOJ 2683: 简单题(CDQ 分治)
题面 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: ...
随机推荐
- openwrt编译出错处理记录
1.代码从windows复制过来编译报错处理,参考:http://www.360doc.com/content/13/1016/21/3884271_321966616.shtml 2.编译lua-s ...
- 小图标外链API
网页上有些分享的小图标,比如分享到facebook,weibo,qq空间等功能的时候,图标以前一般是自己做一个css sprite.当一个网站的图标变了的时候,比如facebook变成assbook的 ...
- 使用perl实现scp批量分发
perl模块Net::SCP::Expect批量下发文件 用Net::SSH::Perl和Net::SCP::Expect写部署脚本 scp分发文件的perl脚本 Perl SCP操作 #!/usr/ ...
- Ionic实战四:ionic 即时通讯_ionic仿雅虎邮箱
此产品是一款Ionic版微博.微信.朋友圈效果(微博.微信.聊天列表.聊天窗口.个人界面.编辑个人信息等)购买后二次开发方便快捷.    
- 【Alpha版本】十天冲刺集结令
031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬涛 [Alp ...
- Code First操作Mysql数据库
前面博客也讲了,自己做一个网站,选用的是MVC+EF Code First+MySql+EasyUI,先说下技术选型.一.为什么选择MVC? 因为之前自己做的系统大部分是webForm,MVC的之前也 ...
- sql server死锁神器
参考文章: http://blogs.msdn.com/b/sqlserverfaq/archive/2013/04/27/an-in-depth-look-at-sql-server-memory- ...
- PHP-- 获取http请求头信息
看官方文档: http://php.net/manual/zh/function.apache-request-headers.php http://php.net/manual/zh/functio ...
- 玩转WIN7的MKLINK
引言: 换了新电脑,终于再次使用上啦WIN7 ,经过一个周每天重装N次系统,终于弄好一个像样的系统啦.由于使用rt7lite对WIN7SP1官方整合包进行了适当精简,所以最终系统的稳定性还得经过一段时 ...
- android-之测试框架的使用AndroidTestCase
Android Manifest.xml中所需要添加的配置 <instrumentation android:name="android.test.InstrumentationTes ...