Dynamic Rankings

带修改的区间第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大的更多相关文章

  1. zoj 2112 Dynamic Rankings(主席树&amp;动态第k大)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  2. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

  3. ZOJ 2112 Dynamic Rankings(树状数组+主席树)

    题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...

  4. 线段树专题2-(加强版线段树-可持续化线段树)主席树 orz! ------用于解决区间第k大的问题----xdoj-1216

    poj-2104(区间第K大问题) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  5. 洛谷P2617 Dynamic Rankings 主席树 单点修改 区间查询第 K 大

    我们将线段树套在树状数组上,查询前预处理出所有要一起移动的节点编号,并在查询过程中一起将这些节点移到左右子树上. Code: #include<cstdio> #include<cs ...

  6. 主席树[可持久化线段树](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 ...

  7. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  8. 整体二分&cdq分治 ZOJ 2112 Dynamic Rankings

    题目:单点更新查询区间第k大 按照主席树的思想,要主席树套树状数组.即按照每个节点建立主席树,然后利用树状数组的方法来更新维护前缀和.然而,这样的做法在实际中并不能AC,原因即卡空间. 因此我们采用一 ...

  9. 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)

    SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...

随机推荐

  1. UE4 本地化不起作用 SetCurrentCulture

    UE4 本地化 FInternationalization::Get ().SetCurrentCulture ( TEXT ( "en" ) ) FInternationaliz ...

  2. spark shuffle的写操作之准备工作

    前言 在前三篇文章中,spark 源码分析之十九 -- DAG的生成和Stage的划分 剖析了DAG的构建和Stage的划分,spark 源码分析之二十 -- Stage的提交 剖析了TaskSet任 ...

  3. 启动Eclipse提示找不到虚拟机

    由于硬盘坏了,把所有东西都清光了,今天重新安装Eclipse,出现了一点小插曲 安装的时候出现了这个画面,以前安装也是照着[软件安装管家]的发布装的,幸好还懂得几个英文单词,看了一下提示信息,直译:[ ...

  4. Docker启用TLS进行安全配置

    之前开启了docker的2375 Remote API,接到公司安全部门的要求,需要启用授权,翻了下官方文档 Protect the Docker daemon socket 启用TLS 在docke ...

  5. react学习(二)--元素渲染

    元素用来描述你在屏幕上看到的内容: const element = <h1>Hello, world</h1>; 与浏览器的 DOM 元素不同,React 当中的元素事实上是普 ...

  6. Java——类型信息

    1.Class对象 Class对象是一个特殊的对象,它包含了与类有关的信息.Class对象就是用来创建类的所有常规对象的. 类是程序的一部分,每个类都有一个Class对象,每当编写并且编译一个新类,就 ...

  7. spring架构解析--入门一

    Spring 框架中的核心组件只有三个:Core.Context 和 Beans.它们构建起了整个 Spring 的骨骼架构.简单理解: spring core是工具,context是环境,而bean ...

  8. 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)

    Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...

  9. EFCore + MySql codeFirst 迁移 Migration出现的问题

    第二次使用Migration update-database的时候出现以下错误: System.NotImplementedException: The method or operation is ...

  10. BFS-迷宫问题

    问题描述 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...