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过 ...
随机推荐
- Windows10 桌面显示 我的电脑
桌面上面有我的电脑还是非常方便的 但是 不激活有时候没法选择: 方法 桌面空白处右键---个性化 然后在 个性化- 主题- 桌面图标设置 增加即可.
- Nginx负载均衡配置与负载策略
原理 负载均衡的目的是为了解决单个节点压力过大,造成Web服务响应过慢,严重的情况下导致服务瘫痪,无法正常提供服务. 应用场景 春节期间在12306网站上买过火车票的朋友应该深有体会,有时查询一张火车 ...
- laravel报404错误与NGINX报404错误区别
nginx自己配置的404页面 和laravel配置的404页面:如果报了404 :执行laravel的404页面: 那这个404页面对nginx来说意味着什么 laravel 路由和页面找 ...
- swusec的构想,顺便送开学福利——校园网一号多登录演示
前言: 我不是什么大牛,我只想通过我的努力,打造swu网络安全爱好者的圈子.期待你加入. swusec是什么? swusec (SouthWestUniversity SecurityTeam),西南 ...
- jion()说明
转自:http://www.cnblogs.com/skywang12345/p/3479275.html jion(),只有当子线程执行完了,主线程才会执行 1. join()介绍 join() 定 ...
- HashMap的put方法返回值问题
API文档中的描述: 先看一个例子 Map<Character, Integer> map = new HashMap<Character, Integer>(); Syste ...
- CF1088F Ehab and a weird weight formula 贪心 倍增
CF1088F Ehab and a weird weight formula 题意 给定一棵树,点有点权,其中这棵树满足除了权值最小的点外,每个点至少有一个点权小于它的相邻点. 要求你重新构建这棵树 ...
- BZOJ 2521: [Shoi2010]最小生成树
2521: [Shoi2010]最小生成树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 445 Solved: 262[Submit][Statu ...
- uoj318 [NOI2017]蔬菜 【贪心 + 堆 + 并查集】
题目链接 uoj 题解 以前看别人博客,在考场上用费用流做,一直以为这题是毒瘤网络流题 没想到竟然是贪心模拟题... 如果只有一个蔬菜呢?这就是一个经典的普及难度的贪心,正着推面临优先选择的困难,而逆 ...
- linux内核分析 第七周 Linux内核如何装载和启动一个可执行程序
一.编译链接的过程和ELF可执行文件格式 vi hello.c gcc -E -o hello.cpp hello.c -m32 //预处理.c文件,预处理包括把include的文件包含进来以及宏替换 ...