求条fhqtreap10pts,搞了至少8.5h了
查看原帖
求条fhqtreap10pts,搞了至少8.5h了
686431
gaodaxia楼主2025/6/21 14:24
#include <bits/stdc++.h>
using namespace std;

const int N=2e6+10;

int stk[N],a[N],top,rt,n,Q;
struct Treap{int ls,rs,siz,val,pri,cov,sum,mx,mxl,mxr,add1,add2;}tree[N];

int New(int x)
{
	int p=stk[top--];
	tree[p].ls=tree[p].rs=tree[p].cov=tree[p].add1=tree[p].add2=0;
	tree[p].pri=rand()*rand();
	tree[p].siz=1;
	tree[p].val=tree[p].sum=x;
	tree[p].mx=x;
	tree[p].mxl=tree[p].mxr=max(x,0);
	return p;
}

void Free(int p)
{
	if(p==0) return;
	stk[++top]=p;
	if(tree[p].ls) Free(tree[p].ls);
	if(tree[p].rs) Free(tree[p].rs);
}

void reverse(int p)
{
	if(p==0) return;
	swap(tree[p].ls,tree[p].rs);
	swap(tree[p].mxl,tree[p].mxr);
	tree[p].add1^=1;
}

void cover(int p,int x)
{
	if(p==0) return;
	tree[p].val=x;
	tree[p].sum=tree[p].siz*x;
	tree[p].mxl=tree[p].mxr=max(x,0);
	tree[p].mx=max(tree[p].sum,x);
	tree[p].add2=1;
	tree[p].cov=x;
}

void pushup(int p)
{
	if(p==0) return;
	tree[p].siz=tree[tree[p].ls].siz+1+tree[tree[p].rs].siz;
	tree[p].sum=tree[tree[p].ls].sum+tree[p].val+tree[tree[p].rs].sum;
	tree[p].mxl=max(max((tree[p].ls?tree[tree[p].ls].mxl:0),(tree[p].ls?tree[tree[p].ls].sum:0)+tree[p].val+(tree[p].rs?tree[tree[p].rs].mxl:0)),0);
	tree[p].mxr=max(max((tree[p].rs?tree[tree[p].rs].mxr:0),(tree[p].rs?tree[tree[p].rs].sum:0)+tree[p].val+(tree[p].ls?tree[tree[p].ls].mxr:0)),0);
	tree[p].mx=max(tree[p].val,tree[tree[p].ls].mxr+tree[p].val+tree[tree[p].rs].mxl);
	if(tree[p].ls) tree[p].mx=max(tree[p].mx,tree[tree[p].ls].mx);
	if(tree[p].rs) tree[p].mx=max(tree[p].mx,tree[tree[p].rs].mx);
}

void pushdown(int p)
{
	if(p==0) return;
	if(tree[p].add1)
	{
		if(tree[p].ls) reverse(tree[p].ls);
		if(tree[p].rs) reverse(tree[p].rs);
		tree[p].add1=0;
	}
	if(tree[p].add2)
	{
		if(tree[p].ls) cover(tree[p].ls,tree[p].cov);
		if(tree[p].rs) cover(tree[p].rs,tree[p].cov);
		tree[p].add2=tree[p].cov=0;
	}
}

int merge(int p1,int p2)
{
	if(p1==0) return p2;
	if(p2==0) return p1;
	pushdown(p1);
	pushdown(p2);
	if(tree[p1].pri<tree[p2].pri)
	{
		tree[p1].rs=merge(tree[p1].rs,p2);
		pushup(p1);
		return p1; 
	}
	else
	{
		tree[p2].ls=merge(p1,tree[p2].ls);
		pushup(p2);
		return p2;
	}
}

pair<int,int> split(int p,int k)
{
	if(p==0) return {0,0};
	pushdown(p);
	if(k<=tree[tree[p].ls].siz)
	{
		pair<int,int> t=split(tree[p].ls,k);
		tree[p].ls=t.second;
		pushup(p);
		return make_pair(t.first,p);
	}
	else if(k==tree[tree[p].ls].siz+1)
	{
		int t=tree[p].rs;
		tree[p].rs=0;
		pushup(p);
		return make_pair(p,t);
	}
	else
	{
		pair<int,int> t=split(tree[p].rs,k-tree[tree[p].ls].siz-1);
		tree[p].rs=t.first;
		pushup(p);
		return make_pair(p,t.second);
	}
}

int build(int l,int r)
{
	if(l==r) return New(a[l]);
	int mid=(l+r)>>1;
	return merge(build(l,mid),build(mid+1,r));
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> Q;
	for(int i=1;i<N;i++) stk[++top]=i;
	for(int i=1;i<=n;i++) cin >> a[i];
	srand(time(0));
	rt=build(1,n);
	while(Q--)
	{
		string opt;
		cin >> opt;
		if(opt=="INSERT")
		{
			int pos,tot;
			cin >> pos >> tot;
			pair<int,int> t=split(rt,pos);
			for(int i=1;i<=tot;i++) cin >> a[i];
			t.first=merge(t.first,build(1,tot));
			rt=merge(t.first,t.second);
		}
		else if(opt=="DELETE")
		{
			int pos,tot;
			cin >> pos >> tot;
			pair<int,int> t1=split(rt,pos-1),t2=split(t1.second,tot);
			Free(t2.first);
			rt=merge(t1.first,t2.second);
		}
		else if(opt=="MAKE-SAME")
		{
			int pos,tot,x;
			cin >> pos >> tot >> x;
			pair<int,int> t1=split(rt,pos-1),t2=split(t1.second,tot);
			cover(t2.first,x);
			t1.second=merge(t2.first,t2.second);
			rt=merge(t1.first,t1.second);
		}
		else if(opt=="REVERSE")
		{
			int pos,tot;
			cin >> pos >> tot;
			pair<int,int> t1=split(rt,pos-1),t2=split(t1.second,tot);
			reverse(t2.first);
			t1.second=merge(t2.first,t2.second);
			rt=merge(t1.first,t1.second);
		}
		else if(opt=="GET-SUM")
		{
			int pos,tot;
			cin >> pos >> tot;
			pair<int,int> t1=split(rt,pos-1),t2=split(t1.second,tot);
			cout << tree[t2.first].sum << '\n';
			t1.second=merge(t2.first,t2.second);
			rt=merge(t1.first,t1.second);
		}
		else cout << tree[rt].mx << '\n';
	}
	return 0;
}
2025/6/21 14:24
加载中...