HDU
来源:互联网 发布:java最简单的登录界面 编辑:程序博客网 时间:2024/06/03 14:10
Play with Chain
HDU - 3487YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.
FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8
He wants to know what the chain looks like after perform m operations. Could you help him?
At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.
FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8
He wants to know what the chain looks like after perform m operations. Could you help him?
For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.
Then m lines follow, each line contains one operation. The command is like this:
CUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
FLIP a b // Means a FLIP operation, 1 ≤ a < b ≤ n.
The input ends up with two negative numbers, which should not be processed as a case.
8 2CUT 3 5 4FLIP 2 6-1 -1
1 4 3 7 6 2 5 8
Splay的基础题,按照要求进行区间移动和区间反转即可。
复杂度 O(m*logn)
#include <iostream>#include <cstdio>#include <string>#include <algorithm>using namespace std;typedef long long LL;const int MAXN = 3e5 + 8;int n;bool flag;struct SplayTree{ int ch[MAXN][2], fa[MAXN], tot, root; int val[MAXN], reversed[MAXN], sz[MAXN]; int newnode(int f, int u){ reversed[tot] = ch[tot][0] = ch[tot][1] = 0; fa[tot] = f; val[tot] = u; sz[tot] = 1; return tot ++; } void init(){ tot = 1; ch[0][0] = ch[0][1] = fa[0] = sz[0] = 0; } void build(int f, int &x, int l, int r){ if (l > r) return; int mid = (l + r) >> 1; x = newnode(f, mid); build(x, ch[x][0], l, mid - 1); build(x, ch[x][1], mid + 1, r); pushup(x); } void pushdown(int x){ if(!reversed[x]) return; reversed[ch[x][0]] ^= 1; reversed[ch[x][1]] ^= 1; swap(ch[x][0], ch[x][1]); reversed[x] = 0; } void pushup(int x){ sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; } void _rotate(int x, int kind){ int y = fa[x]; ch[y][!kind] = ch[x][kind]; fa[ch[x][kind]] = y; if(fa[y]) ch[fa[y]][y == ch[fa[y]][1]] = x; fa[x] = fa[y]; fa[y] = x; ch[x][kind] = y; sz[x] = sz[y]; pushup(y); } void splay(int x, int r){ int y, z; for (int f = fa[r]; fa[x] != f; ){ if(fa[fa[x]] == f){_rotate(x, x == ch[fa[x]][0]); return; } y = x == ch[fa[x]][0], z = fa[x] == ch[fa[fa[x]]][0]; y^z ? (_rotate(x, y), _rotate(x, z)) : (_rotate(fa[x], z), _rotate(x, y)); } } void _find(int &x, int k){ for (int i = x, j = k; ; ){ pushdown(i); if(sz[ch[i][0]] == j){splay(i, x); x = i; break; } if (sz[ch[i][0]] > j){i = ch[i][0]; continue; } j -= sz[ch[i][0]] + 1; i = ch[i][1]; } } void _reverse(int&x, int l, int r){ _find(x, l - 1); _find(ch[x][1], r - l + 1); reversed[ch[ch[x][1]][0]] ^= 1; } void cut_insert(int&x, int l, int r, int c){ _find(x, l - 1); _find(ch[x][1], r - l + 1); int add = sz[ch[ch[x][1]][0]], temp = ch[ch[x][1]][0]; sz[x] -= add; sz[ch[x][1]] -= add; ch[ch[x][1]][0] = 0; _find(x, c); _find(ch[x][1], 0); ch[ch[x][1]][0] = temp; fa[temp] = ch[x][1]; sz[ch[x][1]] += add; sz[x] += add; } void traversal(int x){ pushdown(x); if(ch[x][0]) traversal(ch[x][0]); if(val[x] != 0 && val[x] != n + 1){ if(flag) cout << " "; cout << val[x]; flag = true; } if(ch[x][1]) traversal(ch[x][1]); }}spt;string s;int main(){ #ifdef LOCAL freopen("SplayTree.in", "r", stdin); //freopen("SplayTree.out", "w", stdout); #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int m, l, r, c; while(cin >> n >> m){ if(n == -1) break; spt.init(); spt.root = 0; spt.build(0, spt.root, 0, n + 1); while(m--){ cin >> s; cin >> l >> r; if(s[0] == 'C') cin >> c, spt.cut_insert(spt.root, l, r, c); else spt._reverse(spt.root, l, r); } flag = false; spt.traversal(spt.root); cout << "\n"; } return 0;}
Thank you!
------from ProLights
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Kubernetes计算资源管理--requests和limits
- 算法随练(斐波那契数列)
- IOS开发之关键字synchronized
- JavaScript—变量提升
- MVVM 介绍
- HDU
- 《webrtc权威指南》 资源
- 【SSH网上商城项目实战12】添加和更新商品功能的实现
- 多级反向代理下,Java获取请求客户端的真实IP地址多中方法整合
- 关于Git的几个使用技巧
- 有关opencv的学习(13)—使用固定阈值处理图像
- Ubuntu14.04下深度学习框架Caffe的搭建
- Tomcat中JVM内存溢出及合理配置
- 编码(乱码问题原因)