WA 20 在线等 玄2关
查看原帖
WA 20 在线等 玄2关
983555
fengzhaoyu楼主2025/2/5 20:20
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+15,inf=LLONG_MAX;
struct node{
	int s[2],v,size,p,plus,maxt;
	bool flag;
	void init(int _v,int _p)
	{
		v=_v,p=_p;
		size=1;
	}
}t[N<<2];
int n,m,root,idx;
int read()
{
	int t=1,x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') t=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return t*x;
}
void push_up(int u)
{
	t[u].size=t[t[u].s[0]].size+t[t[u].s[1]].size+1;
	t[u].maxt=t[u].v;
	if(t[u].s[0])t[u].maxt=max(t[u].maxt,t[t[u].s[0]].maxt);
	if(t[u].s[1])t[u].maxt=max(t[u].maxt,t[t[u].s[1]].maxt);
}
void push_down(int u)
{
	if(t[u].flag){
	  swap(t[u].s[0],t[u].s[1]);
	  if(t[u].s[0])t[t[u].s[1]].flag^=1;
	  if(t[u].s[1])t[t[u].s[1]].flag^=1;
	  t[u].flag=0;
	}
	if(t[u].plus)
	{
		if(t[u].s[0])
		{
			t[t[u].s[0]].plus+=t[u].plus;
			t[t[u].s[0]].maxt+=t[u].plus;
			t[t[u].s[0]].v+=t[u].plus;
		}
		if(t[u].s[1])
		{
			t[t[u].s[1]].plus+=t[u].plus;
			t[t[u].s[1]].maxt+=t[u].plus;
			t[t[u].s[1]].v+=t[u].plus;
		}
		t[u].plus=0;
	}
}
void rotate(int x)
{
	int y=t[x].p,z=t[y].p;
	int k=t[y].s[1]==x;
	t[z].s[t[z].s[1]==y]=x,t[x].p=z;
	t[y].s[k]=t[x].s[k^1],t[t[x].s[k^1]].p=y;
	t[x].s[k^1]=y,t[y].p=x;
	push_up(y),push_up(x);
}
void splay(int x,int k)
{
	while(t[x].p!=k)
	{
		int y=t[x].p,z=t[y].p;
		if(z!=k)
		{
			if((t[y].s[1]==x)^(t[z].s[1]==y)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
	if(!k) root=x;
}
void insert(int x)
{
	int u=root,p=0;
	while(u) p=u,u=t[u].s[x>t[u].v];
	u=++idx;
	if(p) t[p].s[x>t[u].v]=u;
	t[u].init(x,p);
	splay(u,0);
}
int get_k(int k)
{
	int u=root;
	while(1)
	{
		push_down(u);
		if(t[t[u].s[0]].size<k-1)
		{
			k-=t[t[u].s[0]].size+1;
			u=t[u].s[1];
		}
		else if(t[t[u].s[0]].size+1==k) return u;
		else 
		{
			u=t[u].s[0];
		}
	}
}
int get_max(int l,int r)
{
	int l1=get_k(l);
	int r1=get_k(r);
	splay(l1,0);
	splay(r1,l1);
	push_down(t[r1].s[0]);
	return t[t[r1].s[0]].maxt;
}
signed main()
{
	n=read();
	m=read();
	for(int i=0;i<=n+1;i++)
	{
		insert(0);
	}
	for(int i=1;i<=m;i++)
	{
		int opt=read();
		int l=read();
		int r=read();
		if(opt==1)
		{
			int v=read();
			l=get_k(l);
			r=get_k(r+2);
			splay(l,0);
			splay(r,l);
			t[t[r].s[0]].v+=v;
			t[t[r].s[0]].maxt+=v;
			t[t[r].s[0]].plus+=v;
//			push_up(t[root].s[1]);
//			push_up(root);
		}
		else if(opt==2)
		{
			l=get_k(l);
			r=get_k(r+2);
			splay(l,0);
			splay(r,l);
			t[t[r].s[0]].flag^=1;
//			push_up(t[root].s[1]);
//			push_up(root);
		}
		else
		{
			printf("%lld\n",get_max(l,r+2));
		}
	}
	return 0;
} 
2025/2/5 20:20
加载中...