sgu 187


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



#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值