bzoj3678 wangxz与OJ splay

15 篇文章 0 订阅

Description


维护一个初始有n个元素的序列(标记为1~n号元素),支持以下操作:

0 p a b (0<=p<=当前序列元素个数) (a<=b) 在p位置和p+1位置之间插入整数:a,a+1,a+2,…,b-1,b。若p为0,插在序列最前面;
1 a b (1<=a<=b<=当前序列元素个数) 删除a,a+1,a+2,…,b-1,b位置的元素;
2 p (1<=p<=当前序列元素个数) 查询p位置的元素。

输入第一行包括两个正整数n(1<=n<=20000),m(1<=m<=20000),代表初始序列元素个数和操作个数。
接下来n个整数,为初始序列元素。
接下来m行,每行第一个为整数sym,
若sym=0,接下来有一个非负整数p,两个整数a,b;
若sym=1,接下来有两个正整数a,b;
若sym=2,接下来有一个正整数p;

在任何情况下,保证序列中的元素总数不超过100000。

Solution


据说stl可以搞

维护序列可以维护差分数组然后前缀和,这样插入等差数列就可以整体打标记了。操作0很容易想到可持久化treap,但是太难打了。
作为splay爱好者我们用一个节点代表一个区间,查询和splay之前就分裂成三份。对于等差数列我们对一个区间记录首项和项数,这样插入就可以直接算了。

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int N=120005;

struct treeNode {int son[2],fa,size,len,num;} t[N];

int root,tot;

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

void push_up(int x) {
	t[x].size=t[x].len+t[t[x].son[0]].size+t[t[x].son[1]].size;
}

void rotate(int x) {
	int y=t[x].fa; int z=t[y].fa;
	int k=t[y].son[1]==x;
	t[z].son[t[z].son[1]==y]=x; t[x].fa=z;
	t[y].son[k]=t[x].son[!k]; t[t[x].son[!k]].fa=y;
	t[x].son[!k]=y; t[y].fa=x;
	push_up(y); push_up(x);
}

void splay(int x,int goal=0) {
	for (;t[x].fa!=goal;) {
		int y=t[x].fa; int z=t[y].fa;
		if (z!=goal) {
			if ((t[z].son[1]==y)^(t[y].son[1]==x)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
	(!goal)?(root=x):0;
}

int kth(int k) {
	int x=root;
	for (;233;) {
		if (t[t[x].son[0]].size>=k) x=t[x].son[0];
		else if (t[t[x].son[0]].size+t[x].len<k) k-=t[t[x].son[0]].size+t[x].len,x=t[x].son[1];
		else {
			k-=t[t[x].son[0]].size;
			if (k!=1) {
				t[++tot].fa=x;
				t[tot].son[0]=t[x].son[0];
				t[tot].num=t[x].num;
				t[t[x].son[0]].fa=tot;
				t[x].son[0]=tot;
				t[tot].len=k-1;
				push_up(tot);
			}
			if (k!=t[x].len) {
				t[++tot].fa=x;
				t[tot].son[0]=t[x].son[1];
				t[tot].num=t[x].num+k;
				t[t[x].son[1]].fa=tot;
				t[x].son[1]=tot;
				t[tot].len=t[x].len-k;
				push_up(tot);
			}
			t[x].len=1; t[x].num+=k-1;
			return x;
		}
	}
}

int build(int l,int r) {
	int mid=(l+r)>>1;
	if (l!=mid) {
		t[mid].son[0]=build(l,mid-1);
		t[t[mid].son[0]].fa=mid;
	}
	if (mid!=r) {
		t[mid].son[1]=build(mid+1,r);
		t[t[mid].son[1]].fa=mid;
	}
	push_up(mid);
	return mid;
}

int split(int l,int r) {
	l=kth(l); r=kth(r+2);
	splay(l); splay(r,root);
	return r;
}

int main(void) {
	int n=read(),m=read();
	t[++tot].size=1; t[1].len=1;
	rep(i,1,n) {
		t[++tot].num=read();
		t[tot].len=t[tot].size=1;
	}
	t[++tot].len=1; t[tot].size=1;
	root=build(1,n+2);
	for (int opt,a,b,p;m--;) {
		opt=read();
		if (opt==0) {
			p=read(),a=read(),b=read();
			int res=split(p+1,p);
			t[++tot].fa=res; t[res].son[0]=tot;
			t[tot].len=t[tot].size=b-a+1; t[tot].num=a;
			push_up(res); push_up(root);
		} else if (opt==1) {
			a=read(),b=read();
			int res=split(a,b);
			t[t[res].son[0]].fa=t[res].son[0]=0;
			push_up(res); push_up(root);
		} else {
			p=read();
			printf("%d\n", t[t[split(p,p)].son[0]].num);
		}
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值