Splay入门题目,区间翻转,区间分割。

 /*  */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define grandlson ch[ch[root][1]][0] const int maxn = 3e5+;
int pre[maxn], ch[maxn][], root, tot;
int key[maxn], s[maxn], rev[maxn];
int stk[maxn], top;
int n, m, cnt; void newNode(int& r, int fa, int k) {
if (top)
r = stk[top--];
else
r = ++tot;
key[r] = k;
pre[r] = fa;
rev[r] = ;
ch[r][] = ch[r][] = ;
s[r] = ;
} void PushUp(int r) {
s[r] = s[ch[r][]] + s[ch[r][]] + ;
} void UpdateRev(int rt) {
if (rt == ) return ;
swap(ch[rt][], ch[rt][]);
rev[rt] ^= ;
} void PushDown(int rt) {
if (rev[rt]) {
UpdateRev(ch[rt][]);
UpdateRev(ch[rt][]);
rev[rt] = ;
}
} void Build(int& rt, int l, int r, int fa) {
if (l > r) return ; int mid = (l + r) >> ; newNode(rt, fa, mid);
Build(ch[rt][], l, mid-, rt);
Build(ch[rt][], mid+, r, rt);
PushUp(rt);
} void inorder(int rt) {
if (rt == ) return ;
inorder(ch[rt][]);
printf("s = %d, key = %d\n", s[rt], key[rt]);
inorder(ch[rt][]);
} void init() {
root = tot = top = ;
ch[][] = ch[][] = s[] = key[] = pre[] = rev[] = ;
newNode(root, , -);
newNode(ch[root][], root, -);
Build(grandlson, , n, ch[root][]);
PushUp(ch[root][]);
PushUp(root);
#ifndef ONLINE_JUDGE
inorder(root);
#endif
} void Rotate(int x, int d) {
int y = pre[x]; PushDown(y);
PushDown(x);
ch[y][d^] = ch[x][d];
pre[ch[x][d]] = y;
if (pre[y])
ch[pre[y]][ch[pre[y]][]==y] = x;
pre[x] = pre[y];
pre[y] = x;
ch[x][d] = y;
PushUp(y);
} void Splay(int r, int goal) {
PushDown(r);
while (pre[r] != goal) {
if (pre[pre[r]] == goal) {
PushDown(pre[r]);
PushDown(r);
Rotate(r, ch[pre[r]][]==r);
} else {
PushDown(pre[pre[r]]);
PushDown(pre[r]);
PushDown(r);
int y = pre[r];
int d = ch[pre[y]][]==y;
if (ch[y][d] == r) {
Rotate(r, d^);
Rotate(r, d);
} else {
Rotate(y, d);
Rotate(r, d);
}
}
}
PushUp(r);
if (goal == )
root = r;
} int kth(int r, int k) {
PushDown(r);
int t = s[ch[r][]] + ; if (t == k)
return r;
else if (k < t)
return kth(ch[r][], k);
else
return kth(ch[r][], k-t);
} void cut(int l, int r, int c) {
Splay(kth(root, l), );
Splay(kth(root, r+), root);
int tmp = grandlson;
grandlson = ;
PushUp(ch[root][]);
PushUp(root);
Splay(kth(root, c+), );
Splay(kth(root, c+), root);
grandlson = tmp;
pre[tmp] = ch[root][];
PushUp(ch[root][]);
PushUp(root);
} void flip(int l, int r) {
Splay(kth(root, l), );
Splay(kth(root, r+), root);
UpdateRev(grandlson);
PushUp(ch[root][]);
PushUp(root);
} void print(int rt) {
if (!rt) return ; PushDown(rt);
print(ch[rt][]);
if (cnt>= && cnt<=n) {
if (cnt < n)
printf("%d ", key[rt]);
else
printf("%d\n", key[rt]);
}
++cnt;
print(ch[rt][]);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif char cmd[];
int a, b, c; while (scanf("%d %d", &n, &m) != EOF) {
if (n< && m<)
break;
init();
while (m--) {
scanf("%s %d %d", cmd, &a, &b);
if (cmd[] == 'C') {
scanf("%d", &c);
cut(a, b, c);
} else {
flip(a, b);
}
}
cnt = ;
print(root);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】3487 Play with Chain的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. .Net之美读书系列(一):委托与事件

    开启新的读书之旅,这次读的书为<.Net之美:.Net关键技术深入解析>. 我是选择性阅读的,把一些自己觉得容易忘记的,或者比较重要的知识点记录下来,以便以后能方便呢查阅. 尊重书本原作者 ...

  2. Log4Net详细配置

    关于Log4Net配置主要分几步 第一步:下载log4net.dll(log4net官网:http://logging.apache.org/log4net/download_log4net.cgi) ...

  3. Saltstack安装配置(一)

    一.服务端和客户端安装 1.下载epel源 http://mirrors.zju.edu.cn/epel/6/ #wget http://mirrors.zju.edu.cn/epel/6/x86_6 ...

  4. ios 可变参数(va_list,va_start,va_end)

    例如:UIAlertView的init方法中的otherButtonTitles:(NSString *)otherButtonTitles, ...等多个可变参数. ios实现传递不定长的多个参数的 ...

  5. DOS命令教学之详解批处理

    批处理文件是由一个或一个以上的DOS命令及可执行命令组成的带有扩展名.BAT的文件.当用户以批处理文件名为命令时,DOS会自动依次执行文件中的命令.批处理文件的特点是一次建立可多次执行.下面,寻修网h ...

  6. spring boot 配置文件提示自定义配置属性

    1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. C++ Union妙用(将列表初始化用于数组元素)

    Union是个不被注意的关键字,意为联合体,这是个诡异的名字.若不是为了继承C语言,它也不会出现在C++中(虽说,union在C++中得到了扩充,完成了接近类的功能).它的作用主要是节省内存空间,在嵌 ...

  8. C++ 数组的地址问题学习随笔

    二维数组额地址问题学习,本文学习内容参考:http://blog.csdn.net/wwdlk/article/details/6322439 #include<iostream> usi ...

  9. ASP.NET获取上传图片的大小

    1.采用客户端javascript可以取得图片大小 <input id="FileUpload" type="file" size="30&qu ...

  10. 【转】Java编程之字符集问题研究

    发现这是对字集说得最明了的一篇文章了. 转发自:http://tomcat-oracle.iteye.com/blog/2037160 1. 概述 本文主要包括以下几个方面:编码基本知识,java,系 ...