http://www.lydsy.com/JudgeOnline/problem.php?id=1176

Description(题面本人自行修改了一下)

维护一个W*W的矩阵,初始值均为0.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.

Input

第一行两个整数,S,W;其中S为数据编号;W为矩阵大小
接下来每行为一下三种输入之一(不包含引号):
"1 x y a"
"2 x1 y1 x2 y2"
"3"
输入1:你需要把(x,y)(第x行第y列)的格子权值增加a
输入2:你需要求出以左下角为(x1,y1),右上角为(x2,y2)的矩阵内所有格子的权值和,并输出
输入3:表示输入结束

Output

对于每个输入2,输出一行,即输入2的答案

Sample Input

0 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

保证答案不会超过int范围

————————————————————————————--

显然二维树状数组又是不可做(而且真二维的话会爆空间)

那么就到了我们用CDQ的时候了。

首先想修改好降维,但是查询不好降维。

但是查询本身就可以(由于容斥原理)变成求四个前缀和然后互相加减就是答案,所以我们就有办法存了。

那么CDQ先按x排序,然后树状数组跑一遍y即可。

(这里差点质疑起了CDQ的正确性,但是……我发现我把代码看错了……之后我就不质疑了……滑稽)

(这里注意一定要对pos排序(pos记录它是第几次查询,修改的pos为0),就算修改放查询前面了只要我们的t是对的它就不会有影响,然而我们把修改放查询后面了就没辙了)

