cdq分治。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2000500
#define maxq 400050
using namespace std;
int s,w,type,a,b,c,d,cnt=,m=,ans[maxq];
struct ques
{
int val,opt,x,y,pos,id;
}q[maxq],tmp[maxq];
int t[maxn];
bool cmp(ques a,ques b)
{
if ((a.x==b.x) && (a.y==b.y)) return a.opt<b.opt;
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int lowbit(int x)
{
return (x&(-x));
}
void add1(int x)
{
scanf("%d%d%d",&a,&b,&c);
q[++m].id=m;q[m].pos=;q[m].val=c;q[m].opt=;
q[m].x=a;q[m].y=b;
}
void add2(int x)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
cnt++;
q[++m].val=;q[m].opt=;q[m].x=c;q[m].y=d;q[m].pos=cnt;q[m].id=m;
q[++m].val=;q[m].opt=;q[m].x=a-;q[m].y=b-;q[m].pos=cnt;q[m].id=m;
q[++m].val=-;q[m].opt=;q[m].x=a-;q[m].y=d;q[m].pos=cnt;q[m].id=m;
q[++m].val=-;q[m].opt=;q[m].x=c;q[m].y=b-;q[m].pos=cnt;q[m].id=m;
}
void add(int x,int val)
{
for (int i=x;i<=w;i+=lowbit(i))
t[i]+=val;
}
int query(int x)
{
int ret=;
for (int i=x;i>=;i-=lowbit(i))
ret+=t[i];
return ret;
}
void cdq(int left,int right)
{
if (left==right) return;
int mid=left+right>>;
int l1=left,l2=mid+;
for (int i=left;i<=right;i++)
{
if ((q[i].id<=mid) && (!q[i].opt)) add(q[i].y,q[i].val);
else if ((q[i].id>mid) && (q[i].opt)) ans[q[i].pos]+=q[i].val*query(q[i].y);
}
for (int i=left;i<=right;i++)
if ((q[i].id<=mid) && (!q[i].opt)) add(q[i].y,-q[i].val);
for (int i=left;i<=right;i++)
{
if (q[i].id<=mid) tmp[l1++]=q[i];
else tmp[l2++]=q[i];
}
for (int i=left;i<=right;i++)
q[i]=tmp[i];
cdq(left,mid);cdq(mid+,right);
}
int main()
{
scanf("%d%d",&s,&w);
while (scanf("%d",&type)!=EOF)
{
if (type==) break;
if (type==) add1(cnt);
else add2(cnt);
}
sort(q+,q+m+,cmp);
cdq(,m);
for (int i=;i<=cnt;i++)
printf("%d\n",ans[i]);
return ;
}

BZOJ 1176 MOKIA的更多相关文章

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

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

  2. bzoj 1176 Mokia(CDQ分治,BIT)

    [题目链接]  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=96974 [题意] 定义查询操作与修改操作:1 x y z 为 ...

  3. [bzoj] 1176 Mokia || CDQ分治

    原题 给出W×W的矩阵(S没有用,题目有误),给出无限次操作,每次操作的含义为: 输入1:你需要把(x,y)(第x行第y列)的格子权值增加a 输入2:你需要求出以左下角为(x1,y1),右上角为(x2 ...

  4. BZOJ 1176: [Balkan2007]Mokia

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

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

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

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

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

  7. 【BZOJ 1176】【Balkan 2007】Mokia

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 整体二分的例题 把每个询问拆成四个询问,整体二分里x坐标递增,按x坐标扫的时候用树状数组维护y ...

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

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

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

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

随机推荐

  1. Delphi中@,^,#,$特殊符号意义

    概述:   ^: 指针   @: 取址   #: 十进制符   $: 十六进制符   @:取址运算符; var   int:integer;   p:^integer; begin   new(P); ...

  2. setTimeout延时0毫秒的作用和问题

    一 作用 http://www.cnblogs.com/xieex/archive/2008/07/11/1241151.html 经常看到setTimeout延时0ms的javascript代码,感 ...

  3. weblogic 安装与配置

    Weblogic 安装 从官网 下载 需要的weblogic 版本, 解压缩后得到 wls1036_generic.jar [fmw_12.1.3.0.0_wls.jar , fmw_12.2.1.1 ...

  4. sqlmap 使用举例

    详细的请见: http://drops.wooyun.org/tips/143 http://wenku.baidu.com/link?url=45xj6EVVWQjV8upwrBAMIqPP3xMc ...

  5. BS与CS的比较

    http://www.oschina.net/news/57811/open-web-not-died?p=2#comments 浏览器是为浏览文档设计的,它的界面布局其实只有从上至下的流布局一种,浏 ...

  6. 【原创】【ViewFlow+GridView】Parameter must be a descendant of this view问题分析

    关于ViewFlow和GridView嵌套导致Parameter must be a descendant of this view问题的解决方案 [关于ViewFlow]   ViewFlow是一款 ...

  7. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

  8. C++:构造函数默认的参数声明

    C++函数的默认参数指的是在函数声明或者定义时给形式参数指定默认值,从而在调用参数时可以少写参数,少掉的参数用默认值代替.LZ的Display()函数的代码看起来似乎是可以有s2和s3两个默认参数,那 ...

  9. iOS:地图:MapKit和CoreLocation

    地图:MapKit和CoreLocation 简介: 现在很多的社交软件都引入了地图和定位功能,要想实现这2大功能,那就不得不学习其中的2个框架:MaKit和CoreLocation CoreLoca ...

  10. 16_采用SharedPreferences保存用户偏好设置参数

    按钮事件 <Button android:id="@+id/button" android:layout_width="wrap_content" and ...