思路是用主席树维护每个版本,为什么没输出?
#include<bits/stdc++.h>
#define ll long long
const int MAX=1e5+10;
using namespace std;
inline int read() {
int fh = 1, res = 0; char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') fh = -1;
for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ '0');
res = res * fh;
return res;
}
inline void write(ll x) {
if(x<0){putchar('-');x=-x;}
if(x>9) write(x/10);
putchar(x%10+'0');
}
struct node{int lc,rc;char w;}a[MAX<<5];
int tot,root[MAX],js;
int build(int l,int r)
{
int k=++tot;
int mid=(l+r)>>1;if(l==r) return k;
a[k].lc=build(l,mid);a[k].rc=build(mid+1,r);
return k;
}
int change(int l,int r,int x,char y,int k)
{
a[++tot]=a[k];k=tot;
if(l==r){a[k].w=y;return k;}
int mid=(l+r)>>1;
if(x<=mid) a[k].lc=change(l,mid,x,y,a[k].lc);
else a[k].rc=change(mid+1,r,x,y,a[k].rc);
return k;
}
char ask(int l,int r,int x,int k)
{
// cout<<l<<" "<<r<<" "<<k<<" "<<a[k].w<<endl;
if(l==r) return a[k].w;
int mid=(l+r)>>1;
if(x<=mid) return ask(l,mid,x,a[k].lc);
else return ask(mid+1,r,x,a[k].rc);
}
int main()
{
int n=read();
// build(1,n);
for(int i=1,cx=0;i+cx<=n;i++)
{
char opt;scanf(" %c",&opt);root[i]=root[i-1];
if(opt=='T') {char x;scanf(" %c",&opt);root[i]=change(1,n,++js,x,root[i-1]);}
if(opt=='U') {int x=read();root[i]=root[i-x];}
if(opt=='Q') {int x=read();printf("%c\n",ask(1,n,x,root[i-1]));i--;cx++;}
}
return 0;
}