思路:首先我们将问题转换一下,变成问在某个点左下角的权值和,那么每一个询问可以拆成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. 对SLIP,PPP,PPPoE,EtherNet的理解。[zz]

    经常看到PPP,PPPoE这些名词,当时也查了不少的资料,但是一直不太理解这是什么东西,干什么用的,今天静下心来,多看了点资料,有了一些初步理解,记录下来,以后有了新的理解再修改.   首先,SLIP ...

  2. libevent学习文档(二)eventbase相关接口和参数

    Setting up a default event_base The event_base_new() function allocates and returns a new event base ...

  3. 使用rabbitmq消息队列

    一.前言 在python中本身就是存在队列queue.一个是线程队列queue,另一个是进程multiprocessing中的队列Queue. 线程queue:只用于线程之间的数据交互 进程Queue ...

  4. Kubernetes Job配置

    我们知道使用kubernetes的rc或者rs创建的pod,kubernetes会实时监控其健康状态,如果发现pod挂掉以后,会自动启动一个新的,让pod的数量始终保持在指定的replicas上.那么 ...

  5. Java SE/EE/ME概念理解(Java版本发展历史)

    继上一篇文章http://www.cnblogs.com/EasonJim/p/6181981.html中说的区别,其实分析的不够彻底,因此再次在这里做详细的分析. 零.Java与Sun.Oracle ...

  6. 重构改善既有代码设计--重构手法11:Move Field (搬移字段)

    你的程序中,某个字段被其所驻类之外的另一个类更多的用到.在目标类建立一个新字段,修改源字段的所有用户,令它们改用新字段.        动机:在类之间移动状态和行为,是重构过程中必不可少的措施.随着系 ...

  7. MySQl学习-——Mysql体系结构与Mysql存储引擎

    Mysql体系结构与Mysql存储引擎 Mysql体系结构 mysql体系结构图:

  8. img图片居中

    关键词:clear: both;    display: block;    margin:auto; 图片居左,居右,居中: /* Alignment */ .alignleft { display ...

  9. 【洛谷 P4219】 [BJOI2014]大融合(LCT)

    题目链接 维护子树信息向来不是\(LCT\)所擅长的,所以我没搞懂qwq 权当背背模板吧.Flash巨佬的blog里面写了虽然我没看懂. #include <cstdio> #define ...

  10. Daily Report-1126

    今日: 上午主要是回顾了react,阅读官方文档的时候发现了list中key值设计的必要性. 看了部分react源码,发现有些吃力,在询问羽牧学长之后调整策略,从redux和mobx入手,先多熟悉用法 ...