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 ...
随机推荐
- 【串线篇】Mybatis之SSM整合
SSM:Spring+SpringMVC+MyBatis 建立Java web项目 一.导包 1).Spring: [aop核心] com.springsource.net.sf.cglib-2.2. ...
- Halo(十二)
@RequestBody @ResponseBody @RequestBody 1) 该注解用于读取 Request 请求的 body 部分数据,使用系统默认配置的 HttpMessageConver ...
- BZOJ 2547: [Ctsc2002]玩具兵(二分答案+二分图匹配)
传送门 解题思路 可以发现天兵不用管,答案的一个上界是\(2*k\),就是天兵一个个换.刚开始写了个拆\(6\)点的网络流,调了半天发现自己假了..说说正解,首先可以发现交换士兵其实就是种类的交换,那 ...
- Extjs的一些基础使用!
一.获取元素(Getting Elements) 1. Ext.get() var el = Ext.getCmp('id');//获取元素,等同于document.getElementById('i ...
- linux从head.s到start_kernelstart_kernel之---内核解压到重定位分析
一: arm linux 内核生成过程 1. 依据arch/arm/kernel/vmlinux.lds 生成linux内核源码根目录下的vmlinux,这个vmlinux属于未压缩,带调试信息.符号 ...
- 测开之路五十一:代码实现MongoDB增删改查
初始化时连接.析构时断开连接 from pymongo import MongoClient class Mogo(object): def __init__(self, host='127.0.0. ...
- charles抓包看性能数据
1.优化某个接口或加载速度(H5加载速度慢) 抓包看Overview ①看Duration,就是接口的加载时间 ②看Latency,就是延时一端传播到另一端所花费的时间:一般和网络有关:可以综合Dur ...
- 【python】 读写文件
#标准输出 sys.stdout.write() sys.stderr.write() #标准输入 while True : try: line = raw_input().rstrip(); exc ...
- java中四种访问修饰符区别及详解全过程
客户端程序员:即在其应用中使用数据类型的类消费者,他的目标是收集各种用来实现快速应用开发的类. 类创建者:即创建新数据类型的程序员,目标是构建类. 访问控制存在的原因:a.让客户端程序员无法触及他们不 ...
- Oracle 11g Compound Trigger
Original Link In Oracle 11g, the concept of compound trigger was introduced. A compound trigger is a ...