Dynamic Rankings(动态第k大+树套树)
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112
题目:

思路:
树套树板子题。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 5e4 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; char op[];
int n, q, x, y, tot1, tot2, cnt;
int a[maxn], q1[maxn], q2[maxn], root1[maxn], root2[maxn];
vector<int> v; struct que {
int op, l, r, x;
}ask[maxn]; struct node {
int l, r, sum;
}tree[maxn*]; int getid(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + ;
} void update(int l, int r, int& x, int y, int pos, int val) {
tree[++cnt] = tree[y], tree[cnt].sum += val, x = cnt;
if(l == r) return;
int mid = (l + r) >> ;
if(pos <= mid) update(l, mid, tree[x].l, tree[y].l, pos, val);
else update(mid + , r, tree[x].r, tree[y].r, pos, val);
} int query(int l, int r, int x, int y, int k) {
if(l == r) return l;
int cnt = tree[tree[y].l].sum - tree[tree[x].l].sum;
for(int i = ; i < tot1; ++i) {
cnt -= tree[tree[q1[i]].l].sum;
}
for(int i = ; i < tot2; ++i) {
cnt += tree[tree[q2[i]].l].sum;
}
int mid = (l + r) >> ;
if(cnt >= k) {
for(int i = ; i < tot1; ++i) {
q1[i] = tree[q1[i]].l;
}
for(int i = ; i < tot2; ++i) {
q2[i] = tree[q2[i]].l;
}
return query(l, mid, tree[x].l, tree[y].l, k);
} else {
for(int i = ; i < tot1; ++i) {
q1[i] = tree[q1[i]].r;
}
for(int i = ; i < tot2; ++i) {
q2[i] = tree[q2[i]].r;
}
return query(mid + , r, tree[x].r, tree[y].r, k - cnt);
}
} int main(){
#ifndef ONLINE_JUDGE
FIN;
#endif
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &q);
cnt = tot1 = tot2 = ;
v.clear();
memset(root2, , sizeof(root2));
for(int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
v.push_back(a[i]);
}
for(int i = ; i <= q; ++i) {
scanf("%s", op);
if(op[] == 'C') ask[i].op = ;
else ask[i].op = ;
if(ask[i].op == ) {
scanf("%d%d", &ask[i].l, &ask[i].x);
v.push_back(ask[i].x);
} else {
scanf("%d%d%d", &ask[i].l, &ask[i].r, &ask[i].x);
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int sz = v.size();
for(int i = ; i <= n; ++i) {
a[i] = getid(a[i]);
update(, sz, root1[i], root1[i-], a[i], );
}
for(int i = ; i <= q; ++i) {
int op = ask[i].op;
if(op == ) {
int pos = ask[i].l, x = ask[i].x, y = a[pos];
x = getid(x);
a[pos] = x;
while(pos <= n) {
update(, sz, root2[pos], root2[pos], y, -);
update(, sz, root2[pos], root2[pos], x, );
pos += lowbit(pos);
}
} else {
int l = ask[i].l, r = ask[i].r, k = ask[i].x;
int x = l - ;
tot1 = tot2 = ;
while(x) {
q1[tot1++] = root2[x];
x -= lowbit(x);
}
x = r;
while(x) {
q2[tot2++] = root2[x];
x -= lowbit(x);
}
printf("%d\n", v[query(, sz, root1[l-], root1[r], k)-]);
}
}
}
return ;
}
Dynamic Rankings(动态第k大+树套树)的更多相关文章
- zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap
Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- zoj2112 主席树动态第k大 (主席树&&树状数组)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- P2617 Dynamic Rankings (动态开点权值线段树 + 树状数组)
题意:带修求区间k小 题解:回忆在使用主席树求区间k小时 利用前缀和的思想 既然是前缀和 那么我们可以使用更擅长维护前缀和的树状数组 但是这里每一颗权值线段树就不是带版本的 而是维护数组里i号点的权值 ...
- ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树套树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 这题调了我相当长的时间,1wa1a,我是第一次写树套树,这个是树状数组套splay,在每个区间 ...
- ZOJ2112 BZOJ1901 Dynamic Rankings 树套树 带修改的区间第k小
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 树套树,线段树套splay或者主席树套树状数组,我抄了一下hzwer ...
- bzoj 1901: Zju2112 Dynamic Rankings(树套树)
1901: Zju2112 Dynamic Rankings 经典的带改动求区间第k小值问题 树套树模板,我是用的线段树套splay实现的,并且用的数组模拟的,所以可能空间略大,bzoj过了,zoj过 ...
随机推荐
- WDS迁移注意事项
先说背景:公司使用WDS来部署操作系统,目前DHCP和WDS都安装在同一台服务器上,但是此服务器已过保,所以筹划迁移,将WDS和DHCP分别迁移到两台服务器上.迁移计划是保持WDS暂时不动,DHCP先 ...
- 多校联赛7 1001 hdu 4666(最远哈曼顿距离+优先队列)
吐个糟,尼玛今天被虐成狗了,一题都没搞出来,这题搞了N久居然还是搞不出来,一直TLE,最后还是参考别人代码才领悟的,思路就这么简单, 就是不会转弯,看着模板却不会改,艹,真怀疑自己是不是个笨蛋题意:求 ...
- MacOS & .DS_Store
MacOS & .DS_Store .DS_Store === Desktop Services Store https://en.wikipedia.org/wiki/.DS_Store h ...
- Codeforces 893F(主席树+dfs序)
在子树内和距离不超过k是一个二维限制,容易想到主席树,但主席树显然没法查最小值,因为不满足区间可减.kdtree和二维线段树可以干这事,但肯定会T飞.但事实上我们的问题有一个特殊性:对某个点x,查询其 ...
- JAVA ACM 基础
java ACM Java做ACM-ICPC的特点: (1) 在一般比赛中,Java程序会有额外的时间和空间,而实际上经过实验,在执行计算密集任务的时候Java并不比C/C++慢多少,只是IO操作较慢 ...
- pragma指令详解(转载)
#pragma comment( comment-type [,"commentstring"] ) 该宏放置一个注释到对象文件或者可执行文件.comment-type是一个预定义 ...
- 【刷题】BZOJ 2816 [ZJOI2012]网络
Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf Solution 维护树上联通块的信息,支持动态加边删边 LCT 总共 ...
- Mininet 系列实验(二)
实验内容 分别通过命令行创建.Python脚本编写以及交互式界面创建来熟悉Mininet的基本功能. 参考 Mininet命令延伸实验扩展 实验环境 虚拟机:Oracle VM VirtualBox ...
- 【poj3294】 Life Forms
http://poj.org/problem?id=3294 (题目链接) 题意 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. Solution 后缀数组论文题.. 将 n 个字符串 ...
- 开源入侵检测系统SELKS系统搭建
一.系统环境配置 系统环境:centos7x64 ip地址:172.16.91.130 1.设置静态IP地址 [root@localhost backlion]#vi /etc/sys ...