sgu 187 分类: sgu 2015-04-22 20:39 29人阅读 评论(0) 收藏


裸的区间翻转,
我把平衡树模板贴了一部分,
然后空间调整了一下,
就过了。。。



#include<map>
#include<queue>
#include<stack>
#include<utility>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
#define Mp(x,y) std::make_pair(x,y)
#define L(x)   b[x].c[0]
#define R(x)   b[x].c[1]
#define Fa(x)  b[x].fa
#define Clear(x) b[x] = emp

const int MAXN = 130005, SIZE = 130005 , INF = (1LL<<31)-1;

int n , m;
int a[MAXN] = {0};
struct TreeNode
{
    int c[2],fa,cnt;
    int val,rev;
}b[SIZE] = {0}, emp = {0};
int root, size ;

void PushDown(int x)
{
    if(b[x].rev)
    {
        std::swap(L(x),R(x));
        if(L(x)) b[L(x)].rev ^= 1;
        if(R(x)) b[R(x)].rev ^= 1;
        b[x].rev = 0;
    }
}
void Update(int x)
{
    b[x].cnt = b[L(x)].cnt + b[R(x)].cnt + 1;
}
void Rotate(int x,int &k)
{
    int y = Fa(x) , z = Fa(y);
    int i = (R(y) == x) ,j = i^1 , fi = (R(z) == y);

    PushDown(y), PushDown(x);

    if(y != k) b[z].c[fi] = x;else  k = x;

    Fa(x) = z,Fa(y) = x ,Fa(b[x].c[j]) = y;
    b[y].c[i] = b[x].c[j], b[x].c[j] = y ;

    Update(y), Update(x);
}
void Splay(int x,int &k)
{
    PushDown(x);
    while(x != k)
    {
        int y = Fa(x), z = Fa(y);
        if(y != k)
        {
            if((Fa(y)==R(z))^(Fa(x)==R(y)))Rotate(x,k);
            else   Rotate(y,k);
        }
        Rotate(x,k);
    }
    Update(x);
    return;
}
int Find(int p)
{
    int pos = root;

    while(1)
    {
        PushDown(pos);
        int lc = b[L(pos)].cnt + 1;
        if(p == lc) break;
        else if(p < lc) pos = L(pos);
        else { p -= lc ,pos = R(pos);}
    }
    return pos;
}
int GetInveral(int l,int r)
{
    if(l == 1 && r == size)
      return root;
    else if(l == 1)
    {
     Splay(Find(r+1),root);
     return L(root);    
    }
    else if(r == size)
    {
     Splay(Find(l-1),root);
     return R(root);    
    }
    else
    {
     Splay(Find(l-1),root);
     Splay(Find(r+1),R(root));
     return L(R(root));
    }
}

void Reverse(int l,int r)
{
    int pos = GetInveral(l,r);
    b[pos].rev ^= 1;
}
int GetVal(int x)
{
    return b[Find(x)].val;
}
int BuildTree(int ll,int rr,int fa)
{
    int ret = (ll + rr)>>1;

    b[ret].fa = fa , b[ret].val = a[ret];
    if(ll < ret) b[ret].c[0] = BuildTree(ll,ret-1,ret);
    if(ret < rr) b[ret].c[1] = BuildTree(ret+1,rr,ret);
    Update(ret);

    return ret;
}
int read()
{
    int x = 0, f = 1;char c = getchar();
    while(c < '0' || c > '9') {if(c == '-')f = -1; c = getchar();}
    while(c >= '0' && c <= '9'){x = (x<<3) + (x<<1) + (c-'0'); c = getchar();}
    return x * f;
}
void write(int x)
{
    if(x < 0) putchar('-'),x = -x;
    static char s[10];int sl = 0;
    while(x) s[++sl] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[sl--]);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("sgu187.in","r",stdin);
    freopen("sgu187.out","w",stdout);
#endif

    n = read(), m = read();

    for(int i = 1; i <= n; i++)  a[i] = i;
    root = BuildTree(1,n,0); size = n;

    for(int i = 1; i <= m; i++)
    {
        int a = read(), b = read();

        Reverse(a, b);
    }

    for(int i = 1; i <= n; i++)
      write(GetVal(i)), putchar(' ');

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-04-22 20:39  <Dash>  阅读(192)  评论(0编辑  收藏  举报