分析:
这道题有修改操作,所以我们需要在操作处理上做一点手脚:
修改操作,我们就变成两个操作:删除和添加
scanf("%d%d",&x,&y);
tot++;
q[tot].x=a[x];q[tot].y=-1;q[tot].type=1;q[tot].id=x; //删除 y=-1
tot++;
a[x]=y; //维护序列状态
q[tot].x=a[x];q[tot].y=1;q[tot].type=1;q[tot].id=x; //添加 y=1
//x:值 id:数组中的位置
我们需要一个辅助数组a,记录序列状态
一旦出现修改操作,我们就需要把之前的数字删除
然后添加新的数字
注意:一定要维护a数组
主程序和poj2104一样
//这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int INF=1e9+7;
const int N=50010;
const int MM=10010;
struct node{
int x,y,k,type,id;
};
node q[N+MM<<1],q1[N+MM<<1],q2[N+MM<<1];
int n,m,t[N],tot,totx,ans[N],a[N];
void add(int x,int z) {for (int i=x;i<=n;i+=(i&(-i))) t[i]+=z;}
int ask(int x) {int ans=0;for (int i=x;i>0;i-=(i&(-i))) ans+=t[i];return ans;}
void solve(int ql,int qr,int L,int R)
{
if (ql>qr) return;
if (L==R)
{
for (int i=ql;i<=qr;i++)
if (q[i].type==2) ans[q[i].id]=L; //统计答案
return;
}
int M=(L+R)>>1; //此次的判断标准
int t1=0,t2=0;
for (int i=ql;i<=qr;i++)
{
if (q[i].type==1)
{
if (q[i].x<=M){ //这种情况下x会给排名产生贡献
add(q[i].id,q[i].y);
q1[t1++]=q[i];
}
else q2[t2++]=q[i]; //x不会给排名产生贡献
}
else
{
int tt=ask(q[i].y)-ask(q[i].x-1); //当前的区间中有多少小于M的数值
if (tt>=q[i].k) q1[t1++]=q[i];
else
{
q[i].k-=tt;
q2[t2++]=q[i];
}
}
}
for (int i=0;i<t1;i++) q[ql+i]=q1[i]; //把q1,q2的信息复制到q中
for (int i=0;i<t2;i++) q[ql+t1+i]=q2[i];
for (int i=0;i<t1;i++)
if (q1[i].type==1) add(q1[i].id,-q1[i].y); //把之前的标记清除
solve(ql,ql+t1-1,L,M);
solve(ql+t1,qr,M+1,R);
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
memset(t,0,sizeof(t));
memset(ans,0,sizeof(ans));
totx=0; tot=0;
scanf("%d%d",&n,&m);
int x,y,k;
char s[5];
for (int i=1;i<=n;i++)
{
scanf("%d",&x); a[i]=x;
tot++;
q[tot].x=x;q[tot].y=1;q[tot].type=1;q[tot].id=i;
//x:值 id:数组中的位置
}
for (int i=1;i<=m;i++)
{
scanf("%s",s);
if (s[0]=='Q'){
scanf("%d%d%d",&x,&y,&k);
tot++;
q[tot].x=x;q[tot].y=y;q[tot].k=k;q[tot].type=2;q[tot].id=++totx;
}
else{
scanf("%d%d",&x,&y);
tot++;
q[tot].x=a[x];q[tot].y=-1;q[tot].type=1;q[tot].id=x;
tot++;
a[x]=y;
q[tot].x=a[x];q[tot].y=1;q[tot].type=1;q[tot].id=x;
//x:值 id:数组中的位置
}
}
solve(1,tot,0,INF);
for (int i=1;i<=totx;i++) printf("%d\n",ans[i]);
}
return 0;
}