zoukankan      html  css  js  c++  java
  • _bzoj3223 Tyvj 1729 文艺平衡树【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223

    裸的,打个标记。

    #include <cstdio>
    #include <algorithm>
    
    const int maxn = 100005;
    
    int n, m, stk[maxn], top, t1, t2;
    int fa[maxn], ch[maxn][2], root, siz[maxn];
    char rev[maxn];
    
    inline void pushdown(int x) {
    	if (rev[x]) {
    		rev[x] = 0;
    		rev[ch[x][0]] ^= 1;
    		rev[ch[x][1]] ^= 1;
    		std::swap(ch[x][0], ch[x][1]);
    	}
    }
    inline void pushup(int x) {
    	siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
    }
    inline void rotate(int x) {
    	int y = fa[x];
    	if (y == ch[fa[y]][0]) {
    		ch[fa[y]][0] = x;
    	}
    	else {
    		ch[fa[y]][1] = x;
    	}
    	fa[x] = fa[y];
    	int dir = x == ch[y][1];
    	ch[y][dir] = ch[x][dir ^ 1];
    	fa[ch[x][dir ^ 1]] = y;
    	ch[x][dir ^ 1] = y;
    	fa[y] = x;
    	pushup(y);
    	pushup(x);
    }
    inline void splay(int x, int rt) {
    	int p;
    	top = 0;
    	for (int i = x; i != rt; i = fa[i]) {
    		stk[top++] = i;
    	}
    	for (int i = top - 1; ~i; --i) {
    		pushdown(stk[i]);
    	}
    	while (fa[x] != rt) {
    		p = fa[x];
    		if (fa[p] == rt) {
    			rotate(x);
    		}
    		else {
    			if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) {
    				rotate(x);
    			}
    			else {
    				rotate(p);
    			}
    			rotate(x);
    		}
    	}
    	if (!rt) {
    		root = x;
    	}
    }
    inline int kth(int k) {
    	int x = root;
    	pushdown(x);
    	while (k != siz[ch[x][0]] + 1) {
    		if (k <= siz[ch[x][0]]) {
    			x = ch[x][0];
    		}
    		else {
    			k -= siz[ch[x][0]] + 1;
    			x = ch[x][1];
    		}
    		pushdown(x);
    	}
    	return x;
    }
    int make_tree(int left, int right) {
    	if (left > right) {
    		return 0;
    	}
    	int rt = (left + right) >> 1;
    	ch[rt][0] = make_tree(left, rt - 1);
    	fa[ch[rt][0]] = rt;
    	ch[rt][1] = make_tree(rt + 1, right);
    	fa[ch[rt][1]] = rt;
    	pushup(rt);
    	return rt;
    }
    void print(int r) {
    	if (!r) {
    		return;
    	}
    	pushdown(r);
    	print(ch[r][0]);
    	if (r != 1 && r != n + 2) {
    		printf("%d ", r - 1);
    	}
    	print(ch[r][1]);
    }
    
    int main(void) {
    	//freopen("in.txt", "r", stdin);
    	scanf("%d%d", &n, &m);
    	root = make_tree(1, n + 2);
    	while (m--) {
    		scanf("%d%d", &t1, &t2);
    		++t1;
    		++t2;
    		t1 = kth(t1 - 1);
    		t2 = kth(t2 + 1);
    		splay(t1, 0);
    		splay(t2, root);
    		rev[ch[ch[root][1]][0]] ^= 1;
    	}
    	print(root);
    	return 0;
    }
    

      

  • 相关阅读:
    【图】max51开发板(手工焊接)
    【图】max51开发板3D效果图
    【图】AT89S52原理页
    频率 时间 单位转换
    [AD 技巧]Altium Designer元件换层
    Windows 的承载网络设置方法
    Windows 定时自动开/关机
    Windows 操作系统快捷键
    转义符 与 转义字符
    keil 中的快捷键
  • 原文地址:https://www.cnblogs.com/ciao-sora/p/6184473.html
Copyright © 2011-2022 走看看