思路:首先我们将问题转换一下,变成问在某个点左下角的权值和,那么每一个询问可以拆成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. springboot用mybatis-generator自动生成mapper和model

    转:http://blog.csdn.net/u011493599/article/details/53928379 1.在pom.xml里添加maven插件 <plugin> <g ...

  2. JSTL与EL与OGNL

    springMVC使用JSTL与EL表达式: spring MV默认的jsp页面的标签就是JSTL,而struts2默认的是OGNL标签. struts2 使用OGNL与EL表达式:OGNL用stru ...

  3. bzoj 2929 [Poi1999]洞穴攀行 网络流

    2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 499  Solved: 295[Submit][Status][ ...

  4. tomcat maven插件启动报错tomcat-users.xml cannot be read

    tomcat maven插件启动报错tomcat-users.xml cannot be read [ERROR] Failed to execute goal org.codehaus.mojo:t ...

  5. HTML5 表单自学记录

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 关于WEB-INF目录不提供外部访问及JSP引用 js,css 文件路径问题

    在 web 项目开发过程中,我们常常使用到 JSP,以及对静态资源,js,css 等引用,但是我们应该把这些资源文件放在哪个目录下面咧,怎么引用. 当然如果是前后端分离的项目倒不用考虑这些. WEB- ...

  7. 多例模式,保证实例的唯一性,仅适用于form窗体

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. HDU 5700 优先队列(或者multiset) 或 线段树

    题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即 ...

  9. DIV+CSS制作斜线效果记录

    DIV+CSS 斜线效果很简单,只需设置一下CSS Border 的边框就能有斜线效果.代码分享给大家,你可以自己变通. 提示要注意两点:1.DIV宽高的定义.2.DIV在 IE6 中默认是有高度的. ...

  10. mysql查询日期相关的

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...