刚学OI,不短的代码,求找UB
查看原帖
刚学OI,不短的代码,求找UB
188769
Vanilla_chan楼主2021/4/2 11:57
/**************************************************************
 * Problem: 2042
 * Author: Vanilla_chan
 * Date: 20210402
 * E-Mail: Vanilla_chan@outlook.com
**************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<limits.h>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
//#ifdef TH
#define debug printf("Now is %d\n",__LINE__);
//#else
//#define debug
//#endif
#ifdef ONLINE_JUDGE
char buf[1<<23],* p1=buf,* p2=buf,obuf[1<<23],* O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
using namespace std;

namespace oi
{
	inline bool isdigit(const char& ch)
	{
		return ch<='9'&&ch>='0';
	}
	inline bool isalnum(const char& ch)
	{
		return (ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9');
	}
	struct istream
	{
		char ch;
		bool fu;
		template<class T>inline istream& operator>>(T& d)
		{
			fu=(d=0);
			while(!isdigit(ch)&&ch!='-') ch=getchar();
			if(ch=='-') fu=1,ch=getchar();
			d=ch-'0';ch=getchar();
			while(isdigit(ch))
				d=(d<<3)+(d<<1)+(ch^'0'),ch=getchar();
			if(fu) d=-d;
			return *this;
		}
		inline istream& operator>>(char& ch)
		{
			ch=getchar();
			for(;!isalnum(ch);ch=getchar());
			return *this;
		}
		inline istream& operator>>(string& str)
		{
			str.clear();
			for(;!isalnum(ch);ch=getchar());
			while(isalnum(ch))
				str+=ch,ch=getchar();
			return *this;
		}
	}cin;
	inline int read()
	{
		int x=0,fu=1;
		char ch=getchar();
		while(!isdigit(ch)&&ch!='-') ch=getchar();
		if(ch=='-') fu=-1,ch=getchar();
		x=ch-'0';ch=getchar();
		while(isdigit(ch)) { x=x*10+ch-'0';ch=getchar(); }
		return x*fu;
	}
	int G[55];
	template<class T>inline void write(T x)
	{
		int g=0;
		if(x<0) x=-x,putchar('-');
		do { G[++g]=x%10;x/=10; } while(x);
		for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
	}
};
using namespace oi;

#define N 500010
struct node
{
	node *ls,*rs;
	int val;
	int sze;
	int sum,zmax,lmax,rmax;
	bool rev;
	bool lazy;
	int k;
	int key;
	void upd()
	{
		sze=1;
		zmax=lmax=rmax=sum=val;
		if(ls)
		{
			sum+=ls->sum;
			sze+=ls->sze;
			lmax=max(lmax,ls->lmax);
			zmax=max(zmax,ls->zmax);
		}
		if(rs)
		{
			sum+=rs->sum;
			sze+=rs->sze;
			rmax=max(rmax,rs->rmax);
			zmax=max(zmax,rs->zmax);
		}
		if(ls&&rs)
		{
			zmax=max(zmax,ls->rmax+rs->lmax);
		}
	}
	node(int v)
	{
		ls=rs=0;
		lmax=rmax=zmax=val=v;
		rev=lazy=k=0;
		key=rand();
		upd();
	}
	void work()
	{
		rev^=1;
		swap(ls,rs);
	}
	void work(int v)
	{
		lazy=1;
		val=k=v;
		sum=sze*k;
		
		rev=0;
	}
	void spread()
	{
		if(lazy)
		{
			if(ls) ls->work(k);
			if(rs) rs->work(k);
			lazy=k=0;
			rev=0;
		}
		if(rev)
		{
			if(ls) ls->work();
			if(rs) rs->work();
			rev=0;
		}
	}
	~node()
	{
		if(ls) delete ls;
		if(rs) delete rs;
	}
}*root;
node* merge(node *x,node *y)
{
	if(!x) return y;
	if(!y) return x;
	if(x->key > y->key)
	{
		x->spread();
		x->rs=merge(x->rs,y);
		x->upd();
		return x;
	}
	else
	{
		y->spread();
		y->ls=merge(x,y->ls);
		y->upd();
		return y;
	}
}
void split(node *i,int k,node *&x,node *&y)
{
	debug cout<<i<<endl;
	if(!i)
	{
		x=y=0;
		return;
	}
	i->spread();
	if(i->ls->sze>=k)
	{
		y=i;
		split(i->ls,k,x,i->ls);
	}
	else
	{
		x=i;
		split(i->rs,k-i->ls->sze-1,i->rs,y);
	}
	i->upd();
}
void out(node *x)
{
	x->spread();
	if(x->ls) out(x->ls);
	cout<<x->val<<" ";
	if(x->rs) out(x->rs);
}
node *x,*y,*z;
int n,m;
int a[N];
int main()
{
	freopen("2042.in","r",stdin);
	//freopen(".out","w",stdout);
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		root=merge(root,new node(read()));
	}
	out(root);
	debug
	string op;
	int pos,tot,c;
	while(m--)
	{
		oi::cin>>op;
		debug
		if(op[0]=='I')//INSERT
		{
			pos=read();
			split(root,pos,x,y);
			tot=read();
			for(int i=1;i<tot;i++) merge(x,new node(read()));
			root=merge(x,y);
		}
		else if(op[0]=='D')//DELETE
		{
			pos=read();
			split(root,pos-1,x,y);
			split(y,tot,y,z);
			root=merge(x,z);
			delete y;
		}
		else if(op[0]=='M'&&op[2]=='K')
		{
			pos=read();
			tot=read();
			c=read();
			split(root,pos-1,x,y);
			split(y,tot,y,z);
			y->work(c);
			root=merge(x,merge(y,z));
		}
		else if(op[0]=='R')
		{
			pos=read();
			tot=read();
			split(root,pos-1,x,y);
			split(y,tot,y,z);
			y->work();
			root=merge(x,merge(y,z));
		}
		else if(op[0]=='G')//GET-SUM
		{
			pos=read();
			tot=read();
			debug
			split(root,pos-1,x,y);
			debug
			cout<<y<<endl;
			split(y,tot,y,z);
			//debug
			//cout<<y<<endl;
			//cout<<"y.size="<<y->sze<<endl;
			write(y->sum);
			debug
			root=merge(x,merge(y,z));
		}
		else
		{
			pos=read();
			tot=read();
			split(root,pos-1,x,y);
			split(y,tot,y,z);
			write(y->zmax);
			root=merge(x,merge(y,z));
		}
	}
	return 0;
}

2021/4/2 11:57
加载中...