【BZOJ 1901】【ZJU 2112】Dynamic Rankings
http://www.lydsy.com/JudgeOnline/problem.php?id=1901
重新用整体二分写了一下。
整体二分的思想详见论文。
貌似带修区间k大和静态区间k大都是\(O(n\log^2n)\)。
cdq是对时间分治,整体二分是对答案分治。
对于动态区间k大怎么实现我还想了很长时间呢,感觉模板题都想这么长时间。。。
动态区间k大的关键步骤是把修改变成两个操作,删除和插入。
这样在一个分治函数内部就可以扫时间轴然后在特定权值范围内的删除和插入分别在bits上-1和+1。
相比较在线bits套主席树,离线整体二分跑得飞快。
离散化后跑得能快一点,不离散的话时间复杂度就是\(O(n\log n\log 10^9)\),离散的话时间复杂度是\(O(n\log^2n)\)。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10003;
struct node {int x, y, k, op, id;} B[N << 2], q1[N << 2], q2[N << 2];
int tot = 0, a[N], n, m, bits[N << 1], top = 0, H[N << 1], cnt = 0;
int ans[N], id[N << 2], anstot = 0, que[N << 1], q1len, q2len;
void add(int x, int flag) {for (; x <= n; x += (x & (-x))) bits[x] += flag;}
int sum(int x) {int ret = 0; for (; x; x -= (x & (-x))) ret += bits[x]; return ret;}
void solve(int p, int q, int l, int r) {
if (p > q) return;
if (l == r) {
for (int i = p; i <= q; ++i)
if (B[i].op == 3)
ans[B[i].id] = l;
return;
}
int mid = (l + r) >> 1;
for (int i = p; i <= q; ++i) {
if (B[i].op != 3) {
if (B[i].y <= mid)
add(B[i].x, B[i].op);
}
else que[i] = sum(B[i].y) - sum(B[i].x - 1);
}
for (int i = p; i <= q; ++i)
if (B[i].op != 3 && B[i].y <= mid) add(B[i].x, -B[i].op);
q1len = q2len = 0;
for (int i = p; i <= q; ++i)
if (B[i].op != 3) {
if (B[i].y > mid) q2[++q2len] = B[i];
else q1[++q1len] = B[i];
} else {
if (que[i] >= B[i].k) q1[++q1len] = B[i];
else B[i].k -= que[i], q2[++q2len] = B[i];
}
int tmp = p - 1;
for (int i = 1; i <= q1len; ++i)
B[++tmp] = q1[i];
for (int i = 1; i <= q2len; ++i)
B[++tmp] = q2[i];
tmp = p + q1len - 1;
solve(p, tmp, l, mid);
solve(tmp + 1, q, mid + 1, r);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
B[++tot] = (node) {i, a[i], 0, 1, 0};
H[++cnt] = a[i];
}
int x, y, k;
char c[2];
for (int i = 1; i <= m; ++i) {
scanf("%s", c);
if (c[0] == 'Q') {
scanf("%d%d%d", &x, &y, &k);
B[++tot] = (node) {x, y, k, 3, ++anstot};
} else {
scanf("%d%d", &x, &y);
B[++tot] = (node) {x, a[x], 0, -1, 0};
B[++tot] = (node) {x, y, 0, 1, 0};
H[++cnt] = (a[x] = y);
}
}
stable_sort(H + 1, H + cnt + 1);
cnt = unique(H + 1, H + cnt + 1) - H;
for (int i = 1; i <= tot; ++i)
if (B[i].op != 3)
B[i].y = lower_bound(H + 1, H + cnt, B[i].y) - H;
solve(1, tot, 1, cnt - 1);
for (int i = 1; i <= anstot; ++i) printf("%d\n", H[ans[i]]);
return 0;
}
【BZOJ 1901】【ZJU 2112】Dynamic Rankings的更多相关文章
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
- 【bzoj 1901】Zju2112 Dynamic Rankings
Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是 ...
- 【BZOJ 1901】Zju2112 Dynamic Rankings &&【COGS 257】动态排名系统 树状数组套线段树
外面是树状数组,里面是动态开点线段树,对于查询我们先把有关点找出来,然后一起在线段树上行走,这样就是单个O(log2)的了 #include <cstdio> #include <v ...
- 【BZOJ】【1901】【Zju2112】 Dynamic Rankings
再填个坑. 动态维护区间第K大(带单点修改) 首先裸的区间第K大我们是用的[前缀和]思想,实现O(n)预处理,O(1)找树查询,那么如果是动态的呢?我们可以利用树状数组(BIT)的思想,进行O(log ...
- [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】
题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...
- BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7143 Solved: 2968[Su ...
- 【BZOJ】3052: [wc2013]糖果公园
http://www.lydsy.com/JudgeOnline/problem.php?id=3052 题意:n个带颜色的点(m种),q次询问,每次询问x到y的路径上sum{w[次数]*v[颜色]} ...
- 【BZOJ】3319: 黑白树
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...
- 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...
随机推荐
- 【BZOJ】1709: [Usaco2007 Oct]Super Paintball超级弹珠
[算法]模拟 [题解]O(n^2)预处理横线(y),纵线(x),主对角线(y-x+n),副对角线(x+y). 然后n^2枚举每个点.
- 头像截取 图片上传 js插件
先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...
- perl6文件操作
use v6; #perl6中读取文件方法 #:r 只读, :w 只写, :rw 读写, :a 追加 my $fp = open 'filename.txt', :rw; for $fp.^metho ...
- Django【设计】可插拔的插件方式实现
需求: 在CMDB系统中,我们需要对资产进行采集和资产入库,包括serverBasic.disk.memory.nic信息等,客户端需要采集这些硬件的信息,服务端则负责资产入库,但是需要采集的硬件并不 ...
- C++学习之路(四):线程安全的单例模式
(一)简单介绍 单例模式分为两种类型:懒汉模式和饿汉模式. 懒汉模式:在实际类对象被调用时才会产生一个新的类实例,并在之后返回这个实例.多线程环境下,多线程可能会同时调用接口函数创建新的实例,为了防止 ...
- C++之初始化问题
首先,我们应该明确的是在C++中初始化不是赋值,因为初始化是必要的,如果读取了未初始化的值将会导致不明确的行为.初始化指创建变量并且给它赋初值,而赋值则是擦除对象的当前值并用新值代替.C++支持两种初 ...
- 深入分析_linux_spinlock_实现机制【转】
转自:http://blog.csdn.net/electrombile/article/details/51289813 在 x86 平台上,spinlock 主要通过处理器的 lock 指令前缀实 ...
- python基础===利用PyCharm进行Python远程调试(转)
原文链接:利用PyCharm进行Python远程调试 背景描述 有时候Python应用的代码在本地开发环境运行十分正常,但是放到线上以后却出现了莫名其妙的异常,经过再三排查以后还是找不到问题原因,于是 ...
- sicily 1172. Queens, Knights and Pawns
Description You all are familiar with the famous 8-queens problem which asks you to place 8 queens o ...
- 【uva10779】收集者的难题
按照题意建模就行了. #include<bits/stdc++.h> #define naive 0 #define inf 1000000007 using namespace std; ...