http://acm.hdu.edu.cn/showproblem.php?pid=3487

题意:有两种操作:1、Flip l r ,把 l 到 r 这段区间 reverse。2、Cut a b c ,把 a 到 b 这段区间切掉,再把这段区间接到切掉后的第 c 个数的后面。

思路:做完了上一道变态题目,做这道题目如鱼得水。Cut的时候就是把a 到 b 放到keytree的位置,记录一下当前keytree的值,然后切掉,再把切掉后的第 c 个数转到 root 的位置,再把这个记录的值重新连接回去。要注意当全部询问结束了,要把所有的rev标记pushdown。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 300100
#define INF 0x3f3f3f3f
#define lson ch[x][0]
#define rson ch[x][1]
#define keytree ch[ch[root][1]][0] struct SplayTree
{
int num[N], rev[N], ch[N][], fa[N], val[N], sz[N];
int n, root, cnt, ans[N], tol; void PushDown(int x)
{
if(rev[x]) {
swap(lson, rson);
if(lson) rev[lson] ^= ;
if(rson) rev[rson] ^= ;
rev[x] = ;
}
} void PushUp(int x)
{
sz[x] = sz[lson] + sz[rson] + ;
} int NewNode(int w, int f, int kind)
{
int x = ++cnt;
ch[x][] = ch[x][] = rev[x] = ;
sz[x] = ; val[x] = w; fa[x] = f;
ch[f][kind] = cnt;
return x;
} void Build(int l, int r, int &x, int f, int kind)
{
if(l > r) return ;
int m = (l + r) >> ;
x = NewNode(num[m], f, kind);
Build(l, m - , ch[x][], x, );
Build(m + , r, ch[x][], x, );
PushUp(x);
} void Init()
{
root = cnt = tol = ;
rev[] = val[] = fa[] = sz[] = ch[][] = ch[][] = ;
root = NewNode(, , );
ch[root][] = NewNode(, root, );
sz[root] = ;
Build(, n, keytree, ch[root][], );
PushUp(ch[root][]); PushUp(root);
} void Rotate(int x, int kind)
{
int y = fa[x], z = fa[y];
PushDown(y); PushDown(x);
ch[y][!kind] = ch[x][kind];
if(ch[y][!kind]) fa[ch[y][!kind]] = y;
if(z) {
if(y == ch[z][]) ch[z][] = x;
else ch[z][] = x;
}
fa[x] = z; fa[y] = x;
ch[x][kind] = y;
PushUp(y);
} void Splay(int x, int goal)
{
while(fa[x] != goal) {
int y = fa[x], z = fa[y];
PushDown(z); PushDown(y); PushDown(x);
int kind1 = x == ch[y][];
int kind2 = y == ch[z][];
if(z == goal) {
Rotate(x, kind1);
} else {
if(kind1 == kind2) {
Rotate(y, kind1);
} else {
Rotate(x, kind1);
}
Rotate(x, kind2);
}
}
PushUp(x);
if(goal == ) root = x;
} void RTO(int k, int goal)
{
int x = root;
PushDown(x);
while(k != sz[lson] + ) {
if(k <= sz[lson]) x = lson;
else k -= sz[lson] + , x = rson;
PushDown(x);
}
Splay(x, goal);
} void Cut(int l, int r, int c)
{
RTO(l, );
// Debug();
RTO(r + , root);
int tmp = keytree;
keytree = ;
// Debug();
RTO(c + , );
// Debug();
RTO(c + , root);
fa[tmp] = ch[root][];
keytree = tmp;
} void Reverse(int l, int r)
{
RTO(l, ); RTO(r + , root);
rev[keytree] ^= ;
} void Down(int x)
{
PushDown(x);
if(lson) Down(lson);
if(rson) Down(rson);
} void Travel(int x)
{
if(lson) Travel(lson);
ans[tol++] = val[x];
if(rson) Travel(rson);
} void Debug()
{
Down(root);
Travel(root);
for(int i = ; i <= n; i++) {
if(i == n) printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
}
}
}spy; int main()
{
int n, m;
while(scanf("%d%d", &n, &m)) {
if(n < || m < ) break;
spy.n = n;
for(int i = ; i <= n; i++) spy.num[i] = i;
spy.Init();
while(m--) {
char s[];
int a, b, c;
scanf("%s", s);
if(s[] == 'C') {
scanf("%d%d%d", &a, &b, &c);
spy.Cut(a, b, c);
} else {
scanf("%d%d", &a, &b);
spy.Reverse(a, b);
}
}
spy.Debug();
}
return ;
}

HDU 3487:Play with Chain(Splay)的更多相关文章

  1. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  2. HDU 5795:A Simple Nim(博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description   Two players take t ...

  3. HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...

  4. HDU 3183:A Magic Lamp(RMQ)

    http://acm.hdu.edu.cn/showproblem.php?pid=3183 题意:给出一个数,可以删除掉其中m个字符,要使得最后的数字最小,输出最后的数字(忽略前导零). 思路:设数 ...

  5. HDU 6011:Lotus and Characters(贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意:共有n种字符,每种字符有一个val和一个cnt,代表这个字符的价值和数量.可以制造的总价值是:第一个 ...

  6. HDU 5968:异或密码(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5968 题意:中文题意. 思路:一开始不会做,后来发现数据范围很小,而且那个数要是连续的,所以可能把所有情况枚举出 ...

  7. HDU 2830:Matrix Swapping II(思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=2830 题意:-- 思路:对于每一列,它是固定的,用dp[][]处理出连续的长度.例如: 假设我们扫第四列的时候, ...

  8. HDU 5113:Black And White(DFS)

    题目链接 题意 给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同. 思路 以为是构造题,结果是爆搜.对于每一 ...

  9. HDU 1231:最大连续子序列(DP)

    pid=1231">最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. 发现EF中字段错误

    在使用EF时,报错: 对一个或多个实体的验证失败.有关详细信息,请参见“EntityValidationErrors”属性 添加一个验证方法: 代码: using System; using Syst ...

  2. linux的5个查找命令_转

    转自:http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 在Linux中 ...

  3. 使用Proj.Net创建空间参考

    在RGEOS项目中,投影变换是通过Proj.Net来实现的. 支持的投影主要包括AlbersProjection.TransverseMercator.Mercator.Krovak.Lambert ...

  4. jsp中${param.user}不解析,原样输出。

    没加<%@ page isELIgnored="false"%>

  5. JAVA的String 类

    String类 1.String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下: String s = “abc”; s ...

  6. Xcode 遇到 App Transport Security has blocked a cleartext HTTP 错误

    今天用Xcode  创建新项目用到 URL 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http://) re ...

  7. Swift游戏实战-跑酷熊猫 05 踩踏平台是怎么炼成的

    这节内容我们一起学习下随机长度的踩踏平台的原理是怎么样的. 要点: 平台的组成 我们的平台由3部分组成 左: 中: 右: 其中中间部分是可以无缝衔接的,下面就是两个中间部分衔接在一起 要任何长度的平台 ...

  8. 随机删除数据库N条记录

    delete from  table where newsID in(select top 50 newsID from  table order by newid())

  9. 20145207《Java程序设计》第6周学习总结

    教材学习内容总结 一.输入/输出 InputStream与Outputstream • 串流设计的概念 从应用程序角度看,将数据从来源取出,可以使用输入串流,将数据写入目的地,可以使用输出串流:在Ja ...

  10. 封装pdo单例模式类

    <?php /** * MyPDO * @author Jason.Wei <jasonwei06@hotmail.com> * @license http://www.sunblo ...