(这题的代码因为是借(抄)鉴的hzw神犇的代码,所以归并排序写法与前面的不一样……)

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int M=;
const int Q=;
const int W=;
inline int read(){
int X=,w=; char ch=;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct num{
int x;
int y;
int add;
int t;
int pos;
}q[M*],tmp[M*];
int s,w,cnt=,t=,ans[Q],tree[W];
bool cmp(num a,num b){
if(a.x==b.x&&a.y==b.y)return a.pos<b.pos;
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
inline int lowbit(int t){return t&(-t);}
void add(int x,int y){//将a[x]+y
for(int i=x;i<=w;i+=lowbit(i))tree[i]+=y;
return;
}
ll query(int x){//1-x区间和
ll res=;
for(int i=x;i>;i-=lowbit(i))res+=tree[i];
return res;
}
void addq(){
int x1=read();
int y1=read();
int x2=read();
int y2=read();
int pos=++cnt;
q[++t].pos=pos;q[t].t=t;q[t].x=x1-;q[t].y=y1-;q[t].add=;
q[++t].pos=pos;q[t].t=t;q[t].x=x2;q[t].y=y2;q[t].add=;
q[++t].pos=pos;q[t].t=t;q[t].x=x1-;q[t].y=y2;q[t].add=-;
q[++t].pos=pos;q[t].t=t;q[t].x=x2;q[t].y=y1-;q[t].add=-;
return;
}
void cdq(int l,int r){
if(l>=r)return;
int mid=(l+r)>>;
for(int i=l;i<=r;i++){
if(q[i].t<=mid&&!q[i].pos)add(q[i].y,q[i].add);
if(q[i].t>mid&&q[i].pos)ans[q[i].pos]+=q[i].add*query(q[i].y);
}
for(int i=l;i<=r;i++){
if(q[i].t<=mid&&!q[i].pos)add(q[i].y,-q[i].add);
}
int l1=l,l2=mid+;
for(int i=l;i<=r;i++){
if(q[i].t<=mid)tmp[l1++]=q[i];
else tmp[l2++]=q[i];
}
for(int i=l;i<=r;i++)q[i]=tmp[i];
cdq(l,mid);cdq(mid+,r);
return;
}
int main(){
s=read();//这东西没用
w=read();
while(){
int k=read();
if(k==)break;
if(k==){
q[++t].x=read();
q[t].y=read();
q[t].add=read();
q[t].t=t;
}else{
addq();
}
}
sort(q+,q+t+,cmp);
cdq(,t);
for(int i=;i<=cnt;i++)printf("%d\n",ans[i]);
return ;
}

BZOJ1176:[Balkan2007]Mokia——题解的更多相关文章

  1. [BZOJ1176][Balkan2007]Mokia cdq+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3134  Solved: 1395[Submit][S ...

  2. BZOJ1176 [Balkan2007]Mokia 【CDQ分治】

    题目 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. 输入格式 ...

  3. Bzoj1176 [Balkan2007]Mokia

    Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2000  Solved: 890 Description 维护一个W*W的矩阵,初始值均为S.每次操作 ...

  4. BZOJ1176: [Balkan2007]Mokia CDQ分治

    最近很不对啊=w= 写程序全是bug啊 ans数组开小了竟然一直不知道,小数据没问题大数据拍不过,交上去RE 蛋疼半天 这个主要把每次询问拆成3个询问. #include<cstdio> ...

  5. bzoj1176: [Balkan2007]Mokia【cdq分治】

    把询问搞成4个,cdq分治. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a;i <= b; i++) #d ...

  6. 2018.09.16 bzoj1176: [Balkan2007]Mokia(cdq分治)

    传送门 调了半天发现是输出优化打错了求心理阴影体积233 这题很简单啊. 一个修改操作x如果对一个询问操作y有贡献那么有. tx<ty,Xx<=Xy,Yx<=Yy" rol ...

  7. bzoj千题计划144:bzoj1176: [Balkan2007]Mokia

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ分治 #include<cstdio> #include<iostre ...

  8. 【kd-tree】bzoj1176 [Balkan2007]Mokia

    裸题不多说,注意在sqrt(n*log(n))次插入后重构树以保持深度. #include<cstdio> #include<cmath> #include<algori ...

  9. bzoj2683(要改一点代码)&&bzoj1176: [Balkan2007]Mokia

    仍然是一道cdq模版.. 那么对于一个询问,容斥一下分成四个,变成问(1,1)~(x,y),那么对于x,y,修改只有x'<=x&&y'<=y,才对询问有影响,那么加上读入顺 ...

随机推荐

  1. 【JUC源码解析】FutureTask

    简介 FutureTask, 一个支持取消行为的异步任务执行器. 概述 FutureTask实现了Future,提供了start, cancel, query等功能,并且实现了Runnable接口,可 ...

  2. 微服务介绍及Asp.net Core实战项目系列之微服务介绍

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.微服务选型 在做微服务架构的技术选型的时候,我们以“无侵入”和“社区活跃”为主要的考量点,将来升级为原子服务架构.量子服务架构 ...

  3. Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  4. Egret入门(三)--创建HelloWorld项目(4.0-使用Egret Wing)

    准备 编辑器: Egret Wing3(4.0.3) 需要下载安装 语言: TepyScript(JS的超集,参考手册http://bbs.egret.com/thread-1441-1-1.html ...

  5. IO多路复用(二) -- select、poll、epoll实现TCP反射程序

    接着上文IO多路复用(一)-- Select.Poll.Epoll,接下来将演示一个TCP回射程序,源代码来自于该博文https://www.cnblogs.com/Anker/p/3258674.h ...

  6. 33.[LeetCode] Search in Rotated Sorted Array

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. org.apache.poi版本问题

    问题描述: 今天跑一段历史代码,发现不能启动,抛出java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK 问题 解决办法: 把org.apache.poi的 ...

  8. A Product Recall 产品召回

    Rick: The Board of Directors has come to a decision. Our company will take an image hit, and it's go ...

  9. Scrum立会报告+燃尽图(十月十日总第一次):选题

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190 Scrum立会master:付佳 一.小组介绍 组长:付佳 组员: ...

  10. SQL连接的方法

    1.创建连接字符串 string con = "Data Source=127.0.0.1;Initial Catalog=dingdan;Integrated Security=True& ...