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. 20151124 Jquery UI form 表单变成dialog

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. ACM——A + B Problem (1)

    A + B Problem (1) 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:5907            测试通过:151 ...

  3. WinEdit7 破解方法

    最近遇到了winEdit7 超过试用期的问题,找了下解决方案. 这个靠谱,通过在每次退出时自动删除注册表信息,达到无限试用的目的: https://lttt.blog.ustc.edu.cn/2012 ...

  4. 安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常)

    安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常) 刚安装完后用plSql登录正常. 在dos命令行下 输入  sqlplus 用户 ...

  5. java新手笔记20 抽象类模板(letter)

    1.抽象类 package com.yfs.javase; //信模板 public abstract class Templater { public abstract String toName( ...

  6. 暑假集训(1)第一弹 -----士兵队列训练问题(Hdu1276)

    Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报 ...

  7. 正则表达式(.NET)

    元字符: \b  单词的开头或结尾,匹配一个位置 .    匹配除换行符以外的任意字符 \d  匹配一位数字  \d{n} n个数字  \d{a,b} a到b个数字,包含a,b \s  匹配任意空白符 ...

  8. IE8+等兼容、360调用webkit内核小记

    首先是处理IE8.9等的兼容问题,注意以下几点: 1,尽可能严格要求自己使用w3c推荐的方式编写html/css 2,在html页面顶部添加<!DOCHTML html>,不清楚请查看参考 ...

  9. "position:relative"在IE中的Bug

    当子元素过高导致父元素出现滚动条时,它并不会像预期的那样呆在父元素里,而是浮在父元素之上,并且位置不随滚动条的移动而改变.根源就是子元素的"position:relative".目 ...

  10. php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。

    这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...