code[VS]1743反转卡片 SPlay
来源:互联网 发布:每周开户数据 编辑:程序博客网 时间:2024/06/10 10:04
弟弟救我。
原本想今晚不来机房的,结果硬生生写了个平衡树再回去。
SPlay区间翻转,为了防止越界可以在最后面增加一个虚拟节点。
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#define INF 1<<30#define N 600050using namespace std;int siz[N],tr[N],s[N][2],tag[N],fa[N];int a[N],rt,cnt,n;inline void update(int t) { siz[t] = siz[ s[t][0] ] + siz[ s[t][1] ] + 1; }void rotate(int x,int &k) {int y = fa[x] , z = fa[y] , l = s[y][0] != x , r = l ^ 1;if (y == k) k = x; else {if (s[z][0] == y) s[z][0] = x; else s[z][1] = x; }fa[x] = z; fa[y] = x; fa[ s[x][r] ] = y;s[y][l] = s[x][r]; s[x][r] = y;update(y); update(x);}void SPlay(int x) {while (x != rt) {int y = fa[x] , z = fa[y];if (y != rt) {if ((s[z][0] == y)^(s[y][0] == x)) rotate(x,rt);elserotate(y,rt);} rotate(x,rt);}}inline void push_down(int t) {if (!tag[t]) return ;swap(s[t][0],s[t][1]);tag[ s[t][0] ] ^= 1;tag[ s[t][1] ] ^= 1;tag[t] ^= 1;}int kth(int k,int x) {push_down(x);int tmp = siz[ s[x][0] ] + 1;if (tmp == k) return x;return tmp > k ? kth(k,s[x][0]) : kth(k-tmp,s[x][1]);}void build(int l,int r,int &t,int f) {//if (l > r) return ;int mid = (l + r) >> 1;tr[ t=++cnt ] = mid;fa[t] = f;siz[t] = 1;if (l == r) return ;if (l <= mid-1) build(l,mid-1,s[t][0],t);if (mid+1 <= r) build(mid+1,r,s[t][1],t);update(t);}inline void solve() {build(1,n,rt,0);int tmp = 0 , ans = 0 , deb = 0;while (( tmp = a[ tr[ deb = kth(1,rt) ] ] ) != 1) {SPlay(deb);int p = kth(tmp+1,rt);SPlay(p);tag[ s[rt][0] ] ^= 1;ans++;}printf("%d\n",ans);}inline void init() {scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]);a[++n] = INF;}int main(){init();solve();return 0;}
0 0
- code vs 1743 反转卡片(splay)
- code[VS]1743反转卡片 SPlay
- Codevs 1743 反转卡片 [Splay]
- Codevs 1743 反转卡片(splay)
- codevs 1743 反转卡片/splay tree
- 【codevs 1743】反转卡片 splay的应用
- codevs 1743 反转卡片【Splay区间翻转】
- [CODEVS1743]反转卡片(splay)
- cv1743 反转卡片(splay)
- [codevs 1743] 反转卡片
- codevs 1743 反转卡片
- codevs 1743 翻转卡片(splay)
- codevs1743 反转卡片
- 【codevs1743】反转卡片
- Splay 区间反转
- code vs 3243 区间反转(线段树)
- 位运算 反转卡片问题
- 小练习,splay区间反转
- 寻找数组中最大值
- 数据结构实验之查找六:顺序查找
- Learning Python 016 写文件时,将其用指定的编码方式保存(比如:UTF-8无BOM编码方式)
- CCF-NOIP2016 PJ T1
- linux系统下使用润乾报表设计器
- code[VS]1743反转卡片 SPlay
- 删除数组零元素
- gps各个模块的解析
- Sparse Matrix --- (0) Intro
- iOS 两应用之间相互跳转
- 非阻塞,et模型 epoll
- Python pandas 0.19.1 Indexing and Selecting Data文档翻译
- 状态模式案例分析
- python搭建本地服务器