线段树套treap(ZOJ2112)

来源:互联网 发布:免费数据恢复精灵 编辑:程序博客网 时间:2024/06/01 20:11

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 have developed a more powerful system such that for N numbers a[1], a[2], ..., a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.

Your task is to write a program for this computer, which

- Reads N numbers from the input (1 <= N <= 50,000)

- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.


Input

The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.

The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format

Q i j k or
C i t

It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.

There're NO breakline between two continuous test cases.


Output

For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])

There're NO breakline between two continuous test cases.


Sample Input

2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3


Sample Output

3
6
3
6


原来用主席树套树状数组写过

线段树套treap,每个线段树节点时是棵treap,查询的时候二分,然后查询比他小的有多少个

treap数组开小了,WA了半天...

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;#define lson lc,L,M#define rson rc,M+1,Rconst int maxn=50010;const int INF=1000000000;int N,M;int a[maxn];int tot;struct Node{    int ch[2];    int r;//优先级    int v;//值    int s;    int cnt;//自身重复次数    void init(int val){v=val;ch[0]=ch[1]=0;s=cnt=1;r=rand();}    int cmp(int x)const    {        if(x==v)return -1;        return x<v?0:1;    }}tree[1000000];void maintain(int x){    tree[x].s=tree[x].cnt;    tree[x].s+=tree[tree[x].ch[0]].s+tree[tree[x].ch[1]].s;}void rotate(int &o,int d){    int k=tree[o].ch[d^1];    tree[o].ch[d^1]=tree[k].ch[d];    tree[k].ch[d]=o;    maintain(o);    maintain(k);    o=k;}void insert(int &o,int x){    if(!o)    {        o=++tot;        tree[o].init(x);    }    else    {        if(x==tree[o].v)tree[o].cnt++;        else        {            int d=(x<tree[o].v?0:1);            insert(tree[o].ch[d],x);            if(tree[tree[o].ch[d]].r>tree[o].r)                rotate(o,d^1);        }    }    maintain(o);}void remove(int &o,int x){    if(!o)return;    int d=tree[o].cmp(x);    if(d==-1)    {        int u=o;        if(tree[o].cnt>1)tree[o].cnt--;        else if(tree[o].ch[0]&&tree[o].ch[1])        {            int d2=(tree[tree[o].ch[0]].r>tree[tree[o].ch[1]].r?1:0);            rotate(o,d2);            remove(tree[o].ch[d2],x);        }        else        {            if(!tree[o].ch[0])o=tree[o].ch[1];            else o=tree[o].ch[0];            tree[u]=tree[0];        }    }    else remove(tree[o].ch[d],x);    if(o)maintain(o);}//返回最大值int get_max(int o){    while(tree[o].ch[0])o=tree[o].ch[0];    return tree[o].v;}//返回最小值int get_min(int o){    while(tree[o].ch[1])o=tree[o].ch[1];    return tree[o].v;}//返回val的前驱,如果没有的话返回y//y的初值可赋成0,表示没有前驱int get_pred(int o,int val,int y){    if(!o)return y;    if(tree[o].v<=val)//注意大于等于号        return get_pred(tree[o].ch[1],val,tree[o].v);    else return get_pred(tree[o].ch[0],val,y);}//返回val的后继,如果没有的话返回y//y的初值可赋成0,表示没有后继int get_succ(int o,int val,int y){    if(!o)return y;    if(tree[o].v>=val)return get_succ(tree[o].ch[0],val,tree[o].v);    else return get_succ(tree[o].ch[1],val,y);}//返回第k大的元素的值int get_kth(int o,int k){    if(!o)return 0;    if(k<=tree[tree[o].ch[0]].s)return get_kth(tree[o].ch[0],k);    else if(k>tree[tree[o].ch[0]].s+tree[o].cnt)        return get_kth(tree[o].ch[1],k-tree[tree[o].ch[0]].s-tree[o].cnt);    return tree[o].v;}//返回val的排名int get_rank(int o,int val){    if(!o)return 0;    int lsize=tree[tree[o].ch[0]].s;    if(val<tree[o].v)        return get_rank(tree[o].ch[0],val);    else if(val>tree[o].v)        return get_rank(tree[o].ch[1],val)+lsize+tree[o].cnt;    return lsize+tree[o].cnt;}struct IntervalTree{    int root[maxn<<2];    void build(int o,int l,int r)    {        for(int i=l;i<=r;i++)            insert(root[o],a[i]);        if(l==r)return;        int mid=(l+r)>>1;        build(o<<1,l,mid);        build(o<<1|1,mid+1,r);    }    void update(int o,int l,int r,int pos,int val)    {        remove(root[o],a[pos]);        insert(root[o],val);        if(l==r)return;        int mid=(l+r)>>1;        if(pos<=mid)update(o<<1,l,mid,pos,val);        else update(o<<1|1,mid+1,r,pos,val);    }    int query(int o,int l,int r,int q1,int q2,int val)    {        if(q1<=l&&r<=q2)return get_rank(root[o],val);        int mid=(l+r)>>1;        int ans=0;        if(q1<=mid)ans+=query(o<<1,l,mid,q1,q2,val);        if(q2>mid)ans+=query(o<<1|1,mid+1,r,q1,q2,val);        return ans;    }}tr;void debug(int r){    if(!r)return ;    debug(tree[r].ch[0]);    printf("%d ",tree[r].v);    debug(tree[r].ch[1]);}void init(){    tot=0;    memset(tr.root,0,sizeof(tr.root));}int main(){    int T;    scanf("%d",&T);    char op[5];    int x,y;    while(T--)    {        scanf("%d%d",&N,&M);        init();        for(int i=1;i<=N;i++)scanf("%d",&a[i]);        tr.build(1,1,N);        while(M--)        {            scanf("%s%d%d",op,&x,&y);            if(op[0]=='C')                tr.update(1,1,N,x,y),a[x]=y;            else            {                int k;                scanf("%d",&k);                int l=0,r=INF,ans=0;                while(l<r)                {                    int mid=l+(r-l)/2;                    int cnt=tr.query(1,1,N,x,y,mid);                    if(tr.query(1,1,N,x,y,mid)>=k)ans=mid,r=mid;                    else l=mid+1;                }                printf("%d\n",ans);            }        }    }    return 0;}






0 0
原创粉丝点击