【splay tree】 POJ 3580 SuperMemo

注意INF不要取的太小。。。注意pushdown的时候要修改节点的最小值,这不是线段树。。。注意REVOLVE 可能有负的。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 400005
#define eps 1e-10
#define mod 1000000007
#define INF 1e17
#define lowbit(x) (x&(-x))  
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
//typedef int LL;
using namespace std;

struct node
{
	int s, flip, add;
	LL v, minv;
	node *ch[2], *fa;
	int cmp(int x)
	{
		if(x == ch[0]->s + 1) return -1;
		if(x > ch[0]->s + 1) return 1;
		else return 0;
	}
	void pushdown(void)
	{
		if(flip) {
			swap(ch[0], ch[1]);
			ch[0]->flip ^= 1;
			ch[1]->flip ^= 1;
			flip = 0;
		}
		if(add) {
			if(ch[0]->fa != NULL) {
				ch[0]->add += add;
				ch[0]->v += add;
				ch[0]->minv += add;
			}
			if(ch[1]->fa != NULL) {
				ch[1]->add += add;
				ch[1]->v += add;
				ch[1]->minv += add;
			}
			add = 0;
		}
	}
	void maintain(void)
	{
		s = ch[0]->s + ch[1]->s + 1;
		minv = v;
		if(ch[0]->fa != NULL) minv = min(minv, ch[0]->minv);
		if(ch[1]->fa != NULL) minv = min(minv, ch[1]->minv);
	}
}*null, *root, C[maxn], *top;
void rotate(node* &o, bool d)
{
	node *p = o->fa; p->ch[d^1] = o->ch[d], o->fa = p->fa;
	if(p->fa != null) {
		if(p->fa->ch[0] == p) p->fa->ch[0] = o;
		else p->fa->ch[1] = o;
	}
	if(p->ch[d^1] != null) p->ch[d^1]->fa = p;
	p->fa = o, o->ch[d] = p;
	p->maintain(), o->maintain();
}
void splay(node *o)
{
	node *p;
	while(o->fa != null) {
		p = o->fa;
		if(p->ch[0] == o) {
			if(p != null && p->fa->ch[0] == p)
				rotate(p, true);
			rotate(o, true);
		}
		else {
			if(p != null && p->fa->ch[1] == p)
				rotate(p, false);
			rotate(o, false);
		}
	}
}
void kth(node* &o, int k)
{
	o->pushdown();
	o->maintain();
	int d = o->cmp(k);
	while(d != -1) {
		if(d) {
			k -= o->ch[0]->s + 1;
			o = o->ch[1];
		}
		else o = o->ch[0];
		o->pushdown();
		o->maintain();
		d = o->cmp(k);
	}
	splay(o);
}
node* merge(node *left, node *right)
{
	kth(left, left->s);
	left->ch[1] = right;
	right->fa = left;
	left->maintain();
	return left;
}
void split(node *o, int k, node* &left, node* &right)
{
	kth(o, k);
	right = o->ch[1];
	right->fa = null;
	o->ch[1] = null;
	left = o;
	left->maintain();
}
node* newnode(void)
{
	top++;
	top->ch[0] = top->ch[1] = top->fa = null;
	top->flip = top->add = top->v = top->s = top->minv = 0;
	return top;
}
void init(void)
{
	top = C;
	null = top++;
	null->ch[0] = null->ch[1] = null->fa = NULL;
	null->v = null->s = null->flip = null->add = 0;
	null->minv = INF;
	root = newnode();
	root->v = INF;
	root->maintain();
}
int n, m;
int num[maxn];
char s[100];

void read(void)
{
	for(int i = 1; i <= n; i++)
		scanf("%d", &num[i]);
	scanf("%d", &m);
}
void build(void)
{
	node *p;
	for(int i = 1; i <= n; i++) {
		p = newnode();
		p->v = num[i];
		p->ch[0] = root;
		root->fa = p;
		root = p;
		root->maintain();
	}
}
void debug(node *o)
{
	o->pushdown();
	if(o->ch[0] != null) debug(o->ch[0]);
	printf("AA %lld %lld %d BB\n", o->v, o->minv, o->s);
	if(o->ch[1] != null) debug(o->ch[1]);
}
void work(void)
{
	node *left, *right, *mid, *o, *p1, *p2;
	int a, b, k;
	LL v;
	while(m--) {
		scanf("%s", s);
		if(s[0] == 'R' && s[4] == 'R') {
			scanf("%d%d", &a, &b);
			split(root, a, left, o);
			split(o, b-a+1, mid, right);
			mid->flip^=1;
			root = merge(merge(left, mid), right);
		}
		if(s[0] == 'A') {
			scanf("%d%d%d", &a, &b, &k);
			split(root, a, left, o);
			split(o, b-a+1, mid, right);
			mid->add += k;
			mid->minv += k;
			mid->v += k;
			mid->pushdown();
			mid->maintain();
			root = merge(merge(left, mid), right);
		}
		if(s[0] == 'M') {
			scanf("%d%d", &a, &b);
			split(root, a, left, o);
			split(o, b-a+1, mid, right);
			printf("%lld\n", mid->minv);
			root = merge(merge(left, mid), right);
		}
		if(s[0] == 'D') {
			scanf("%d", &k);
			split(root, k, left, o);
			split(o, 1, mid, right);
			root = merge(left, right);
		}
		if(s[0] == 'I') {
			scanf("%d%lld", &k, &v);
			o = newnode();
			o->v = v;
			o->maintain();
			split(root, k+1, left, right);
			root = merge(merge(left, o), right);
		}
		if(s[0] == 'R' && s[4] == 'L') {
			scanf("%d%d%d", &a, &b, &k);
			split(root, a, left, o);
			split(o, b-a+1, mid, right);
			k %= mid->s;
			k = (mid->s + k) % mid->s;
			k = mid->s - k;
			split(mid, k, p1, p2);
			if(p2 == null) mid = p1;
			else mid = merge(p2, p1);
			root = merge(merge(left, mid), right);
		}
	}
}
int main(void)
{
	while(scanf("%d", &n)!=EOF) {
		init();
		read();
		build();
		work();
	}
	return 0;
}


你可能感兴趣的:(poj)