BZOJ4825 [Hnoi2017]单旋 【线段树】

题目链接

BZOJ4825

题解

手模一下操作,会发现一些很优美的性质:
每次旋到根,只有其子树深度不变,剩余点深度\(+1\)
每次旋到根,【最小值为例】右儿子接到其父亲的左儿子,其余点形态不改变,然后将该点接到根之上,原根变为其右儿子
每次插入,都是插入到其前驱后继深度较大的那一个点之下

所以我们很容易模拟出树的形态,同时用线段树维护离散化后各权值的深度

#include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
	return out * flag;
}
set<int> S;
struct Que{
	int opt,v;
}Q[maxn];
int fa[maxn],ls[maxn],rs[maxn];
int b[maxn],tot,m;
int getn(int x){return lower_bound(b + 1,b + 1 + tot,x) - b;}
int val[maxn << 2],tag[maxn << 2];
void pd(int u){
	if (tag[u]){
		val[u << 1] += tag[u]; tag[u << 1] += tag[u];
		val[u << 1 | 1] += tag[u]; tag[u << 1 | 1] += tag[u];
		tag[u] = 0;
	}
}
void change(int u,int l,int r,int pos,int v){
	if (l == r) {val[u] = v; return;}
	pd(u);
	int mid = l + r >> 1;
	if (mid >= pos) change(u << 1,l,mid,pos,v);
	else change(u << 1 | 1,mid + 1,r,pos,v);
}
void modify(int u,int l,int r,int L,int R,int v){
	if (l >= L && r <= R) {val[u] += v; tag[u] += v; return;}
	pd(u);
	int mid = l + r >> 1;
	if (mid >= L) modify(u << 1,l,mid,L,R,v);
	if (mid < R) modify(u << 1 | 1,mid + 1,r,L,R,v);
}
int query(int u,int l,int r,int pos){
	if (l == r) return val[u];
	pd(u);
	int mid = l + r >> 1;
	if (mid >= pos) return query(u << 1,l,mid,pos);
	return query(u << 1 | 1,mid + 1,r,pos);
}
void print(int u,int l,int r){
	if (l == r) printf("%d ",val[u]);
	else {
		pd(u);
		int mid = l + r >> 1;
		print(u << 1,l,mid);
		print(u << 1 | 1,mid + 1,r);
	}
}
int main(){
	m = read(); int n = 0;
	REP(i,m){
		Q[i].opt = read();
		if (Q[i].opt == 1) b[++n] = Q[i].v = read();
	}
	sort(b + 1,b + 1 + n); tot = 1;
	for (int i = 2; i <= n; i++) if (b[i] != b[tot]) b[++tot] = b[i];
	S.insert(0); S.insert(tot + 10);
	int pre,nxt,d1,d2,u,v,rt;
	for (int i = 1; i <= m; i++){
		if (Q[i].opt == 1){
			u = getn(Q[i].v);
			S.insert(u);
			pre = *--S.find(u);
			nxt = *++S.find(u);
			if (pre == 0 && nxt == tot + 10)
				change(1,1,tot,u,1),rt = u;
			else if (pre == 0){
				d1 = query(1,1,tot,nxt);
				change(1,1,tot,u,d1 + 1);
				fa[u] = nxt; ls[nxt] = u;
			}
			else if (nxt == tot + 10){
				d2 = query(1,1,tot,pre);
				change(1,1,tot,u,d2 + 1);
				fa[u] = pre; rs[pre] = u;
			}
			else {
				d1 = query(1,1,tot,nxt);
				d2 = query(1,1,tot,pre);
				if (d1 > d2){
					change(1,1,tot,u,d1 + 1);
					fa[u] = nxt; ls[nxt] = u;
				}
				else {
					change(1,1,tot,u,d2 + 1);
					fa[u] = pre; rs[pre] = u;
				}
			}
			ls[u] = rs[u] = 0;
			printf("%d\n",query(1,1,tot,u));
		}
		else if (Q[i].opt == 2){
			u = *++S.find(0);
			printf("%d\n",query(1,1,tot,u));
			if (!fa[u]) continue;
			modify(1,1,tot,fa[u],tot,1);
			change(1,1,tot,u,1);
			v = fa[u];
			ls[v] = rs[u]; if (rs[u]) fa[rs[u]] = v;
			rs[u] = rt; fa[rt] = u; fa[u] = 0;
			rt = u;
		}
		else if (Q[i].opt == 3){
			u = *--S.find(tot + 10);
			printf("%d\n",query(1,1,tot,u));
			if (!fa[u]) continue;
			modify(1,1,tot,1,fa[u],1);
			change(1,1,tot,u,1);
			v = fa[u];
			rs[v] = ls[u]; if (ls[u]) fa[ls[u]] = v;
			ls[u] = rt; fa[rt] = u; fa[u] = 0;
			rt = u;
		}
		else if (Q[i].opt == 4){
			u = *++S.find(0);
			printf("%d\n",query(1,1,tot,u));
			if (fa[u]) modify(1,1,tot,u,fa[u] - 1,-1);
			else modify(1,1,tot,1,tot,-1);
			if (v = fa[u]){
				ls[v] = rs[u]; if (rs[u]) fa[rs[u]] = v;
				rs[u] = 0;
			}
			else if (rs[u]) fa[rs[u]] = 0,rt = rs[u],rs[u] = 0;
			S.erase(u);
		}
		else if (Q[i].opt == 5){
			u = *--S.find(tot + 10);
			printf("%d\n",query(1,1,tot,u));
			if (fa[u]) modify(1,1,tot,fa[u] + 1,u,-1);
			else modify(1,1,tot,1,tot,-1);
			if (v = fa[u]){
				rs[v] = ls[u]; if (ls[u]) fa[ls[u]] = v;
				ls[u] = 0;
			}
			else if (ls[u]) fa[ls[u]] = 0,rt = ls[u],ls[u] = 0;
			S.erase(u);
		}
		/*printf("rt = %d\n",b[rt]);
		for (int i = 1; i <= tot; i++){
			printf("node%d  fa = %d  ,ls = %d  rs = %d\n",b[i],b[fa[i]],b[ls[i]],b[rs[i]]);
		}
		print(1,1,tot); puts("");*/
	}
	return 0;
}

posted @ 2018-05-10 07:38  Mychael  阅读(86)  评论(0编辑  收藏