bzoj 1176 cdq分治套树状数组
题面:
维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.
Input
第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小
接下来每行为一下三种输入之一(不包含引号):
"1 x y a"
"2 x1 y1 x2 y2"
"3"
输入1:你需要把(x,y)(第x行第y列)的格子权值增加a
输入2:你需要求出以左下角为(x1,y1),右上角为(x2,y2)的矩阵内所有格子的权值和,并输出
输入3:表示输入结束
Output
对于每个输入2,输出一行,即输入2的答案
思路:正常思路用树状数组套树状数组,但是w范围过大,开不下。考虑用cdq分治套树状数组。我们用cdq分治对x坐标排序,用树状数组维护y坐标,把询问用差分思想拆分成四个不同的询问即可。
代码:
#include <bits/stdc++.h>
#define LL long long
#define lowbit (x & (-x))
using namespace std;
const int maxn = 200010;
int tot, tot_ans, n;
struct query{
int type, x, y, pos, flag;
};
query q[maxn], tmp[maxn];
int c[maxn * 10], ans[maxn];
void add(int x, int y) {
for (; x <= n; x += lowbit)
c[x] += y;
}
int ask(int x) {
int ans = 0;
for (; x; x-= lowbit) {
ans += c[x];
}
return ans;
}
void clear(int x) {
for (; x <= n; x += lowbit) {
c[x] = 0;
}
}
void cdq(int l, int r) {
if(l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid);
cdq(mid + 1, r);
int l1 = l, l2 = mid + 1, pos = l;
while(l1 <= mid && l2 <= r) {
if(q[l1].x <= q[l2].x) {
if(q[l1].type == 1) {
add(q[l1].y, q[l1].pos);
}
tmp[pos++] = q[l1++];
} else {
if(q[l2].type == 2) {
ans[q[l2].pos] += q[l2].flag * ask(q[l2].y);
}
tmp[pos++] = q[l2++];
}
}
while(l1 <= mid) tmp[pos++] = q[l1++];
while(l2 <= r) {
if(q[l2].type == 2) {
ans[q[l2].pos] += q[l2].flag * ask(q[l2].y);
}
tmp[pos++] = q[l2++];
}
for (int i = l; i <= mid; i++) {
if(q[i].type == 1) {
clear(q[i].y);
}
}
for (int i = l; i <= r; i++)
q[i] = tmp[i];
}
int main() {
int l1, r1, l2, r2, x, op, val;
scanf("%d%d", &x, &n);
while(~scanf("%d", &op) && op != 3) {
if(op == 1) {
scanf("%d%d%d", &l1, &r1, &val);
q[++tot] = (query){1, l1, r1, val, 0};
} else {
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
tot_ans++;
ans[tot_ans] = x * (l2 - l1 + 1) * (r2 - r1 + 1);
q[++tot] = (query){2, l1 - 1, r1 - 1, tot_ans, 1};
q[++tot] = (query){2, l1 - 1, r2, tot_ans, -1};
q[++tot] = (query){2, l2, r1 - 1, tot_ans, -1};
q[++tot] = (query){2, l2, r2, tot_ans, 1};
}
}
cdq(1, tot);
for (int i = 1; i <= tot_ans; i++) {
printf("%d\n", ans[i]);
}
}
bzoj 1176 cdq分治套树状数组的更多相关文章
- bzoj2253纸箱堆叠(动态规划+cdq分治套树状数组)
Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , 之后,即可自动化生产三边边长为 (a mod P,a^2 mod p,a^3 mod P) (a^4 ...
- [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)
[APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)
题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...
- BZOJ 2716 [Violet 3]天使玩偶 (CDQ分治、树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2716 怎么KD树跑得都那么快啊..我写的CDQ分治被暴虐 做四遍CDQ分治,每次求一个 ...
- 【BZOJ4285】使者 cdq分治+扫描线+树状数组
[BZOJ4285]使者 Description 公元 8192 年,人类进入星际大航海时代.在不懈的努力之下,人类占领了宇宙中的 n 个行星,并在这些行星之间修建了 n - 1 条星际航道,使得任意 ...
- 【CJOJ2616】 【HZOI 2016】偏序 I(cdq分治,树状数组)
传送门 CJOJ Solution 考虑这是一个四维偏序对吧. 直接cdq套在一起,然后这题有两种实现方法(树状数组的更快!) 代码实现1(cdq+cdq+cdq) /* mail: mleautom ...
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 【模板】cdq分治代替树状数组(单点修改,区间查询)
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...
随机推荐
- elasticsearch查询与sql对应关系
must: AND must_not:NOT should:OR
- ARC101E Ribbons on Tree 容斥原理+dp
题目链接 https://atcoder.jp/contests/arc101/tasks/arc101_c 题解 直接容斥.题目要求每一条边都被覆盖,那么我们就容斥至少有几条边没有被覆盖. 那么没有 ...
- NOSQL导图
- C#基础提升系列——C#委托
C# 委托 委托是类型安全的类,它定义了返回类型和参数的类型,委托类可以包含一个或多个方法的引用.可以使用lambda表达式实现参数是委托类型的方法. 委托 当需要把一个方法作为参数传递给另一个方法时 ...
- 服务器构建CentOS+Jenkins+Git+Maven之爬坑
ssh端口变更后,git如何访问远端中央代码库 参考来源: http://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin http://blog.csdn ...
- 【MySQL】实现自增函数sequence
前言 当前数据库为:mysql由于mysql和oracle不太一样,不支持直接的sequence,所以需要创建一张table来模拟sequence的功能,理由sql语句如下: 步骤 1.创建seque ...
- 【2019 Multi-University Training Contest 2】
01: 02: 03: 04: 05:https://www.cnblogs.com/myx12345/p/11584100.html 06: 07: 08:https://www.cnblogs.c ...
- [CSP-S模拟测试]:联盟(搜索+树的直径)
题目描述 $G$国周边的$n$个小国家构成一个联盟以抵御$G$国入侵,为互相支援,他们建立了$n−1$条双向通路,使得任意两个国家可以经过通路相互到达.当一个国家受到攻击时,所有其它国家都会沿着最短路 ...
- 20175203 2018-2019 实验三 《敏捷开发与XP实践》
20175203 2018-2019 实验三 <敏捷开发与XP实践> 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课 ...
- SSAS MDX语句 期末查询简单示例
WITH Member [Measures].[num Last Day of Month] AS( [时间].[YQMD].CurrentMember.LastChild,[Measures].[门 ...