死循环求助
查看原帖
死循环求助
362627
frank15楼主2021/4/29 21:02
#include<iostream>
#include<cstdio>
#include<string>
#define mid ((l+r)>>1)
#define bug puts("-----------");
using namespace std;
const int maxn=2e6+5;
int t,pos,len,tot,rt;
char c[maxn],op[20];
struct t{
	int f,sz;
	int ch[2];
	char val;
}tree[maxn];
void maintain(int x){
	tree[x].sz=tree[tree[x].ch[0]].sz+tree[tree[x].ch[1]].sz+1;
}
bool son(int x){
	return x=tree[tree[x].f].ch[1];
}
int build(int l,int r,int f){
	if(l>r)
		return 0;
	int node=++tot;
	tree[node].f=f;
	tree[node].val=c[mid];
	tree[node].ch[0]=build(l,mid-1,node);
	tree[node].ch[1]=build(mid+1,r,node);
	maintain(node);
	return node;
}
void rotate(int x){
	int y=tree[x].f,z=tree[y].f;
	bool chk1=son(x),chk2=son(y);
	tree[y].ch[chk1]=tree[x].ch[chk1^1];
	if(tree[x].ch[chk1^1])
		tree[tree[x].ch[chk1^1]].f=y;
	tree[x].ch[chk1^1]=y;
	tree[y].f=x;
	tree[x].f=z;
	if(z)
		tree[z].ch[chk2]=x;
	maintain(y);
	maintain(x);
	return ;
}
void splay(int x,int goal){
	while(tree[x].f!=goal){
		bug
		int y=tree[x].f;
		int z=tree[y].f;
		if(z!=goal){
			if(son(x)==son(y))
				rotate(y);
			else
				rotate(x);
		}
		rotate(x);
	}
	
	if(!goal)
		rt=x;
}
int qfind(int k){
	int cur=rt;
//	cout<<tree[rt].sz<<' '<<tree[tree[cur].ch[1]].sz<<' '<<tree[cur].ch[1]<<endl;
//	return 0;
	while(1){
		if(k<=tree[tree[cur].ch[0]].sz)
			cur=tree[cur].ch[0];
		else{
			k-=tree[tree[cur].ch[0]].sz+1;
			if(k<=0)
				return cur;
			cur=tree[cur].ch[1];
		}
	}
}
void qput(int x){
	if(tree[x].ch[0])
		qput(tree[x].ch[0]);
	putchar(tree[x].val);
	if(tree[x].ch[1])
		qput(tree[x].ch[1]);
}
int main(){
	scanf("%d",&t);
	rt=build(1,2,0);
	while(t--){
		scanf("%s",op);
		if(op[0]=='M')
			scanf("%d",&pos);
		if(op[0]=='I'){
			scanf("%d",&len);
			for(int i=1;i<=len;i++){
				c[i]=getchar();
				if(c[i]=='\n'||c[i]=='\r')
					--i;
			}
			bug
			for(int i=1;i<=len;i++)
				cout<<c[i];
			puts("");
			int L=qfind(pos+1);
			int R=qfind(pos+2);
			splay(L,0);
			splay(R,L);
			bug
			tree[R].ch[0]=build(1,len,R);
		}
		if(op[0]=='D'){
			scanf("%d",&len);
			int L=qfind(pos+1);
			int R=qfind(pos+len+2);
			splay(L,0);
			splay(R,L);
			tree[R].ch[0]=0;
		}
		if(op[0]=='G'){
			scanf("%d",&len);
			int L=qfind(pos+1);
			int R=qfind(pos+len+2);
			splay(L,0);
			splay(R,L);
			qput(tree[R].ch[0]);
			puts("");
		}
		if(op[0]=='P') 
			--pos;
        if(op[0]=='N')
			++pos;
	}
	return 0;
}
2021/4/29 21:02
加载中...