ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大
带修改的区间第K大其实就是先和静态区间第K大的操作一样。先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树状数组的方式去修改,并且修改那些地方。查询的时候就是查询原主席树+树状数组的值。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
//#define lson l,m,rt<<1
//#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = (int)1e9+;
const int N = ;
const int M = ;
int root[N], S[N], lson[M], rson[M], ll[N], rr[N], cnt[M];
int tot, t;
int lsz, rsz;
int a[N], b[N];
struct Node{
int l, r, op, k;
}A[N];
int Build(int l, int r){
int now = ++tot;
cnt[now] = ;
if(l < r){
int m = l+r >> ;
lson[now] = Build(l,m);
rson[now] = Build(m+,r);
}
return now;
}
int Update(int l, int r, int pre, int pos, int v){
int now = ++tot;
cnt[now] = cnt[pre] + v;
if(l < r){
int m = l+r >> ;
if(pos <= m){
rson[now] = rson[pre];
lson[now] = Update(l, m, lson[pre], pos, v);
}
else {
lson[now] = lson[pre];
rson[now] = Update(m+, r, rson[pre], pos, v);
}
cnt[now] = cnt[lson[now]] + cnt[rson[now]];
}
return now;
}
int Query(int l, int r, int L, int R, int k){
if(l == r) return l;
int num1 = cnt[lson[L]];
int num2 = cnt[lson[R]];
for(int i = ; i <= lsz; i++) num1 += cnt[lson[ll[i]]];
for(int i = ; i <= rsz; i++) num2 += cnt[lson[rr[i]]];
num2 -= num1;
int m = l+r >> ;
if(num2 >= k){
for(int i = ; i <= lsz; i++) ll[i] = lson[ll[i]];
for(int i = ; i <= rsz; i++) rr[i] = lson[rr[i]];
return Query(l, m, lson[L], lson[R], k);
}
else {
for(int i = ; i <= lsz; i++) ll[i] = rson[ll[i]];
for(int i = ; i <= rsz; i++) rr[i] = rson[rr[i]];
return Query(m+, r, rson[L], rson[R], k-num2);
}
}
int id(int x){
return lower_bound(b+, b++t, x) - b;
}
int lowbit(int x){
return x&(-x);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
tot = t = ;
int n, m;
char s[];
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%d", &a[i]), b[++t] = a[i];
for(int i = ; i <= m; i++){
scanf("%s", s);
if(s[] == 'Q'){
A[i].op = ;
scanf("%d%d%d", &A[i].l, &A[i].r, &A[i].k);
}
else {
A[i].op = ;
scanf("%d%d", &A[i].l, &A[i].r);
b[++t] = A[i].r;
}
}
sort(b+, b++t);
int tmp = ;
for(int i = ; i <= t; i++)
if(b[i] != b[i-]) b[++tmp] = b[i];
t = tmp;
root[] = Build(, t);
for(int i = ; i <= n; i++){
root[i] = Update(, t, root[i-], id(a[i]), );
}
for(int i = ; i <= n; i++) S[i] = root[];
for(int i = ; i <= m; i++){
if(A[i].op == ){
lsz = rsz = ;
for(int j = A[i].l-; j; j-=lowbit(j)) ll[++lsz] = S[j];
for(int j = A[i].r; j; j-=lowbit(j)) rr[++rsz] = S[j];
printf("%d\n", b[Query(,t,root[A[i].l-], root[A[i].r], A[i].k)]);
}
else {
for(int j = A[i].l; j <= t; j += lowbit(j)) S[j] = Update(, t, S[j], id(a[A[i].l]), -);
for(int j = A[i].l; j <= t; j += lowbit(j)) S[j] = Update(, t, S[j], id(A[i].r), );
a[A[i].l] = A[i].r;
}
}
}
return ;
}
ZOJ 2112
ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大的更多相关文章
- zoj 2112 Dynamic Rankings(主席树&动态第k大)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- ZOJ 2112 Dynamic Rankings(树状数组+主席树)
题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...
- 线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216
poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- 洛谷P2617 Dynamic Rankings 主席树 单点修改 区间查询第 K 大
我们将线段树套在树状数组上,查询前预处理出所有要一起移动的节点编号,并在查询过程中一起将这些节点移到左右子树上. Code: #include<cstdio> #include<cs ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
- 整体二分&cdq分治 ZOJ 2112 Dynamic Rankings
题目:单点更新查询区间第k大 按照主席树的思想,要主席树套树状数组.即按照每个节点建立主席树,然后利用树状数组的方法来更新维护前缀和.然而,这样的做法在实际中并不能AC,原因即卡空间. 因此我们采用一 ...
- 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)
SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...
随机推荐
- 【iOS】PrefixHeader.pch
还不太理解,暂且记下.
- 腾讯企业邮箱 POP3/SMTP 设置
下午魅族MX2刷完机,原先配置的公司邮箱还要重新配置.有些地方需要改,找到了篇文章,如下: 腾讯企业邮箱支持通过客户端进行邮件管理.POP3/SMTP协议收发邮件服务器地址分别如下.接收邮件服务器:p ...
- UE4 代理 BindRaw和BindUObject
代理允许您在C++对象上以通用的但类型安全的方式调用成员函数.通过使用代理,可以将其动态地绑定到任何对象的成员函数上,然后在该对象上调用函数,即时调用者不知道该对象的类型也没关系. 任何时候都应该通过 ...
- c# 控制台console进度条
1 说明 笔者大多数的开发在 Linux 下,多处用到进度条的场景,但又无需用到图形化界面,所以就想着弄个 console 下的进度条显示. 2 步骤 清行显示 //清行处理操作 int curren ...
- ByteBuf
ByteBuf readerIndex ,读索引 writerIndex ,写索引 capacity ,当前容量 maxCapacity ,最大容量,当 writerIndex 写入超过 capaci ...
- 安装MySQL5.7 安装环境:CentOS7 64位 MINI版,
安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...
- ccf 201809-4 再卖菜
这题一开始不知道剪枝这种操作,只会傻傻地dfs. 然后dfs递归写80分超时,非递归写70分超时(纳尼?我一直以为非递归算法在时间上会更优秀一些,为什么会这样?!!) 剪一下枝就都能过了 #inclu ...
- 【win】【qt5打包】【qt程序打包成一个可执行文件(带图标任何win都可以运行哦)】
[前言] 业务需求将qt程序打包成win可执行文件.咱是做linux的,奈何用的麒麟系统,程序运行在win,好嘛,重新在win qtcreator编译后打包呗. [目标] 1.给qt程序添加一个图标. ...
- windbg 使用与技巧
基本知识和常用命令 (1) Windbg下载地址http://msdn.microsoft.com/en-us/windows/hardware/gg463009.aspx 安装完后执行w ...
- vSphere Web Client 监控 esxi 主机硬件状态
开启插件能对 vcenter 管理的 esxi 主机的硬件状态进行监控. 以下操作均在 vcenter 主机上操作. 0x00 修改配置 文档中关于启用脚本插件支持的说明: Enabling Scri ...