权值线段树求调,悬赏关注
查看原帖
权值线段树求调,悬赏关注
456675
a_sad_soul楼主2023/10/9 13:17
#include<bits/stdc++.h>
#define lc(x)(x<<1)
#define rc(x)(x<<1|1)
#define MAXN 1000006
using namespace std;
int ans;
struct node{
	int le,ri;//值域
	int l,r;
	int lazycl=-1,lazy;//lazycl是指这个区间是否要被清空
  //lazy则是这个区间变动,左移或右移多少
	int sum;
}tree[MAXN<<2];
int tot;
void pushdown(int p)
{
	tree[lc(p)].le+=tree[p].lazy;
	tree[lc(p)].ri+=tree[p].lazy;
	tree[rc(p)].le+=tree[p].lazy;
	tree[rc(p)].ri+=tree[p].lazy;
	tree[lc(p)].lazy+=tree[p].lazy;
	tree[rc(p)].lazy+=tree[p].lazy;
	tree[p].lazy=0;
	if(tree[p].lazycl==0)
		tree[lc(p)].sum=tree[rc(p)].sum=0,tree[p].lazycl=-1,tree[lc(p)].lazycl=tree[rc(p)].lazycl=0;
}
void Lazy(int p,int val,int cle)
{
	tree[p].le+=val;
	tree[p].ri+=val;
	if(cle==0)tree[p].sum=0,tree[p].lazycl=0;
	tree[p].lazy+=val;
}
void build(int p,int L,int R,int l,int r)
{
	tree[p].l=L,tree[p].le=l,tree[p].r=R,tree[p].ri=r;
	if(L==R)return ;
	int mid=(L+R)>>1;
	int MID=(l+r)>>1;
	build(lc(p),L,mid,l,MID);
	build(rc(p),mid+1,R,MID+1,r);
}
void updateByval(int val){Lazy(1,val,1);}
void Insert(int p,int val)
{		
	tree[p].sum++;
	if(tree[p].lazy||tree[p].lazycl==0)pushdown(p);
	if(tree[p].l==tree[p].r)return ;
	int mid=(tree[p].le+tree[p].ri)/2;
	if(val<=mid)Insert(lc(p),val);
	else Insert(rc(p),val);
}
void del(int p)
{
	if(tree[p].lazy||tree[p].lazycl==0)pushdown(p);
	if(tree[p].le>=0)return ;
	if(tree[p].ri<0)
	{
		tot+=tree[p].sum;
		Lazy(p,0,0);
		return ;
	}
	if(tree[p].l==tree[p].r)return ;
	del(lc(p));
	del(rc(p));
	tree[p].sum=tree[lc(p)].sum+tree[rc(p)].sum;
}
int query(int p,int k)
{
	if(tree[p].lazy||tree[p].lazycl==0)pushdown(p);
	if(tree[p].l==tree[p].r)return (tree[p].sum==0?-1:tree[p].le);
	if(k<=tree[rc(p)].sum)return query(rc(p),k);
	if(tree[lc(p)].sum)return query(lc(p),k-tree[rc(p)].sum);
	return -1;
}
int n,m;
int main()
{
	freopen("P1486_2.in","r",stdin);
	freopen("out.txt","w",stdout);
	scanf("%d%d",&n,&m);
	build(1,1,1000001,0,1000000);
	while(n--)
	{
		char ch;
		int k;
		cin>>ch;
		scanf("%d",&k);
		if(ch=='I')
      {
			if(k>=m) Insert(1,k-m);
		}
		if(ch=='A'){del(1);updateByval(k);}
		if(ch=='S'){updateByval(-k);del(1);}
		if(ch=='F')
		{
			del(1);
			int re=query(1,k);
			if(re!=-1)re+=m;
			printf("%d\n",re);
		}
	}
	del(1);
	cout<<tot<<endl;
	return 0;
}
2023/10/9 13:17
加载中...