首页 > 代码库 > zoj 2112

zoj 2112

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N =50000+10;
const int maxn=N*18;
int tot;
int T[N],A[N],t[N],s[N];
int lson[maxn],rson[maxn],c[maxn];
int n,q,m;
void init()
{
sort(t+1,t+1+n);
m=unique(t+1,t+1+n)-t-1;
}
void build(int l,int r)
{
int rt=tot++;
c[rt]=0;
if (l>=r) return;
int mid=(l+r)>>1;
lson[rt]=build(l,mid);
rson[rt]=build(mid+1,r);
return rt;
}
int inserts(int rt,int loc,int val)
{
int newrt=tot++,tmp=newrt;
int l=1,r=m;
c[newrt]=c[rt]+val;
while (l<r)
{
int mid=(l+r)>>1;
if (loc<=mid){
lson[newrt]=tot++;
rson[newrt]=rson[rt];
rt=lson[rt];
newrt=lson[newrt];
r=mid;
}
else{
rson[newrt]=tot++;
lson[newrt]=lson[rt];
rt=rson[rt];
newrt=rson[newrt];
l=mid+1;
}
c[newrt]=c[rt]+val;
}
return tmp;
}
void modify(int loc,int p,int val)
{
while (loc<=n)
{
s[loc]=inserts(s[loc],loc,val);
loc+=loc&(-loc);
}
}
int main()
{
char op[5];
int t,a,b,k;
scanf("%d",&t);
while (t--)
{
tot=0;
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++) scanf("%d",&A[i]),t[i]=A[i];
init();
T[0]=build(0,m-1);
for (int i=0;i<=n;i++) s[i]=0;
for (int i=1;i<=n;i++){
int pos=lower_bound(t+1,t+m+1,A[i])-t;
T[i]=inserts(T[i-1],pos,1);
}
while (q--)
{
scanf("%s",op);
if (op[0]==‘Q‘){
scanf("%d%d%d",&a,&b,&k);
}
else{
scanf("%d%d",&a,&b);
int pos;
pos=lower_bound(t+1,t+1+n,A[a])-t;
modify(a,pos,-1);
pos=lower_bound(t+1,t+1+n,A[b])-t;
modify(a,pos,1);
A[a]=b;
}
}
}
}