ZOJ 2112 Dynamic Rankings(线段树套treap求动态第K大)

题目链接:点击打开链接

思路:我们都知道, treap可以维护整个区间内的数的大小关系, 那么我们在线段树的每个节点上建一棵treap, 那么对于一个n个数的每一个数, 他都会经历logn个结点,所以总的结点数是n * logn。 然后二分答案ans, 询问区间内<=ans的个数来判断二分方向就行了。

一个防止超内存的黑科技:开一个数组做内存池。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 5e4 + 10;
int T,n,m;
struct node {
    node *ch[2];
    int r, v, s;
    int cmp(int x) const {
        if(x == v) return -1;
        return x < v ? 0 : 1;
    }
    void maintain() {
        s = 1;
        if(ch[0] != NULL) s += ch[0]->s;
        if(ch[1] != NULL) s += ch[1]->s;
    }
} *root[maxn<<2], table[900000], *top;
node *newnode(int x) {
    top -> v = x ;
    top -> s = 1 ;
    top -> r = rand() ;
    top -> ch[0] = top -> ch[1] = NULL ;
    return top ++ ;
}
void rotate(node* &o, int d) {
    node* k = o->ch[d^1];  //旋转, 使得优先级满足堆的意义
    o->ch[d^1] = k->ch[d];
    k->ch[d] = o;
    o->maintain();
    k->maintain();
    o = k;
}
void insert(node* &o, int x) {
    if(o == NULL) o = newnode(x);
    else {
        int d = (x < o->v ? 0 : 1);
        insert(o->ch[d], x);
        if(o->ch[d]->r > o->r) rotate(o, d^1);
    }
    o->maintain();
}
void remove(node* &o, int x) {
    if(o == NULL) return ;
    int d = o->cmp(x);
    if(d == -1) {
        node* u = o;
        if(o->ch[0] != NULL && o->ch[1] != NULL) {
            int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);
            rotate(o, d2);
            remove(o->ch[d2], x);
        }
        else {
            if(o->ch[0] == NULL) o = o->ch[1];
            else o = o->ch[0];
        }
    }
    else remove(o->ch[d], x);
    if(o != NULL) o->maintain();
}
int kth(node* o, int k) {
    if(o == NULL || k <= 0 || k > o->s) return 0;
    int s = (o->ch[0] != NULL ? o->ch[0]->s : 0);

    if(k == s+1) return o->v;
    else if(k <= s) return kth(o->ch[0], k);
    else return kth(o->ch[1], k-s-1);
}

void build(int l, int r, int o, int pos, int v) {
    int m = (l + r) >> 1;
    insert(root[o], v);
    if(l == r) return ;
    if(pos <= m) build(l, m, o<<1, pos, v);
    else build(m+1, r, o<<1|1, pos, v);
}
int getsum(node* root, int k) {
    if(root == NULL) return 0;
    int ans = 0;
    int d = k < root -> v ? 0 : 1;
    if(d == 0) ans += getsum(root->ch[d], k);
    else {
        ans += 1 + (root -> ch[0] != NULL ? root->ch[0]->s : 0);
        ans += getsum(root->ch[1], k);
    }
    return ans;
}
void update(int l, int r, int o, int pos, int old, int v) {
    int m = (l + r) >> 1;
    remove(root[o], old);
    insert(root[o], v);
    if(l == r) return ;
    if(pos <= m) update(l, m, o<<1, pos, old, v);
    else update(m+1, r, o<<1|1, pos, old, v);
}
int query(int L, int R, int l, int r, int o, int x) {
    int m = (l + r) >> 1;
    if(L <= l && r <= R) return getsum(root[o], x);
    int ans = 0;
    if(L <= m) ans += query(L, R, l, m, o<<1, x);
    if(m < R) ans += query(L, R, m+1, r, o<<1|1, x);
    return ans;
}
int erfen(int L, int R, int k) {
    int l = 1, r = INF, mid;
    while(r > l) {
        mid = (l + r) >> 1;
        int cur = query(L, R, 1, n, 1, mid);
        if(cur >= k) r = mid;
        else l = mid + 1;
    }
    return l;
}
int a[maxn], pos, v, l, r;
char op[2];
int main() {
    scanf("%d",&T);
    while(T--) {
        top = table;
        memset(root, 0, sizeof(root));
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++) {
            scanf("%d",&a[i]);
            build(1, n, 1, i, a[i]);
        }
        while(m--) {
            scanf("%s",op);
            if(op[0] == 'C') {
                scanf("%d%d",&pos, &v);
                update(1, n, 1, pos, a[pos], v);
                a[pos] = v;
            }
            else {
                scanf("%d%d%d",&l,&r,&v);
                int ans = erfen(l, r, v);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}
时间: 2024-08-28 23:55:03

ZOJ 2112 Dynamic Rankings(线段树套treap求动态第K大)的相关文章

ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大

Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树状数组的方式去修改,并且修改那些地方.查询的时候就是查询原主席树+树状数组的值. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r&quo

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

Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They

ZOJ 2112 Dynamic Rankings(主席树の动态kth)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. T

BZOJ 3196 二逼平衡树 树套树(线段树套Treap)

题目大意: 写一种数据结构,他可以: 1.查询k在区间内的排名. 2.查询区间内排名为k的值 3.修改某一个值. 4.求k在区间内的前驱. 5.求k在区间内的后继. 思路:本来以为有什么只有神犇才知道的神一般的数据结构来维护它,问了别人之后,发现只是树套树.据说怎么套都行.我见识鄙陋,就只能线段树套Treap了. 这也是第一次写树套树,还1A了,有点开心. 写树套树,一定要确定自己对这两个树及其熟练,加上少量精细的思考,就可以完成树套树.(我只是弱渣,求神犇别D) 具体实现:第一层是线段树,第二

zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap

Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 Description The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query l

【bzoj3196】 Tyvj 1730 二逼平衡树 线段树套Treap

题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)5.查询k在区间内的后继(后继定义为大于x,且最小的数) 输入 第一行两个数 n,m 表示长度为n的有序序列和m个操作第二行有n个数,表示有序序列下面有m行,opt表示操作标号若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名若opt=2 则为操

bzoj 3196 &amp;&amp; luogu 3380 JoyOI 1730 二逼平衡树 (线段树套Treap)

链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 题面; 3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6372  Solved: 2406[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为

POJ2761---Feed the dogs (Treap求区间第k大)

题意 就是求区间第k大,区间 不互相包含. 尝试用treap解决一下 第k大的问题. 1 #include <set> 2 #include <map> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <cstdio> 8 #include <string> 9 #include <

ZOJ 2112 Dynamic Rankings(主席树套树状数组+静态主席树)

题意:给定一个区间,求这个区间第k大的数,支持单点修改. 思路:主席树真是个神奇的东西.........速度很快但是也有一个问题就是占用内存的很大,一般来说支持单点修改的主席树套树状数组空间复杂度为O(n*logn*logn), 如果查询较少的话,可以初始的时候用一颗静态主席树,这样空间复杂度可以降为O(n*logn+q*logn*logn),勉强可以过zoj这道题. 这道题看了好久好久才懂...网上题解一般就几句话.......下面是自己的一些理解. 首先静态主席树这个东西其实比较好懂,就是对