ZOJ 2112 动态区间第k大 树状数组套主席树

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. 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

题目大意:静态区间第k大+修改操作。

思路:动态区间第k大,这里用的是树套树的方法。题目卡内存,要先建一棵静态主席树。

#include<bits/stdc++.h>
using namespace std;

const int maxn=6e4+5;
const int M=2400005;

struct node
{
    int lc,rc,siz;
}tree[M];//节点

struct qury
{
    int a,b,c;
}q[10010];   //离线操作

char op[10];//指令
int n,m,ansx,ansy,tot,len;//n个数 m个查询 tot个节点 len为离散化后的长度
                          //ansx ansy 用于记录 查询操作 所需要的节点个数
int srt[maxn],rt[maxn],a[maxn],b[maxn],qx[maxn],qy[maxn];//记录静态主席树根节点编号 新增节点编号 原序列 离散化后的序列
                         // qx qy 用于记录 查询操作 所需要的节点

inline int lowbit(int x)
{
    return x&(-x);
}

inline void update(int &root,int pre,int l,int r,int val,int num)
{
    root=++tot;
	tree[root]=tree[pre];
	tree[root].siz+=num;
	if(l==r)
        return;
    int mid=(l+r)>>1;
	if(val<=mid)
        update(tree[root].lc,tree[pre].lc,l,mid,val,num);
	else
        update(tree[root].rc,tree[pre].rc,mid+1,r,val,num);
}

inline void add(int pos,int v)
{
	int val=lower_bound(b+1,b+len+1,a[pos])-b;
	for(int i=pos;i<=n;i+=lowbit(i))
		update(rt[i],rt[i],1,len,val,v);
}

inline int query(int x,int y,int l,int r,int k)
{
	if(l==r) return l;
	int mid=(l+r)>>1;
    int sum=tree[tree[y].lc].siz-tree[tree[x].lc].siz;
	for(int i=1;i<=ansx;++i)
        sum-=tree[tree[qx[i]].lc].siz;
	for(int i=1;i<=ansy;++i)
        sum+=tree[tree[qy[i]].lc].siz;
	if(k<=sum){
		for(int i=1;i<=ansx;++i)
            qx[i]=tree[qx[i]].lc;
		for(int i=1;i<=ansy;++i)
            qy[i]=tree[qy[i]].lc;
		return query(tree[x].lc,tree[y].lc,l,mid,k);
	}
	else{
		for(int i=1;i<=ansx;++i)
            qx[i]=tree[qx[i]].rc;
		for(int i=1;i<=ansy;++i)
            qy[i]=tree[qy[i]].rc;
		return query(tree[x].rc,tree[y].rc,mid+1,r,k-sum);
	}
}

int build(int l,int r)
{
    int root=++tot;
    if(l==r)
        return root;
    int mid=(l+r)>>1;
    tree[root].lc=build(l,mid);
    tree[root].rc=build(mid+1,r);
    return root;
}

inline void prework()//初始化操作 包括离散化等
{
    tot=len=rt[0]=0;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
        b[++len]=a[i];
    }
    for(int i=1;i<=m;++i)
    {
        scanf("%s",op);
        scanf("%d %d",&q[i].a,&q[i].b);
        if(op[0]=='Q')
            scanf("%d",&q[i].c);
        else if(op[0]=='C')
            q[i].c=-1,b[++len]=q[i].b;
    }
    sort(b+1,b+1+len);
    len=unique(b+1,b+1+len)-b-1;
    srt[0]=build(1,len);
    for(int i=1;i<=n;++i)
    {
        int val=lower_bound(b+1,b+len+1,a[i])-b;
        update(srt[i],srt[i-1],1,len,val,1);
    }
    for(int i=1;i<=n;i++)
        rt[i]=srt[0];
}

inline void mainwork()//查询操作
{
    for(int i=1;i<=m;++i)
    {
        if(q[i].c>=0)
        {
            ansx=0,ansy=0;
            for(int j=q[i].b;j;j-=lowbit(j))
                qy[++ansy]=rt[j];
            for(int j=q[i].a-1;j;j-=lowbit(j))
                qx[++ansx]=rt[j];
            printf("%d\n",b[query(srt[q[i].a-1],srt[q[i].b],1,len,q[i].c)]);
        }
        else
        {
            add(q[i].a,-1);
            a[q[i].a]=q[i].b;
            add(q[i].a,1);
        }
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--)
    {
        prework();
        mainwork();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值