题面

Time Limit: 50 Sec  Memory Limit: 128 MB

Description

你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

命令

参数限制

内容

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

输入文件第一行一个正整数N。
接下来每行一个操作。
 

Output

对于每个2操作,输出一个对应的答案。
 

Sample Input

4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3

Sample Output

3
5

HINT

1<=N<=500000,操作数不超过200000个,内存限制20M。
对于100%的数据,操作1中的A不超过2000。
 
解题思路
 
cdq分治,原问题分为三维:时间,横坐标,纵坐标,之后cdq分治解决时间, 排序解决横坐标,树状数组解决纵坐标,时间复杂度O(nlog^2n)
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib> using namespace std;
const int MAXN = ;
typedef long long LL; inline int rd(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?:;ch=getchar();}
while(isdigit(ch)) {x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
} int n,cnt,Num;
LL f[MAXN],ans[MAXN]; struct Query{
int x,y,type;
int val,id,num;
friend bool operator<(const Query A,const Query B){
if(A.x!=B.x) return A.x<B.x;
if(A.y!=B.y) return A.y<B.y;
return A.type<B.type;
}
}q[MAXN<<],tmp[MAXN<<]; void add(int x,int y){
for(;x<=n;x+=x&-x) f[x]+=y;
} LL sum(int x){
LL ret=;
for(;x;x-=x&-x) ret+=f[x];
return ret;
} void Clear(int x){
for(;x<=n;x+=x&-x) f[x]=;
} void cdq(int l,int r){
if(l==r) return;
int mid=l+r>>;cdq(l,mid);cdq(mid+,r);
int L=l,R=mid+,o=;
while(L<=mid && R<=r) {
if(q[L]<q[R]) {
if(q[L].type==) add(q[L].y,q[L].val);
tmp[++o]=q[L++];
}
else{
if(q[R].type==) ans[q[R].num]+=q[R].val*sum(q[R].y);
tmp[++o]=q[R++];
}
}
while(L<=mid) tmp[++o]=q[L++];
while(R<=r) {
if(q[R].type==) ans[q[R].num]+=q[R].val*sum(q[R].y);
tmp[++o]=q[R++];
}
for(register int i=l;i<=mid;i++) if(q[i].type==) Clear(q[i].y);
for(register int i=;i<=o;i++) q[i+l-]=tmp[i];
} int main(){
n=rd();
int op,x1,x2,y1,y2;
while(){
op=rd();if(op==) break;
if(op==) {
q[++cnt].type=op;q[cnt].x=rd();q[cnt].y=rd();
q[cnt].val=rd();q[cnt].id=cnt;
}
else{
x1=rd(),y1=rd(),x2=rd(),y2=rd();
q[++cnt].type=op;q[cnt].x=x1-;q[cnt].y=y1-;
q[cnt].val=;q[cnt].id=cnt;q[cnt].num=++Num;
q[++cnt].type=op;q[cnt].x=x1-;q[cnt].y=y2;
q[cnt].val=-;q[cnt].id=cnt;q[cnt].num=Num;
q[++cnt].type=op;q[cnt].x=x2;q[cnt].y=y1-;
q[cnt].val=-;q[cnt].id=cnt;q[cnt].num=Num;
q[++cnt].type=op;q[cnt].x=x2;q[cnt].y=y2;
q[cnt].val=;q[cnt].id=cnt;q[cnt].num=Num;
}
}
// cout<<cnt<<endl;
// for(int i=1;i<=cnt;i++)
// cout<<q[i].type<<" "<<q[i].x<<" "<<q[i].y<<" "<<q[i].val<<" "<<q[i].id<<" "<<q[i].num<<endl;
cdq(,cnt);
for(int i=;i<=Num;i++) printf("%lld\n",ans[i]);
return ;
}

BZOJ 2683: 简单题(CDQ 分治)的更多相关文章

  1. BZOJ 2683: 简单题 [CDQ分治]

    同上题 那你为什么又发一个? 因为我用另一种写法又写了一遍... 不用排序,$CDQ$分治的时候归并排序 快了1000ms... #include <iostream> #include ...

  2. BZOJ 2683 简单题 cdq分治+树状数组

    题意:链接 **方法:**cdq分治+树状数组 解析: 首先对于这道题,看了范围之后.二维的数据结构是显然不能过的.于是我们可能会考虑把一维排序之后还有一位上数据结构什么的,然而cdq分治却可以非常好 ...

  3. BZOJ 2683 简单题 ——CDQ分治

    [题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...

  4. bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...

  5. 【BZOJ-1176&2683】Mokia&简单题 CDQ分治

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  6. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  7. bzoj2683简单题 cdq分治

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1803  Solved: 731[Submit][Status][Discuss] ...

  8. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

  9. 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治

    [BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...

随机推荐

  1. 2019亚太内容分发大会,阿里云获CDN领袖奖、技术突破奖

    近日,亚太CDN产业联盟主办的2019亚太内容分发大会在上海召开.本次大会以"5G分发"为主题,集结了CDN领域近千名行业领袖.专家参与.在会上,阿里云斩获“CDN领袖奖”.“技术 ...

  2. 爬虫-Requests 使用入门

    requests 的底层实现其实就是 urllib json在线解析工具 ---------------------------------------------- Linux alias命令用于设 ...

  3. 安装和设置kubectl命令

    Linux [root@cx-- ~]# curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.13.5/bin/ ...

  4. Http学习(三)

    HTTP的问题: 通信使用明文,可能会遭到窃听:HTTP本身不具备加密功能,根据TCP/IP协议工作的线路上可能会遭到窃听,即使通信内容已经加密,也会被看到 通信加密:通过SSL(Secure Soc ...

  5. c# 中Linq Lambda 的ToLookup方法的使用

    同样直接上代码: List<Student> ss = new List<Student>(); Student ss1 = , Age = , Name = " } ...

  6. Systm.IO.File.cs

    ylbtech-Systm.IO.File.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5619 ...

  7. Python 日期和时间_python 当前日期时间_python日期格式化

    Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...

  8. 使用gulp搭建less编译环境

    什么是less? 一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, Fi ...

  9. PAT甲级——A1123 Is It a Complete AVL Tree【30】

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  10. Java之RabbitMQ(一)与SpringBoot整合

    应用场景:异步处理.应用解耦.流量削峰 参照:https://blog.csdn.net/weixin_38364973/article/details/82834348 开端 RabbitAutoC ...