BZOJ3223 Tyvj 1729 文艺平衡树

fhq:我们有一个新的Treap,支持splay的一切操作你怕不怕。。。

memphis:我们讲的更详细一点好了

  1 /**************************************************************
  2     Problem: 3223
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:2320 ms
  7     Memory:3148 kb
  8 ****************************************************************/
  9
 10 #include <cstdio>
 11 #include <algorithm>
 12
 13 using namespace std;
 14
 15 struct node {
 16     node *son[2];
 17     int pri, rev, sz, key;
 18 } *null, *root, mempool[100005], *cnt_treap = mempool;
 19
 20 int n;
 21
 22 inline int read() {
 23     int x = 0;
 24     char ch = getchar();
 25     while (ch < ‘0‘ || ‘9‘ < ch)
 26         ch = getchar();
 27     while (‘0‘ <= ch && ch <= ‘9‘) {
 28         x = x * 10 + ch - ‘0‘;
 29         ch = getchar();
 30     }
 31     return x;
 32 }
 33
 34 inline int rnd() {
 35     static int random = 233333;
 36     return random += random << 2 | 1;
 37 }
 38
 39 #define Ls p -> son[0]
 40 #define Rs p -> son[1]
 41 #define Sz p -> sz
 42 #define Rev p -> rev
 43 inline void reverse_node(node *p) {
 44     if (p == null) return;
 45     Rev ^= 1;
 46     swap(Ls, Rs);
 47 }
 48
 49 inline void push_down(node *p) {
 50     if (Rev) {
 51         Rev = 0;
 52         reverse_node(Ls), reverse_node(Rs);
 53     }
 54 }
 55
 56 inline node *update(node *p) {
 57     Sz = Ls -> sz + Rs -> sz + 1;
 58     return p;
 59 }
 60
 61 node *new_node(int x) {
 62     ++cnt_treap;
 63     cnt_treap -> sz = 1, cnt_treap -> rev = 0;
 64     cnt_treap -> key = x, cnt_treap -> pri = rnd();
 65     cnt_treap -> son[0] = cnt_treap -> son[1] = null;
 66     return cnt_treap;
 67 }
 68
 69 node *merge(node *p, node *q) {
 70     if (p == null) return update(q);
 71     if (q == null) return update(p);
 72     if (p -> pri < q -> pri) {
 73         push_down(p);
 74         p -> son[1] = merge(p -> son[1], q);
 75         return update(p);
 76     } else {
 77         push_down(q);
 78         q -> son[0] = merge(p, q -> son[0]);
 79         return update(q);
 80     }
 81 }
 82
 83 void split(node *p, node *&q, node *&r, int x) {
 84     if (p == null) {
 85         q = null, r = null;
 86         return;
 87     }
 88     push_down(p);
 89     if (Ls -> sz >= x) {
 90         split(Ls, q, r, x);
 91         Ls = null;
 92         update(p);
 93         r = merge(r, p);
 94     } else {
 95         split(Rs, q, r, x - Ls -> sz - 1);
 96         Rs = null;
 97         push_down(p);
 98         q = merge(p, q);
 99     }
100 }
101
102 void reverse(int x, int y) {
103     node *p, *q, *r, *s;
104     split(root, p, q, x - 1);
105     split(q, r, s, y - x + 1);
106     reverse_node(r);
107     root = merge(p, merge(r, s));
108     update(root);
109 }
110
111 void print(node *p) {
112     if (p == null) return;
113     push_down(p);
114     print(Ls);
115     printf("%d ", p -> key);
116     print(Rs);
117 }
118 #undef Ls
119 #undef Rs
120 #undef Sz
121 #undef Rev
122
123 int main() {
124     int Q, i, x, y;
125     n = read(), Q = read();
126     null = cnt_treap;
127     null -> son[0] = null -> son[1] = null, null -> sz = 0;
128     root = null;
129     for (i = 1; i <= n; ++i)
130         root = merge(root, new_node(i));
131     while (Q--) {
132         x = read(), y = read();
133         if (x > y) swap(x, y);
134         reverse(x, y);
135     }
136     print(root);
137     return 0;
138 }

(p.s. 话说这程序又难看速度又捉鸡,下次认真学习了再写。。。)

时间: 2024-08-20 09:12:10

BZOJ3223 Tyvj 1729 文艺平衡树的相关文章

BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 Input 第一行为n,m n表示初始序列有n个数,这个序列依次

【Splay】bzoj3223 Tyvj 1729 文艺平衡树

#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; #define maxn 100010 #define INF 2147483647 int fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn],cnt[maxn],val2[maxn]; bool delta[maxn];

【Splay】【块状链表】bzoj3223 Tyvj 1729 文艺平衡树

让蒟蒻见识到了常数大+滥用STL的危害. <法一>很久之前的Splay #include<cstdio> #include<algorithm> using namespace std; #define maxn 110000 #define INF 2147483647 int n,m,l,r,fa[maxn],c[maxn][2],val[maxn],head,tail,root,tot,siz[maxn]; bool delta[maxn]; inline voi

3223: Tyvj 1729 文艺平衡树

3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3473  Solved: 1962[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 Input 第一行为n,m n表示初始序列有n个数,这个序列依次

BZOJ 3223: Tyvj 1729 文艺平衡树

3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3629  Solved: 2053[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 Input 第一行为n,m n表示初始序列有n个数,这个序列依次

[HZOI 2016][Tyvj 1729]文艺平衡树 这道题我真是哭了,调了一下午,一晚上

[题目描述] 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 [输入格式] 第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2--n-1,n)  m表示翻转操作次数 接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n [输出格式] 输出一行n个数字,表示原始序列经过m次变换后的结果 [样例输入] 5 3 1 3 1 3 1 4 [

bzoj 3223/tyvj 1729 文艺平衡树 splay tree

原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 const int MAX_N = 100010; 6 struct Node{ 7

Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4604  Solved: 2691[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 Input 第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作

【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)

http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> #include <set> #in