思路:首先我们将问题转换一下,变成问在某个点左下角的权值和,那么每一个询问可以拆成4的这样的询问,然后

进行CDQ 分治,回溯的时候按x轴排序,然后用树状数组维护y的值。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define x2 skdjflsdg
#define y2 sdkfjsktge using namespace std; const int N = 2e6 + ;
const int M = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int s, w, cnt, tot, op; LL ans[M], a[N];
struct Qus {
int x, y, idx, id;
} qus[M], tmp[M]; void modify(int x, int v) {
for(int i = x; i < N; i += i & -i) {
a[i] += v;
}
} LL sum(int x) {
LL ans = ;
for(int i = x; i; i -= i & -i) {
ans += a[i];
}
return ans;
} void cdq(int l, int r) { if(l == r) return;
int mid = l + r >> ;
cdq(l, mid); cdq(mid + , r); int p = l, q = mid + , cnt = l; for(int i = mid + ; i <= r; i++) {
while(p <= mid && qus[p].x <= qus[i].x) {
if(qus[p].idx == )
modify(qus[p].y, qus[p].id);
p++;
}
ans[qus[i].id] += qus[i].idx * sum(qus[i].y);
} for(int i = l; i < p; i++) {
if(qus[i].idx == ) {
modify(qus[i].y, -qus[i].id);
}
} p = l, q = mid + , cnt = l; while(p <= mid && q <= r) {
if(qus[p].x <= qus[q].x) {
tmp[cnt++] = qus[p++];
} else {
tmp[cnt++] = qus[q++];
}
} while(p <= mid) tmp[cnt++] = qus[p++];
while(q <= r) tmp[cnt++] = qus[q++]; for(int i = l; i <= r; i++) qus[i] = tmp[i]; } int main() {
scanf("%d%d", &s, &w);
while(scanf("%d", &op) && op < ) {
if(op == ) {
int x, y, a; scanf("%d%d%d", &x, &y, &a);
x += ; y += ;
qus[++tot].x = x;
qus[tot].y = y;
qus[tot].id = a;
qus[tot].idx = ;
} else {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1 += , y1 += ;
x2 += , y2 += ;
qus[++tot].x = x2;
qus[tot].y = y2;
qus[tot].id = ++cnt;
qus[tot].idx = ; qus[++tot].x = x2;
qus[tot].y = y1 - ;
qus[tot].id = cnt;
qus[tot].idx = -; qus[++tot].x = x1 - ;
qus[tot].y = y2;
qus[tot].id = cnt;
qus[tot].idx = -; qus[++tot].x = x1 - ;
qus[tot].y = y1 - ;
qus[tot].id = cnt;
qus[tot].idx = ; ans[cnt] = 1ll * (x2 - x1) * (y2 - y1) * s;
}
}
cdq(, tot);
for(int i = ; i <= cnt; i++)
printf("%lld\n", ans[i]);
return ;
}
/*
*/

bzoj 1176 CDQ分治的更多相关文章

  1. bzoj 1176 cdq分治套树状数组

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

  2. BZOJ 1537 cdq分治

    思路: 我只是想写一下cdq-- 二维偏序 一维排序 一维cdq分治 (我忘了归并排序怎么写了,,,) 写了个sort- 复杂度是O(nlog^2n) //By SiriusRen #include ...

  3. BZOJ 3262 cdq分治 OR 树套树

    注意判断 三个条件都一样的-- (CDQ分治 其实并不是很难理解 只是想不到--) CDQ分治: //By SiriusRen #include <cstdio> #include < ...

  4. bzoj 2683 CDQ分治

    题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...

  5. BZOJ - 1935 / 1176 cdq分治 三维偏序

    题意:给定n*m的网格,且给出n个(x,y)表示该网格已被占有,q次询问(x1,y1)到(x2,y2)的网格中有多少个被占有,n,m范围1e7,q范围5e5 cdq按x轴排序,树状数组维护y轴 #in ...

  6. 【BZOJ4237】稻草人(CDQ分治,单调栈)

    [BZOJ4237]稻草人(CDQ分治,单调栈) 题面 BZOJ 题解 \(CDQ\)分治好题呀 假设固定一个左下角的点 那么,我们可以找到的右下角长什么样子??? 发现什么? 在右侧是一个单调递减的 ...

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

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

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

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

  9. 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...

随机推荐

  1. mac和linux下sed使用的不同

    http://note.youdao.com/noteshare?id=84a6a0eb3e3f1698c9de4f48db24226e

  2. STL源码分析-string

    http://note.youdao.com/noteshare?id=c9b53b7d3379939aae7c05d1ea141a42

  3. 为Azure Web Site 添加ADFS验证支持之一 设置ADFS的信任关系

    很多时候企业开发的应用都会通过AD(Active Directory)进行验证用户名密码的,在企业里面统一一个AD来进行账号密码管理也是一个很好的实践.当企业打算将一个应用迁移到Azure的时候,使用 ...

  4. python---Scrapy模块的使用(二)

    出处:http://www.cnblogs.com/wupeiqi/ 一:去除重复URL scrapy默认使用 scrapy.dupefilter.RFPDupeFilter 进行去重,相关配置有: ...

  5. 【Java-GUI】homework~QQ登录界面

    话说有图有真相:(图片文件自己ps吧,动态网页未添加成功,后附html源码) Java源码: import javax.swing.*; import java.awt.*; import java. ...

  6. Mock InjectMocks ( @Mock 和 @InjectMocks )区别

    之前一直对这两个注解的区别不是很明白. 搜到过一篇博客园的文章举例说明了代码行为的区别.后来在stackoverflow上看到一个问答简单明了的解释了这两个注解在定义上的区别: 在此翻译记录一下: / ...

  7. 【CodeForces】576 D. Flights for Regular Customers

    [题目]D. Flights for Regular Customers [题意]给定n个点m条边的有向图,每条边有di表示在经过该边前必须先经过di条边,边可重复经过,求1到n的最小经过边数.n,m ...

  8. easyUI导出数据

    easyUI导出数据模式 后台: //导出数据 public function index_doExport() { $search['diqu']=$_POST['diqu']; $search[' ...

  9. Spring Boot中对log4j进行多环境不同日志级别的控制

    之前介绍了在<Spring boot中使用log4j记录日志>,仅通过log4j.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需 ...

  10. 【leetcode 简单】第十四题 最后一个单词的长度

    给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...