zoukankan      html  css  js  c++  java
  • 【模板】文艺平衡树(Splay) 区间翻转 BZOJ 3223

    您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 



    N,M<=100000

    Sample Output4 3 2 1 5 Hint


    Input

    第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
    接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n 

    Output

     

    输出一行n个数字,表示原始序列经过m次变换后的结果 

    Sample Input5 3 1 3 1 3 1 4
     
    Splay Tree ,和上道题一样,我们的kth 返回的序列的第k个数;
    然后中序遍历即可得到我们翻转后的序列;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#pragma GCC optimize(2)
    //#include<cctype>
    //#pragma GCC optimize("O3")
    using namespace std;
    #define maxn 100005
    #define inf 0x3f3f3f3f
    #define INF 9999999999
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    ll sqr(ll x) { return x * x; }
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    
    
    
    ll qpow(ll a, ll b, ll c) {
    	ll ans = 1;
    	a = a % c;
    	while (b) {
    		if (b % 2)ans = ans * a%c;
    		b /= 2; a = a * a%c;
    	}
    	return ans;
    }
    
    struct splay {
    	int fa, ch[2], size;
    	int lazy, rev, maxx, value;
    }Sp[maxn];
    
    int n, m, root, a[maxn];
    void pushup(int rt) {
    	Sp[rt].size = Sp[Sp[rt].ch[0]].size + Sp[Sp[rt].ch[1]].size + 1;
    	Sp[rt].maxx = max(Sp[rt].value, max(Sp[Sp[rt].ch[0]].maxx, Sp[Sp[rt].ch[1]].maxx));
    }
    
    void pushdown(int rt) {
    	if (Sp[rt].lazy) {
    		if (Sp[rt].ch[0]) {
    			Sp[Sp[rt].ch[0]].lazy += Sp[rt].lazy;
    			Sp[Sp[rt].ch[0]].maxx += Sp[rt].lazy;
    			Sp[Sp[rt].ch[0]].value += Sp[rt].lazy;
    		}
    		if (Sp[rt].ch[1]) {
    			Sp[Sp[rt].ch[1]].lazy += Sp[rt].lazy;
    			Sp[Sp[rt].ch[1]].maxx += Sp[rt].lazy;
    			Sp[Sp[rt].ch[1]].value += Sp[rt].lazy;
    		}
    		Sp[rt].lazy = 0;
    	}
    	if (Sp[rt].rev) {
    		if (Sp[rt].ch[0]) {
    			Sp[Sp[rt].ch[0]].rev ^= 1;
    			swap(Sp[Sp[rt].ch[0]].ch[0], Sp[Sp[rt].ch[0]].ch[1]);
    		}
    		if (Sp[rt].ch[1]) {
    			Sp[Sp[rt].ch[1]].rev ^= 1;
    			swap(Sp[Sp[rt].ch[1]].ch[0], Sp[Sp[rt].ch[1]].ch[1]);
    		}
    		Sp[rt].rev = 0;
    	}
    }
    
    int id(int x) {
    	return Sp[Sp[x].fa].ch[1] == x;
    }
    void link(int son, int fa, int k) {
    	Sp[son].fa = fa; Sp[fa].ch[k] = son;
    }
    
    void rotate(int  x) {
    	int y = Sp[x].fa;
    	int z = Sp[y].fa;
    	int yk = id(x);
    	int zk = id(y);
    	int b = Sp[x].ch[yk ^ 1];
    	link(b, y, yk); link(y, x, yk ^ 1);
    	link(x, z, zk);
    	pushup(y); pushup(x);
    }
    
    void SPLAY(int x, int aim) {
    	while (Sp[x].fa != aim) {
    		int y = Sp[x].fa;
    		int z = Sp[y].fa;
    		if (z != aim)id(x) == id(y) ? rotate(y) : rotate(x);
    		rotate(x);
    	}
    	if (aim == 0)root = x;
    }
    
    int kth(int k) {
    	int now = root;
    	while (1) {
    		pushdown(now);
    		int left = Sp[now].ch[0];
    		if (Sp[left].size + 1 < k) {
    			k -= Sp[left].size + 1; now = Sp[now].ch[1];
    		}
    		else if (Sp[left].size >= k)now = left;
    		else return now;
    	}
    }
    
    int build(int l, int r, int fa) {
    	if (l > r)return 0;
    	if (l == r) {
    		Sp[l].fa = fa; Sp[l].maxx = Sp[l].value = a[l];
    		Sp[l].size = 1; return l;
    	}
    	int mid = (l + r) >> 1;
    	Sp[mid].ch[0] = build(l, mid - 1, mid);
    	Sp[mid].ch[1] = build(mid + 1, r, mid);
    	Sp[mid].value = a[mid];
    	Sp[mid].fa = fa;
    	pushup(mid);
    	return mid;
    }
    
    int split(int l, int r) {
    	l = kth(l); r = kth(r + 2);
    	SPLAY(l, 0); SPLAY(r, l);
    	return Sp[Sp[root].ch[1]].ch[0];
    }
    
    void upd(int l, int  r, int v) {
    	int now = split(l, r);
    	Sp[now].lazy += v; Sp[now].maxx += v; Sp[now].value += v;
    	pushup(Sp[root].ch[1]); pushup(root);
    }
    
    void Reverse(int l, int r) {
    	int now = split(l, r);
    	Sp[now].rev ^= 1;
    	swap(Sp[now].ch[0], Sp[now].ch[1]);
    	pushup(Sp[root].ch[1]); pushup(root);
    }
    int query(int l, int  r) {
    	return Sp[split(l, r)].maxx;
    }
    
    void dfs(int rt) {
    	pushdown(rt);
    	if (Sp[rt].ch[0])dfs(Sp[rt].ch[0]);
    	if (Sp[rt].value != inf && Sp[rt].value != -inf) {
    		cout << Sp[rt].value << ' ';
    	}
    	if (Sp[rt].ch[1])dfs(Sp[rt].ch[1]);
    }
    
    int main()
    {
    	//ios::sync_with_stdio(0);
    	rdint(n); rdint(m);
    	for (int i = 2; i <= n + 1; i++)a[i] = i - 1;
    	a[1] = -inf; a[n + 2] = inf;
    	root = build(1, n + 2, 0);
    	while (m--) {
    		int l, r; rdint(l); rdint(r);
    		Reverse(l, r);
    	}
    	dfs(root); cout << endl;
        return 0;
    }
    
    EPFL - Fighting
  • 相关阅读:
    SDUTOJ 2775 小P的故事——奇妙的饭卡
    STL vector的构造函数和析构函数(2)
    Spark调研笔记第6篇
    【精】iOS GCD 具体解释
    爆头
    uva 624 CD
    一站式学习Wireshark(转载)
    TCP的状态 (SYN, FIN, ACK, PSH, RST, URG)
    loadrunner两个函数:取参数长度和时间戳函数
    自行控制loadrunner的socket协议性能测试 (转)
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10073020.html
Copyright © 2011-2022 走看看