蒟蒻萌新,求助主席树!!!
查看原帖
蒟蒻萌新,求助主席树!!!
658786
STUDENT00楼主2022/11/20 17:15

萌新刚学主席树0.5天,求助!!!

#include<bits/stdc++.h>
#define N 100010
using namespace std;
int t,cnt,rt[N];
struct hjt{
	int l,r;
	char data;
	int size;
} tree[N<<5];
void update(int &rt,int fa,int l,int r,char c){
	rt=++cnt;
	tree[rt].l=tree[fa].l;
	tree[rt].r=tree[fa].r;
	tree[rt].data=tree[fa].data;
	tree[rt].size=tree[fa].size;
	if(l==r){
		tree[rt].data=c;
		tree[rt].size=1;
		return;
	}
	int mid=l+r>>1;
	if(tree[tree[rt].l].size==mid-l+1) update(tree[rt].r,rt,mid+1,r,c);
	else update(tree[rt].l,rt,l,mid,c);
	tree[rt].size=tree[tree[rt].l].size+tree[tree[rt].r].size;
}
char query(int rt,int l,int r,int k){
	if(l==r) return tree[rt].data;
	int mid=l+r>>1;
	if(k<=tree[tree[rt].l].size) return query(tree[rt].l,l,mid,k);
	else return query(tree[rt].r,mid+1,r,k-tree[tree[rt].l].size);
}
int main(){
	scanf("%d",&t);
	for(int i=1;i<=t;i++){
		char c[1];
		scanf("%s",c);
		if(c[0]=='T'){
			char x[1];
			scanf("%s",x);
			update(rt[++cnt],rt[cnt-1],1,t,x[0]);
		}else if(c[0]=='U'){
			int x;
			scanf("%d",&x);
			rt[++cnt]=rt[cnt-x-1];
		}else{
			int x;
			scanf("%d",&x);
			putchar(query(rt[cnt],1,t,x));
			putchar(10);
		}
	}
	return 0;
}
2022/11/20 17:15
加载中...