[BZOJ3223] 文艺平衡树 - splay

    验证了一发自己还有水平之后就懒得写了

    直接上维修数列那题的模板TAT

#include "algorithm"
#include "iostream"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
using namespace std;

const int inf= (int)1e9,N=100005;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
struct node {
	node *c[2],*fa;
	bool rev; int tag,siz;
	int lmx,rmx,smx,sum,key;
	node(); 
	void push_up();
	void push_down();
	void ins(node *lc,node *rc,node *f,int _k);
} Tnull ,*null=&Tnull, *root=null;

void seq(node *x,int tot,bool rv){
	if (x->tag&&tot==inf) tot=x->key;
	int l=x->rev^rv,r=l^1; 
	if (x->c[l]!=null) printf("{ "),seq(x->c[l],tot,l),printf(" }");
	if (tot==inf) printf ("[%d L=%d,R=%d,M=%d,S=%d] ",x->key,x->lmx,x->rmx,x->smx,x->sum);
	else printf ("[%d L=%d,R=%d,M=%d,S=%d] ",tot,max(tot,tot*x->siz),max(tot,tot*x->siz),max(tot,tot*x->siz),tot*x->siz);
    if (x->c[r]!=null) printf("{ "),seq(x->c[r],tot,l),printf(" }");
}
int ___write[N],cnt;
void check(node *x){
	x->push_down();
	if (x->c[0]!=null) check(x->c[0]);
	___write[cnt++]=x->key;
	if (x->c[1]!=null) check(x->c[1]);
}

node:: node(){
	c[0]=c[1]=fa=null;
}
void node :: push_up(){
	c[0]->push_down(); c[1]->push_down();
    sum=c[0]->sum+c[1]->sum+key;
	siz=c[0]->siz+c[1]->siz+1;
	lmx=rmx=smx=-inf;
	if (c[0]!=null&&c[1]!=null){
		lmx=max(c[0]->lmx,c[0]->sum+key+max(c[1]->lmx,0));
		rmx=max(c[1]->rmx,c[1]->sum+key+max(c[0]->rmx,0));
		smx=max(max(c[0]->smx,c[1]->smx),max(c[0]->rmx,0)+max(c[1]->lmx,0)+key);
	} else {
		if (c[0]==null&&c[1]==null){
			lmx=rmx=smx=key;
		} else {
			if (c[0]==null){
				lmx=max(c[1]->lmx,0)+key;
				rmx=max(c[1]->rmx,c[1]->sum+key);
				smx=max(c[1]->smx,max(c[1]->lmx,0)+key);
			} else {
				lmx=max(c[0]->lmx,c[0]->sum+key);
				rmx=max(c[0]->rmx,0)+key;
				smx=max(c[0]->smx,max(c[0]->rmx,0)+key);
			}
		}
	}
}
void node :: push_down(){
	if (tag){
		if(c[0]!=null){ 
			c[0]->tag=1; c[0]->key=key; c[0]->sum=key*c[0]->siz;
			c[0]->lmx=c[0]->rmx=c[0]->smx=max(key,key*c[0]->siz);
		}
		if(c[1]!=null){ 
			c[1]->tag=1; c[1]->key=key; c[1]->sum=key*c[1]->siz;
			c[1]->lmx=c[1]->rmx=c[1]->smx=max(key,key*c[1]->siz);
		}
		tag=0;
	}
	if (rev){ rev=0;
		if(c[0]!=null) c[0]->rev^=1;
		if(c[1]!=null) c[1]->rev^=1;
		swap(c[0],c[1]); swap(lmx,rmx);
	}
}
void node :: ins (node *lc,node *rc,node *f,int _k){
	c[0]=lc; c[1]=rc; fa=f;
	key=lmx=rmx=smx=_k;
	push_up(); tag=rev=0;
}

node * build (int *x,int l){ 
	if (l<=0) return null;
	int mid=(l+1)>>1;
	node * d=new node();
	node *c0=build(x,mid-1);
	node *c1=build(x+mid,l-mid);
	if (c0!=null) c0->fa=d;
	if (c1!=null) c1->fa=d;
	d->ins(c0,c1,null,x[mid]);
	return d;
}

