zoukankan      html  css  js  c++  java
  • 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 }
    View Code

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

    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    DAY 42 前端
    DAY 41 mysql
    DAY 40 前端学习
    DAY 39 前端学习
    DAY 38 前端学习
    DAY 37 前端学习
    上传一个桌面
    找到anaconda中自带的qtdesigner,设计ui文件并生成py文件
    python课程:python3的数字与字符串
    python3 偏最小二乘法实现
  • 原文地址:https://www.cnblogs.com/rausen/p/4295546.html
Copyright © 2011-2022 走看看