您的位置:首页 > 大数据 > 人工智能

【HDOJ】3487 Play with Chain

2015-10-23 00:03 423 查看
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+5;
int pre[maxn], ch[maxn][2], 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] = 0;
ch[r][0] = ch[r][1] = 0;
s[r] = 1;
}

void PushUp(int r) {
s[r] = s[ch[r][0]] + s[ch[r][1]] + 1;
}

void UpdateRev(int rt) {
if (rt == 0)    return ;
swap(ch[rt][0], ch[rt][1]);
rev[rt] ^= 1;
}

void PushDown(int rt) {
if (rev[rt]) {
UpdateRev(ch[rt][0]);
UpdateRev(ch[rt][1]);
rev[rt] = 0;
}
}

void Build(int& rt, int l, int r, int fa) {
if (l > r)    return ;

int mid = (l + r) >> 1;

newNode(rt, fa, mid);
Build(ch[rt][0], l, mid-1, rt);
Build(ch[rt][1], mid+1, r, rt);
PushUp(rt);
}

void inorder(int rt) {
if (rt == 0)    return ;
inorder(ch[rt][0]);
printf("s = %d, key = %d\n", s[rt], key[rt]);
inorder(ch[rt][1]);
}

void init() {
root = tot = top = 0;
ch[0][0] = ch[0][1] = s[0] = key[0] = pre[0] = rev[0] = 0;
newNode(root, 0, -1);
newNode(ch[root][1], root, -1);
Build(grandlson, 1, n, ch[root][1]);
PushUp(ch[root][1]);
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^1] = ch[x][d];
pre[ch[x][d]] = y;
if (pre[y])
ch[pre[y]][ch[pre[y]][1]==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]][0]==r);
} else {
PushDown(pre[pre[r]]);
PushDown(pre[r]);
PushDown(r);
int y = pre[r];
int d = ch[pre[y]][0]==y;
if (ch[y][d] == r) {
Rotate(r, d^1);
Rotate(r, d);
} else {
Rotate(y, d);
Rotate(r, d);
}
}
}
PushUp(r);
if (goal == 0)
root = r;
}

int kth(int r, int k) {
PushDown(r);
int t = s[ch[r][0]] + 1;

if (t == k)
return r;
else if (k < t)
return kth(ch[r][0], k);
else
return kth(ch[r][1], k-t);
}

void cut(int l, int r, int c) {
Splay(kth(root, l), 0);
Splay(kth(root, r+2), root);
int tmp = grandlson;
grandlson = 0;
PushUp(ch[root][1]);
PushUp(root);
Splay(kth(root, c+1), 0);
Splay(kth(root, c+2), root);
grandlson = tmp;
pre[tmp] = ch[root][1];
PushUp(ch[root][1]);
PushUp(root);
}

void flip(int l, int r) {
Splay(kth(root, l), 0);
Splay(kth(root, r+2), root);
UpdateRev(grandlson);
PushUp(ch[root][1]);
PushUp(root);
}

void print(int rt) {
if (!rt)    return ;

PushDown(rt);
print(ch[rt][0]);
if (cnt>=1 && cnt<=n) {
if (cnt < n)
printf("%d ", key[rt]);
else
printf("%d\n", key[rt]);
}
++cnt;
print(ch[rt][1]);
}

int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif

char cmd[7];
int a, b, c;

while (scanf("%d %d", &n, &m) != EOF) {
if (n<0 && m<0)
break;
init();
while (m--) {
scanf("%s %d %d", cmd, &a, &b);
if (cmd[0] == 'C') {
scanf("%d", &c);
cut(a, b, c);
} else {
flip(a, b);
}
}
cnt = 0;
print(root);
}

#ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: