ZOJ 2112 Dynamic Rankings

61 篇文章 0 订阅
28 篇文章 0 订阅
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


(adviser)

Site: http://zhuzeyuan.hp.infoseek.co.jp/index.files/our_contest_20040619.htm

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

主席树+树状数组+离散化~

带修改区间k小值。

首先用unique离散化一下。

因为主席树记录所有历史版本,所以每次修改一个值时最多可能要n的时间,显然会T。那么我们就要让每次修改的复杂度降下来。

具体的方法是用树状数组记录。树状数组每次修改的复杂度是logn,那么我们在树状数组的每个节点上记录一棵树,每次logn修改,查询时在需要的节点上的树(同构)同时向下查。这样就能保证复杂度,但同时查询的复杂度也会*logn。

每次查询的时候,我们用两个数组q1[]和q2[]分别记录目前在查的节点的编号,其中q1[]相当于主席树中w[l-1]的k值,q2[]相当于主席树中w[r]的k值,最后把所有sum{q2[]-q1[]}即为答案。这个查询过程体现在函数cal()中。

注意:

1.数组要开得足够大;

2.区分l和r!


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int t,n,m,num[50001],rank[100001],root[121001],tot,cnt,q1[121001],q2[121001],tot1,tot2;
char s[2];

struct node{
	int l,r,k,id;
}a[10001];

struct node1{
	int l,r,w;
}c[2000001];

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

void add(int &k,int l,int r,int v)
{
	c[++cnt]=c[k];k=cnt;
	c[k].w++;
	if(l==r) return;
	int mid=l+r>>1;
	if(v<=mid) add(c[k].l,l,mid,v);
	else add(c[k].r,mid+1,r,v);
}

void chan(int &k,int l,int r,int u,int v)
{
	if(!k)
	{
		c[++cnt]=c[k];k=cnt;
	}
	c[k].w+=v;
	if(l==r) return;
	int mid=l+r>>1;
	if(u<=mid) chan(c[k].l,l,mid,u,v);
	else chan(c[k].r,mid+1,r,u,v);
}

void inse(int u,int num,int v)
{
	int ran=lower_bound(rank+1,rank+tot+1,num)-rank;
	for(;u<=n;u+=u&-u) chan(root[u],1,tot,ran,v);
}

int cal(int l,int r,int k)
{
	if(l==r) return l;
	int now=0,mid=l+r>>1;
	for(int i=1;i<=tot1;i++) now-=c[c[q1[i]].l].w;
	for(int i=1;i<=tot2;i++) now+=c[c[q2[i]].l].w;
	for(int i=1;i<=tot1;i++) q1[i]=k<=now ? c[q1[i]].l:c[q1[i]].r;
	for(int i=1;i<=tot2;i++) q2[i]=k<=now ? c[q2[i]].l:c[q2[i]].r;
	if(k<=now) return cal(l,mid,k);
	return cal(mid+1,r,k-now);
}

int main()
{
	t=read();
	while(t--)
	{
		n=tot=read();m=read();cnt=0;
		for(int i=1;i<=n;i++) rank[i]=num[i]=read();
		for(int i=1;i<=m;i++)
		{
			scanf("%s",s);
			if(s[0]=='Q')
			{
				a[i].id=0;a[i].l=read();a[i].r=read();a[i].k=read();
			}
			else
			{
				a[i].id=1;a[i].l=read();rank[++tot]=a[i].k=read();
			}
		}
		sort(rank+1,rank+tot+1);
		tot=unique(rank+1,rank+tot+1)-rank-1;
		memset(root,0,sizeof(root));
		for(int i=1;i<=n;i++)
		  root[i+n]=root[i+n-1],add(root[i+n],1,tot,lower_bound(rank+1,rank+1+tot,num[i])-rank);
		for(int i=1;i<=m;i++)
		{
			if(a[i].id)
			{
				inse(a[i].l,num[a[i].l],-1);
				inse(a[i].l,a[i].k,1);
				num[a[i].l]=a[i].k;
			}
			else
			{
				tot1=tot2=0;
				q1[++tot1]=root[a[i].l==1 ? 0:a[i].l-1+n];
				q2[++tot2]=root[a[i].r+n];
				for(int j=a[i].l-1;j;j-=j&-j) q1[++tot1]=root[j];
				for(int j=a[i].r;j;j-=j&-j) q2[++tot2]=root[j];
				printf("%d\n",rank[cal(1,tot,a[i].k)]);
			}
		}
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值