WA on #1,3,6 不知道哪里有问题,求各位大佬帮忙看一下
提交记录
#include<bits/stdc++.h>
using namespace std;
const int maxm=1e6+7;
int N=2e5+7;
//注意在涉及到主席树的更新操作时最好使用这个N
//他是值域,与下面那个表示元素个数的n不同
int root[maxm*30];
int a[maxm];
struct Persistent_Segment_Tree{
int lch[maxm*30],rch[maxm*30],tot;
char lazy[maxm*30];
int len[maxm*30];
//这里的lazy为直接赋值的lazy
void init(){
tot=0;
}
inline void Copy(int from,int to){
lch[to]=lch[from];
rch[to]=rch[from];
lazy[to]=lazy[from];
len[to]=len[from]+1;//字母长度多了一个
}
void update(int &root,int old,int cl,int cr,int loc,char x){
root=++tot;
Copy(old,root);
if(cl==cr){
lazy[root]=x;
return;
}
int mid=(cl+cr)>>1;
if(loc<=mid) update(lch[root],lch[old],cl,mid,loc,x);
else update(rch[root],rch[old],mid+1,cr,loc,x);
}
char query(int root,int cl,int cr,int loc){
//cout<<root<<"---"<<cl<<" "<<cr<<endl;
if(cl==cr) {return lazy[root];}
int mid=(cl+cr)>>1;
if(loc<=mid) return query(lch[root],cl,mid,loc);
else return query(rch[root],mid+1,cr,loc);
}
};
Persistent_Segment_Tree seg;
int main(){
int n,num;
char op;
cin>>n;
root[0]=1;
seg.init();
int version = 0;//现在的版本号
while(n--){
cin>>op;
if(op=='T'){
char c;
cin>>c;
int loc;
loc = seg.len[root[version]]+1;
seg.update(root[version+1],root[version],1,N,loc,c);
//执行键入操作
version = version+1;
}
else if(op=='U'){
//撤销最后num次操作
cin>>num;
root[version+1] = root[version-num];
if(version < num) root[version+1] = root[0];
version = version+1;
}
else{
//询问当前文章的第x个字母并进行输出
cin>>num;
cout<<(char)seg.query(root[version],1,N,num)<<'\n';
}
}
}
/*
5 5
1 2 3 4 5
0 1 1 5
1 2 1
0 1 1 5
2 2 1
*/