10pts求调(本地正确)
查看原帖
10pts求调(本地正确)
788951
TLE_AK楼主2023/3/25 11:54
#include<bits/stdc++.h>
using namespace std;

namespace acac
{
	struct node
	{
		int v;
		int lson,rson;
		int sz;
		int pri;
		int tag;
		int cnt;
	}A[100010];
	int n,m;
	int root,cnt,ans;
	void new_node(int &va,int x)
	{
		cnt++;
		va=cnt;
		A[va].sz=1;
		A[va].v=x;
		A[va].pri=rand();
		A[va].cnt=1;
	}

	void pd(int u)
	{
		if(u&&A[u].tag)
		{
			A[A[u].lson].tag^=1;
			swap(A[A[u].lson].lson,A[A[u].lson].rson);
			A[A[u].rson].tag^=1;
			swap(A[A[u].rson].lson,A[A[u].rson].rson);
			A[u].tag=0;
		}
		return ;
	}
	void pushup(int x)
	{
		A[x].sz=A[A[x].lson].sz+A[A[x].rson].sz+A[x].cnt;
		return ;
	}
	void print(int u)
	{
	
		if(A[u].lson)print(A[u].lson);
		cout<<A[u].v<<' ';
		if(A[u].rson)print(A[u].rson);
	}
	void sp(int u,int& l,int& r,int sum)
	{
		if(!u)
		{
			l=r=0;
			return ;
		}
	
		if(A[u].v<=sum)
		{

			l=u;
			sp(A[u].rson,A[u].rson,r,sum);
			
		}
		else
		{
			r=u;
			sp(A[u].lson,l,A[u].lson,sum);
		}
		pushup(u);
		return ;
	}
	void me(int &u,int l,int r)
	{
		if(!l||!r)
		{
			u=l+r;
			return ;		
		}	
		
		if(A[l].pri>A[r].pri)
		{
			u=l;
			
			me(A[u].rson,A[l].rson,r);
		}
		else
		{
			u=r;
			
			me(A[u].lson,l,A[r].lson);
		}
		pushup(u);
	}
	
	void ins(int v)
	{
		int root1,root2,root3,root4;
		sp(root,root1,root2,v);
		sp(root1,root3,root4,v-1);
		
		if(!root4)new_node(root4,v);
		 else 
		 {
		 	A[root4].cnt++;
		 	A[root4].sz++;
		 }
		me(root1,root3,root4);
		me(root,root1,root2); 
	}
	void del(int v)
	{
		int root1,root2;
		sp(root,root1,root2,v);
		//cout<<A[root2].v<<" "<<v<<endl;
		root=root2;
		ans+=A[root1].sz;
		return ;
	}
	int kth(int k)
	{
		int cur=root;
		while(1)
		{
			if(k<=A[A[cur].lson].sz)cur=A[cur].lson;
			else if(A[A[cur].lson].sz+1==k)return cur;
			else 
			{
				k-=A[A[cur].lson].sz+1;
				cur=A[cur].rson;
			}
		}
	}
	int main()
	{
		srand(114514);
		scanf("%d%d",&n,&m);
		long long d=0;
		for(int i=1;i<=n;i++)
		{
			char c;
			int num;
			cin>>c>>num;
			if(c=='I')
			{
				if(num>=m)ins(num-d);
			//	print(root);
			//	cout<<endl;
			}
			else if(c=='A')d+=num;
			else if(c=='S')
			{
				d-=num;
				del(m-d-1);
			//	print(root);
			//	cout<<endl;
			}
			else
			{
				if(A[root].sz<num)cout<<"-1\n";
				else cout<<A[kth(A[root].sz-num+1)].v+d<<'\n';
			}
		}
	
		cout<<ans;
		return 0;
	}
}

int main()
{
	acac::main();
	return 0;
}

大概是另类的fhqtreap,下了个数据发现也是对的,不知哪里错了

2023/3/25 11:54
加载中...