首页 > 代码库 > 【BZOJ】1251: 序列终结者(splay)

【BZOJ】1251: 序列终结者(splay)

http://www.lydsy.com/JudgeOnline/problem.php?id=1251

不行。。为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint...

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const ll getll() { ll r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }struct node *null;struct node {	node *f, *c[2];	ll mx, k, tag;	int s;	bool rev;	node(ll _k=0) { f=c[0]=c[1]=null; k=mx=_k; s=1; rev=0; tag=0; }	bool d() { return f->c[1]==this; }	void setc(node *x, bool d) { c[d]=x; x->f=this; }	void pushup() { mx=max(k, max(c[0]->mx, c[1]->mx)); s=1+c[0]->s+c[1]->s; }	void upd(int x) {		if(this==null) return;		tag+=x;		k+=x;		mx+=x;	}	void pushdown() {		if(rev) {			rev=0;			swap(c[0], c[1]);			if(c[0]!=null) c[0]->rev=!c[0]->rev;			if(c[1]!=null) c[1]->rev=!c[1]->rev;		}		if(tag) {			c[0]->upd(tag);			c[1]->upd(tag);			tag=0;		}	}}*root;void rot(node *x) {	node *f=x->f;	f->pushdown(); x->pushdown(); bool d=x->d();	f->f->setc(x, f->d());	f->setc(x->c[!d], d);	x->setc(f, !d);	f->pushup();	if(f==root) root=x;}void splay(node *x, node *f=null) {	x->pushdown();	while(x->f!=f) 		if(x->f->f==f) rot(x);		else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));	x->pushup();}node *sel(node *x, int k) {	if(x==null) return null;	x->pushdown();	int s=x->c[0]->s;	if(s==k) return x;	if(s<k) return sel(x->c[1], k-s-1); return sel(x->c[0], k);}node *getrange(int l, int r) {	splay(sel(root, l-1));	splay(sel(root, r+1), root);	return root->c[1]->c[0];}void build(int l, int r, node *&x) {	if(l>r) return;	x=new node();	if(l==r) return;	int mid=(l+r)>>1;	build(l, mid-1, x->c[0]);	build(mid+1, r, x->c[1]);	if(l<=mid-1) x->c[0]->f=x;	if(mid+1<=r) x->c[1]->f=x;	x->pushup();}int n, m;void init() {	null=new node(); null->s=0; null->k=null->mx=-(~0ull>>1);	null->f=null->c[0]=null->c[1]=null;	root=new node();	root->setc(new node(), 1);	node *x;	build(1, n, x);	root->c[1]->setc(x, 0);	root->c[1]->pushup();	root->pushup();}void plu() {	int l=getint(), r=getint(); ll v=getll();	node *x=getrange(l, r);	x->upd(v);}void rev() {	int l=getint(), r=getint();	node *x=getrange(l, r);	x->rev=!x->rev;}void ask() {	int l=getint(), r=getint();	node *x=getrange(l, r);	printf("%lld\n", x->mx);}int main() {	read(n); read(m);	init();	while(m--) {		int c=getint();		if(c==1) plu();		else if(c==2) rev();		else ask();	}	return 0;}

  

 


 

 

Description

网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。

Input

第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。

Output

对于每个第3种操作,给出正确的回答。

Sample Input

4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4

Sample Output

2
【数据范围】
N<=50000,M<=100000。

HINT

 

Source

Splay

 

【BZOJ】1251: 序列终结者(splay)