速度居然进前十了...第八...

splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转

----------------------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
 
#define rep(i, n) for(int i = 0; i < n; ++i)
#define clr(x, c) memset(x, c, sizeof(x))
  
using namespace std;
 
const int maxn = 100000 + 5;
 
int n;
 
struct node *null, *pt;
struct node {
    node* ch[2];
    int v, s;
    bool flip;
    node(int _v = 0) : v(_v), s(1), flip(0) {
        ch[0] = ch[1] = null;
    }
    inline int cmp(int k) const {
        k -= ch[0]->s;
        if(k == 1) return -1;
        return k <= 0 ? 0 : 1;
    }
    inline void maintain() {
        s = ch[0]->s + ch[1]->s + 1;
    }
    inline void pushdown() {
        if(flip) {
            flip = 0;
            swap(ch[0], ch[1]);
            ch[0]->flip ^= 1;
            ch[1]->flip ^= 1;
        }
    }
     
    void *operator new (size_t) { return pt++; }
};
  
node* root;
node N[maxn], Null;
  
void rotate(node* &o, int d) {
    node* k = o->ch[d^1];
    o->ch[d^1] = k->ch[d];
    k->ch[d] = o;
    o->maintain(); k->maintain();
    o = k;
}
  
void splay(node* &o, int k) {
    o->pushdown();
    int d = o->cmp(k);
    if(d == 1) k -= o->ch[0]->s + 1;
    if(d != -1) {
        node* p = o->ch[d];
        p->pushdown();
        int d2 = p->cmp(k);
        int k2 = d2 ? k - p->ch[0]->s - 1 : k;
        if(d2 != -1) {
            splay(p->ch[d2], k2);
            d == d2 ? rotate(o, d^1) : rotate(o->ch[d], d);
        }
        rotate(o, d^1);
    }
}
  
node* build(int l, int r) {
    if(l >= r) return null;
    int m = (l + r) >> 1;
    node* o = new node(m);
    if(l < m) o->ch[0] = build(l, m);
    if(m + 1 < r) o->ch[1] = build(m + 1, r);
    o->maintain();
    return o;
}
  
void init() { pt = N; null = &Null; null->s = 0; root = build(0, n + 2); }
  
void dfs(node* o) {
    if(o == null) return;
    o->pushdown();
    dfs(o->ch[0]);
    if(o->v >= 1 && o->v <= n) printf("%d ",o->v);
    dfs(o->ch[1]);
}
  
inline int read() {
int ans = 0, f = 1;
char c = getchar();
while(!isdigit(c)) {
if(c == '-') f = -1;
c = getchar();
}
while(isdigit(c)) {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
 
int main() {
     
    n = read();
    int m = read();
    init();
    while(m--) {
    int l = read(), r = read();
        if(l == r) continue;
        splay(root, l);
        splay(root->ch[1], r + 1 - root->ch[0]->s);
        root->ch[1]->ch[0]->flip ^= 1;
    }
    dfs(root);
     
    return 0;
}

----------------------------------------------------------------------------------------

3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1675  Solved: 931
[Submit][Status][Discuss]

Description

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

Input

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

Output

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

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

Source

BZOJ 3223: Tyvj 1729 文艺平衡树(splay)的更多相关文章

  1. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  2. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  3. bzoj 3223/tyvj 1729 文艺平衡树 splay tree

    原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...

  4. BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)

    题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...

  5. BZOJ 3223 Tyvj 1729 文艺平衡树 | Splay 维护序列关系

    题解: 每次reverse(l,r) 把l-1转到根,r+1变成他的右儿子,给r+1的左儿子打个标记就是一次反转操作了 每次find和dfs输出的时候下放标记,把左儿子和右儿子换一下 记得建树的时候建 ...

  6. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  7. fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)

    题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...

  8. [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转

    题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...

  9. BZOJ 3223 Tyvj 1729 文艺平衡树(Splay)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3223 [题目大意] 给出一数列,问m次区间翻转后的结果. [题解] Splay 区间翻 ...

随机推荐

  1. poj2823_单调队列简单入门

    题目链接:http://poj.org/problem?id=2823 #include<iostream> #include<cstdio> #define M 100000 ...

  2. 汉诺塔VII(递推,模拟)

    汉诺塔VII Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. elmah - Error Logging Modules and Handlers for ASP.NET - 1 : 初体验

    elmah(英文):https://code.google.com/p/elmah/   写作思路:先看结果,然后再说原理   elmah文章基本内容如下   1.安装 2.基本使用 3.详细配置讲解 ...

  4. springMvc 支持hibernate validator

    SpringMVC 支持Hibernate Validator 发表于9个月前(2014-08-04 11:34)   阅读(1780) | 评论(0) 11人收藏此文章, 我要收藏 赞0 5月23日 ...

  5. XML编程与应用-读取XML

    实例:使用XmlTextReader类的对象读取XML文档 代码如下 using System; using System.Collections.Generic; using System.Linq ...

  6. org.openqa.selenium.SessionNotCreatedException: A new session could not be created.

    解决方案 1. 重新插拔手机. 2. 检查appium端口是否被占用,如是,杀掉占用了改端口的进程,然后重启appium. 3.

  7. eclipse使用技巧---使用正则表达式查找替换

    1,Eclipse ctrl+f 打开查找框2,选中 Regular expressions (正则表达式) 去掉/* */(eclipse)        /\*(.|[\r\n])*?\*/去掉/ ...

  8. WebApi服务

    WCF 它利用TCP.HTTP.MSMQ等传输协议构建“契约先行”的服务.WCF最初为基于SOAP的服务而设计[xml],繁琐.冗余.慢.沉重 WebApi 基于http协议,轻量级的,支持URL路由 ...

  9. 关于express4不再支持body-parser

    express的bodyParser能将表单里的数据格式化,bodyParser原是绑定在express中的,但从express4开始,不在绑定了 如果依然直接使用app.use(express.bo ...

  10. Let's Format Css Documents

    每次想参考一些好看网站的时候,打开css文档都是一行的,琢磨了下就自己写了块短短的代码,各路Java大神别笑我呀.^_^ 复制粘贴控制台的输出就好了.(瞬间觉得跟上大神的脚步了←_←) package ...