首页 > 代码库 > [bzoj1901][zoj2112][Dynamic Rankings]

[bzoj1901][zoj2112][Dynamic Rankings]

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

25 33 2 1 4 7Q 1 4 3C 2 6Q 2 5 35 33 2 1 4 7Q 1 4 3C 2 6Q 2 5 3

Sample Output

3636

Solution

算是整体二分的经典题目吧

大概是带修改的求区间第k小

这里用离线做法

首先,在数据范围较大的情况下要对于所有出现在输入中的区间数字进行离散化

之后,存下所有的询问,将修改视为2操作,赋值操作视为1操作,3操作则是询问

将离散化后的数字区间拿来做二分

大致思路是这样的:

1.确定答案范围[L,R],mid=L+R>>1;
2.算出答案在[L,mid]内的操作对答案在[mid+1,R]内的操作的贡献;
3.将答案在[L,mid]内的操作放入队列Q1,solve(Q1,L,mid)
将答案在[mid+1,R]内的操作放入Q2,solve(Q2,mid+1,R)

由于询问和操作是按时间顺序处理的,为了满足二分性质还需对询问和操作的区间进行排序,这里用到一点树状数组求逆序对的思想,我们用树状数组维护题目中虚拟的那个区间中的n个数的区间,用树状数组统计区间中询问和操作的贡献,用差分进行区间修改。

之后对每个询问和操作进行分治,分到两个区间中分别处理

每当遇到当前区间已经缩小成为点时,求记录答案

最后按要求输出即可

#include<map>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int M=300000;inline int read(){  int x=0,c=getchar(),f=1;  for(;c<48||c>57;c=getchar())    if(!(c^45))      f=-1;  for(;c>47&&c<58;c=getchar())    x=(x<<1)+(x<<3)+c-48;  return x*f;}vector<int>v;map<int,int>h;int Qcnt,Pcnt,a[M],ans[M],c[M],n,tmp[M];struct Que{  int x,y,typ,cur,kth,pos;}q[M],q1[M],q2[M];inline void add(int x,int d){  for(;x<=n;x+=x&(-x))    c[x]+=d;}inline int sum(int x){  int res=0;  for(;x;x-=x&(-x))    res+=c[x];  return res;}void divide(int hd,int tl,int l,int r){  if(hd>tl)    return;  if(l==r){    for(int i=hd;i<=tl;i++)      if(q[i].typ==3)    ans[q[i].pos]=l;    return;  }  int mid=l+r>>1;  for(int i=hd;i<=tl;i++){    if(q[i].typ==3)      tmp[i]=sum(q[i].y)-sum(q[i].x-1);    else      if(q[i].typ==2&&q[i].y<=mid)    add(q[i].x,-1);      else    if(q[i].typ==1&&q[i].y<=mid)      add(q[i].x,1);  }  for(int i=hd;i<=tl;i++){    if(q[i].typ==2&&q[i].y<=mid)    add(q[i].x,1);    else      if(q[i].typ==1&&q[i].y<=mid)    add(q[i].x,-1);  }  int l1=0,l2=0;  for(int i=hd;i<=tl;i++){    if(q[i].typ==3){      if(q[i].cur+tmp[i]>=q[i].kth)    q1[++l1]=q[i];      else{    q[i].cur+=tmp[i];    q2[++l2]=q[i];      }    }    else{      if(q[i].y<=mid)    q1[++l1]=q[i];      else    q2[++l2]=q[i];    }  }  for(int i=1;i<=l1;i++)    q[hd+i-1]=q1[i];  for(int i=1;i<=l2;i++)    q[hd+l1+i-1]=q2[i];  divide(hd,hd+l1-1,l,mid);  divide(hd+l1,tl,mid+1,r);}int main(){  int m,T;  T=read();  while(T--){    Qcnt=Pcnt=0;    v.clear();    h.clear();    memset(a,0,sizeof(a));    memset(c,0,sizeof(c));    memset(tmp,0,sizeof(tmp));    n=read(),m=read();    for(int i=1;i<=n;i++){      a[i]=read();      q[++Qcnt].x=i;      q[Qcnt].y=a[i];      q[Qcnt].typ=1;      v.push_back(a[i]);    }    for(int i=1;i<=m;i++){      char sign[4];      int x,y,z;      scanf("%s",sign);      if(sign[0]==Q){    x=read(),y=read(),z=read();    q[++Qcnt].x=x;    q[Qcnt].y=y;    q[Qcnt].kth=z;    q[Qcnt].typ=3;    q[Qcnt].cur=0;    q[Qcnt].pos=++Pcnt;      }      else{    x=read(),y=read();    q[++Qcnt].x=x;    q[Qcnt].y=a[x];    q[Qcnt].typ=2;    q[++Qcnt].x=x;    q[Qcnt].y=y;    q[Qcnt].typ=1;    a[x]=y;    v.push_back(y);      }    }    sort(v.begin(),v.end());    v.erase(unique(v.begin(),v.end()),v.end());    for(int i=0;i<v.size();i++)      h[v[i]]=i+1;    for(int i=1;i<=Qcnt;i++)      if(q[i].typ<=2)    q[i].y=h[q[i].y];    divide(1,Qcnt,1,v.size());    for(int i=1;i<=Pcnt;i++)      printf("%d\n",v[ans[i]-1]);  }  return 0;}

 

[bzoj1901][zoj2112][Dynamic Rankings]