node * find_rank (int rank,node *x){
	x->push_down(); node* rk;
	if (x->c[0]->siz+1==rank) return x;
	if (x->c[0]->siz>=rank) rk=find_rank(rank,x->c[0]);
	else rk=find_rank(rank-x->c[0]->siz-1,x->c[1]);
	x->push_up(); return rk;
}

void rotate (node *x,node *& f){
	node *y=x->fa,*z=y->fa; int l,r;
	if (y->c[0]==x) l=0; else l=1; r=l^1;
	if (y==f) f=x;
	if (z!=null){
		if(z->c[0]==y) z->c[0]=x;
		else z->c[1]=x;
	}
	x->fa=z; y->fa=x;
    if(x->c[r]!=null) x->c[r]->fa=y;
	y->c[l]=x->c[r]; x->c[r]=y;
	y->push_up(); x->push_up();
}

void splay (node * x, node * & f){
    while (x!=f){
		node *y=x->fa,*z=y->fa; 
		if (y!=f){
			if (z->c[0]==y ^ y->c[0]==x) rotate(x,f);
			else rotate(y,f);
		}
		rotate(x,f);
	}
	if(f->fa!=null) f->fa->push_up();
}

void insert (int pos,int *x,int l){
	node * q=find_rank(pos,root);
	node * qq=find_rank(pos+1,root);
    splay (q,root); splay(qq,root->c[1]);
	root->c[1]->c[0]=build(x,l); root->c[1]->c[0]->fa=root->c[1];
    root->c[1]->push_up(); root->push_up();
}

void deleteit (node * x){
	if (x->c[0]!=null) deleteit(x->c[0]);
	if (x->c[1]!=null) deleteit(x->c[1]);
	delete x;
}

void del (int pos,int tot){
	node * q=find_rank(pos-1,root);
	node * qq=find_rank(pos+tot-1,root);
    splay (q,root); splay (qq,root->c[1]);
	node * freeit=root->c[1]; root->c[1]=root->c[1]->c[1];
	root->c[1]->fa=root; freeit->c[1]=null; deleteit(freeit); 
	root->push_up();
}

void make_same (int pos,int tot,int v){
	node * q=find_rank(pos-1,root);
	node * qq=find_rank(pos+tot,root);
    splay (q,root); splay (qq,root->c[1]);
	node * t=root->c[1]->c[0]; t->tag=1; t->key=v; t->sum=v*t->siz;
	t->lmx=t->rmx=t->smx=max(v,v*t->siz); t->push_down();
	root->c[1]->push_up(); root->push_up();
}

void reverse(int pos,int tot){
	node * q=find_rank(pos-1,root);
	node * qq=find_rank(pos+tot,root);
    splay (q,root); splay (qq,root->c[1]);
	node * t=root->c[1]->c[0]; t->rev^=1; t->push_down();
	t->fa->push_up(); t->fa->fa->push_up();
}

int querysum(int pos,int tot){
	node * q=find_rank(pos-1,root);
	node * qq=find_rank(pos+tot,root);
    splay (q,root); splay (qq,root->c[1]);
	return root->c[1]->c[0]->sum;
}
int maxsum(){ 
    node * q=find_rank(1,root);
	node * qq=find_rank(root->siz,root);
    splay (q,root); splay (qq,root->c[1]);
	return root->c[1]->c[0]->smx;
}
int n,m,data[N];

int main(){
	n=read(),m=read(); int i,pos,tot,j;
	for (i=1;i<=n;i++) data[i]=i;
	root=build(data-1,n+2);
	for (i=1;i<=m;i++) {
		pos=read(); tot=read();
		reverse(pos+1,tot-pos+1);
	}
	check(root);
	for (i=1;i<=n;i++) printf("%d ",___write[i]);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的权值为 $a_x+a_y$,求所有满足条件的路径中,所有点的权值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的权值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (树形dp) $O(n^3)$ 我们可以先将所有点的权值求出来,然后将其看作是一个有权值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的权值和的最小值。 我们可以使用树形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵树,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的权值和的最小值,然后再将这个值加上当前节点的权值,就可以得到从根节点到当前节点的路径中,所有点的权值和的最小值。 时间复杂度 树形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的权值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的权值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值