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过 ...
随机推荐
- Excel作为数据源TesTNG做数据驱动完整代码
说明:EXCEL 支持xls 和xlsx 俩种格式 : 已经过测试 ! package main.java; import org.apache.poi.ss.usermodel.*; import ...
- [转帖]nvidia nvlink互联与nvswitch介绍
nvidia nvlink互联与nvswitch介绍 https://www.chiphell.com/thread-1851449-1-1.html 差不多在一个月前在年度gtc会议上,老黄公开了d ...
- RFC 8446
https://tools.ietf.org/html/rfc8446#section-2.3 简要内容.. [Docs] [txt|pdf] [draft-ietf-tls-...] [Tracke ...
- 微信小程序 功能函数 将对象的键添加到数组 (函数深入)
// 将对象的键添加到数组 var arr = Object.keys(site); //英文 https://developer.mozilla.org/en-US/docs/Web/JavaScr ...
- mybatis的setting
在mybaits中,setting的的配置参数如下(如果不在配置文件中配置将使用默认值): 设置参数 描述 有效值 默认值 cacheEnabled 该配置影响的所有映射器中配置的缓存的全局开关 tr ...
- java 静态类与静态方法应用场景
静态类:工具类 例如 Array.sort(arry) 静态方法:设置文件名
- 腾讯下载的视频qlv格式转化为MP4格式
最近在看腾讯视频的时候发现下载下来的视频格式都是qlv格式,且不能用其他播放器播放,甚是恼怒,网上找了很多方法都很繁琐,于是自己写了一个小程序来处理这个问题.把下载下来的qlv格式转化为MP4格式 首 ...
- Backdooring a OS VM
Backdooring a OS VM 来源 https://www.cnblogs.com/studyskill/p/6524672.html 提示: 1.经过实验,fortios 5.4 be ...
- 【刷题】BZOJ 3591 最长上升子序列
Description 给出1~n的一个排列的一个最长上升子序列,求原排列可能的种类数. Input 第一行一个整数n. 第二行一个整数k,表示最长上升子序列的长度. 第三行k个整数,表示这个最长上升 ...
- linux内核分析综合总结
linux内核分析综合总结 zl + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核分析 ...