【HDOJ】3487 Play with Chain
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的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- sqlserver临时表排序问题
在2005和2008中,如果将有序的记录插入临时表,则从临时表查看出来的记录是有序的(不使用Order by也是有序状态) 但从2012开始,即使插入的记录集是有序的,查看出来的结果变得无序了,需要依 ...
- PHP和MYSQL的编码问题
http://blog.csdn.net/martinkro/article/details/5352474 1 MYSQL中的字符集概念 Mysql的字符集里有两个概念,一个是"Char ...
- O-C相关05:方法的封装.
前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...
- redis研究之watch
具体的看注释,代码如下: public static void TransTest() { RedisClient client1 = new RedisClient(host, port); Red ...
- java中的JSON对象的使用
申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...
- MYSQL命令cmd操作
今天我们就来看一下数据库的各种命令,以下命令全部是从CMD命令窗口下的命令行输入指令,首先如果如果输入mysql,系统提示“mysql不是内部命 令或外部命令.那么这其实是环境变量没有设置好的原因,例 ...
- 记录一次mount问题
linux centos 6.5 _64 oracle 11.2g 今天接到一个客户电话说重启了服务器,数据库没有重启来,看了数据库的报错 没有找到control文件的路径,数据库启动到了 unmo ...
- find命令使用, -exec xargs
find [path] [expression] 例如:find /home -name \*.o -exec rm '{}' \; find: 实时精确,支持众多查找标准,遍历指定目录中 ...
- /etc/rc.local ; /etc/init.d ;/etc/profile;/etc/bashrc;~/.bash_profile;~/.bashrc;~/.bash_logout
1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里. 把脚本程序写在/etc/rc.d/init.d/目录下也可以 在完成 run level 3 ...
- 4MLinux7.0 服务器配置详解 别名TheSSS
TheSSS download 特性:thttp,php5.5.1,mysql,vsftp,proxy,firewall,带rpm管理器.更新频繁. 官方帮助文件:View (新窗口打开) 发现国内4 ...