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. WebWork2和Spring MVC Framework的比较

    http://daihaixiang.blog.163.com/blog/static/3830134200711411515336/ WebWork2和Spring MVC Framework的比较 ...

  2. 20151224jquery学习笔记---cookie插件

    hello,祝自己平安夜快乐. Cookie 是网站用来在客户端保存识别用户的一种小文件.一般来用库可以保存用户登录信息.购物数据信息等一系列微小信息.一. 使用 cookie 插件官方网站: htt ...

  3. JavaScript之call()&apply()

    场景一:定义了一个类A,给它一个getName的方法:定义了一个类B,给它一个setName的方法:之前A只需要获取它的Name,B也只需要设置它的Name,但现在有新的需求,A和B都需要设置和获取他 ...

  4. Jquery Ajax调用aspx页面实例

    目前,我会的几种asp.net界面与后台代码交互方式有几种: 1.webform+服务器控件交互: 2.webform+jquery+ajax+一般处理程序交互: 3.webform+jquery+a ...

  5. PIMP模式的理解

    看了[C++程序设计技巧]Pimpl机制 之后,想了半天才理解    // MyClass.h 2: class MyClassImpl; // forward declaration 3: clas ...

  6. ASP.NET服务端基本控件介绍

    ASP.NET服务端基本控件介绍 大概分为三种控件: HTML控件,ASP.NET把HTML控件当成普通字符串渲染到浏览器端,不去检查正确性,无法在服务端进行处理ASP.NET服务端控件,经过ASP. ...

  7. LAMP 环境 快速安装

    (一)安装Apache 1.下载安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 yum install zlib-devel -y wget http://m ...

  8. 【转】oracle null

    转自:oracle的null和空字符串'' 1.oracle 将 空字符串即''当成null 2.null 与任何值做逻辑运算得结果都为 false,包括和null本身 3.用 is null 判断时 ...

  9. PHP 面向对象之自定义类

    所谓面向对象就是什么时候什么东西做什么,我们设计类的时候需要想的就是怎么做的内容,那么怎么样的一个类才算是符合OOP的思想呢,答案是:这个类写好之后,在使用的过程中,能准确的代表一个事物,在书写的时候 ...

  10. html meta标签用法详细介绍

    meta是html语言head区的一个辅助性标签. 在页面中都有类似这样的html代码: <head> <meta http-equiv="content-Type&quo ...