W过大,很难在线维护,考虑离线算法
给每个操作加一个时间属性n,显然,对于n=i的询问,对它有影响的修改只在n<i中,所以可以CDQ(因为是按时间序读进来的,所以不用排序了
对于统计矩形和,可以使用二维前缀和的思想,即只需要统计四个点即可
这样就转化成了三维偏序问题,只是询问和修改要分开处理。
(初始值不用管,输出的时候加上就行
 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int n,s,qu,tot,ans[N],t[N],p[N],cnt,q[N];
struct qwe
{
int o,x,y,v,n;
qwe(){}
qwe(int a1,int a2,int a3,int a4,int a5)
{
o=a1,x=a2,y=a3,v=a4,n=a5;
}
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
return (a.x==b.x&&a.y==b.y&&a.o<b.o)||(a.x==b.x&&a.y<b.y)||(a.x<b.x);
}
int read()
{
int r=,f=;
char p=getchar();
while(p>''||p<'')
{
if(p=='-')
f=-;
p=getchar();
}
while(p>=''&&p<='')
{
r=r*+p-;
p=getchar();
}
return r*f;
}
int lb(int x)
{
return x&(-x);
}
void update(int x,int v)
{
for(int i=x;i<=n;i+=lb(i))
t[i]+=v;
}
int ques(int x)
{
int r=;
for(int i=x;i>=;i-=lb(i))
r+=t[i];
return r;
}
void cdq(int l,int r)
{
if(l==r)
return;
int mid=(l+r)/,tmp=;
cdq(l,mid);
cdq(mid+,r);
sort(a+l,a+mid+,cmp);
sort(a+mid+,a+r+,cmp);
int i=l,j=mid+;
while(j<=r)
{
for(;a[i].o==&&i<=mid;i++);
for(;a[j].o==&&j<=r;j++);
if(i<=mid&&a[i].x<=a[j].x)
update(a[i].y,a[i].v),i++,tmp=i-;
else if(j<=r)
ans[a[j].n]+=ques(a[j].y),j++;
}
for(int t=l;t<=tmp;t++)
if(a[t].o==)
update(a[t].y,-a[t].v);
}
int main()
{
s=read();n=read();
while()
{
qu=read();
if(qu==)
break;
if(qu==)
{
int x=read(),y=read(),z=read();
a[++tot]=qwe(,x,y,z,tot);
}
else
{
int x1=read(),y1=read(),x2=read(),y2=read();
p[++cnt]=tot;
q[cnt]=(x2-x1+)*(y2-y1+)*s;
a[++tot]=qwe(,x1-,y1-,,tot);
a[++tot]=qwe(,x2,y2,,tot);
a[++tot]=qwe(,x1-,y2,,tot);
a[++tot]=qwe(,x2,y1-,,tot);
}
}
cdq(,tot);
for(int i=;i<=cnt;i++)
printf("%d\n",q[i]+ans[p[i]+]+ans[p[i]+]-ans[p[i]+]-ans[p[i]+]);
return ;
}
 

bzoj 1176 [Balkan2007]Mokia 【CDQ分治】的更多相关文章

  1. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  2. BZOJ 1176[Balkan2007]Mokia(CDQ分治)

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

  3. BZOJ 1176: [Balkan2007]Mokia [CDQ分治]

    题意: 有一个n * n的棋盘,每个格子内有一个数,初始的时候全部为0.现在要求维护两种操作: 1)Add:将格子(x, y)内的数加上A. 2)Query:询问矩阵(x0, y0, x1, y1)内 ...

  4. BZOJ 1176 [Balkan2007]Mokia ——CDQ分治

    [题目分析] 同BZOJ2683,只需要提前处理s对结果的影响即可. CDQ的思路还是很清晰的. 排序解决一维, 分治时间, 树状数组解决一维. 复杂度是两个log [代码] #include < ...

  5. BZOJ 1176: [Balkan2007]Mokia

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

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

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

  7. BZOJ1176: [Balkan2007]Mokia CDQ分治

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

  8. BZOJ 1176: [Balkan2007]Mokia KDtree

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin), ...

  9. BZOJ 1176 Mokia CDQ分治+树状数组

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

随机推荐

  1. 【Leetcode】经典的Jump Game in JAVA

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. Comparable 和 Comparator的理解

    对Comparable 的解释 Comparable是一个排序接口 此接口给实现类提供了一个排序的方法,此接口有且只有一个方法 public int compareTo(T o); compareTo ...

  3. Linux主要命令

    pwd  查看当前路径 cd ..    表示后一级目录级 cd .   表示当前目录 cd ../..   后退两级 cd  表示进入当前家目录 date 返回当前的一个具体时间    -s  修改 ...

  4. Quartz深入浅出(二)

    Hello Quartz / 本文通过一个简单的样例让大家高速了解Quartz,上手.并了解Quartz内的一些关键对象 如 Scheduler.Job.Trigger.JobExecutionCon ...

  5. redis中的五种基本的数据结构

    1 String 基本的数据类型. 2 list 2.1 将元素放入一个list中 rpush mylist A rpush mylist B rpush mylist A 如果mylist本来是不存 ...

  6. Flume接收器组的指数退避上限

    指数退避 agent.sinkgroups.sg1.sinks=k1,k2,k3agent.sinkgroups.sg1.processor.type=failoveragent.sinkgroups ...

  7. jquery和CSS3带倒影的3D万花筒旋转动画特效效果演示

    <!DOCTYPE html> <html> <head> <title></title> <meta charset='utf-8' ...

  8. B.大钉骑马走江湖

    江湖是什么,对于在象棋界厮杀的大钉来说,江湖就是一个矩阵,他的目标,就是在江湖之中骑着马,从他的位置出发,走到终点. 当然,大钉的马也遵从中国象棋中的“马走日”的规则,而且在矩阵中,也会有一些障碍物, ...

  9. hihocoder #1068 : RMQ-ST算法 ( RMQ算法 O(nlogn)处理 O(1)查询 *【模板】 1)初始化d数组直接读入+计算k值用数学函数log2()==*节约时间 )

    #1068 : RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备 ...

  10. MYSQL进阶学习笔记十一:MySQL 表的分析,检查和优化!(视频序号:进阶_28)

    知识点十二:MySQL 表的分析,检查和优化(28) 表的分析,检查和优化: 定期分析表: ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, ...