• hdu 1890 Robotic Sort


    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890

    如下:

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<iostream>
      4 #include<algorithm>
      5 using std::sort;
      6 using std::swap;
      7 const int Max_N = 100010;
      8 struct node{
      9     int val, pos;
     10 }rec[100001];
     11 inline bool cmp(const node &a, const node &b){
     12     if (a.val == b.val) return a.pos < b.pos;
     13     return a.val < b.val;
     14 }
     15 struct Node{
     16     int v, s, rsz, rev;
     17     Node *pre, *ch[2];
     18     inline void set(int _v = -1, int _s = 0, Node *p = NULL){
     19         v = _v, s = _s, rev = rsz = 0;
     20         pre =  ch[0] = ch[1] = p;
     21     }
     22     inline void update(){
     23         rev ^= 1;
     24         swap(ch[0], ch[1]);
     25     }
     26     inline void push_up(){
     27         s = ch[0]->s + ch[1]->s + 1;
     28         rsz = ch[0]->rsz + ch[1]->rsz;
     29         if (v != -1) rsz++;
     30     }
     31     inline void push_down(){
     32         if (rev != 0){
     33             rev ^= 1;
     34             ch[0]->update();
     35             ch[1]->update();
     36         }
     37     }
     38 };
     39 struct SplayTree{
     40     int top = 0;
     41     Node *tail, *root, *null;
     42     Node stack[Max_N], *store[Max_N], *ptr[Max_N];
     43     void init(int n){
     44         top = 0;
     45         tail = &stack[0];
     46         null = tail++;
     47         null->set();
     48         root = newNode(-1);
     49         root->ch[1] = newNode(-1);
     50         root->ch[1]->pre = root;
     51         Node *x = built(1, n);
     52         root->ch[1]->ch[0] = x;
     53         x->pre = root->ch[1];
     54         root->ch[1]->push_up();
     55         root->push_up();
     56     }
     57     inline Node *built(int l, int r){
     58         Node *p = null;
     59         if (l > r) return null;
     60         int mid = (l + r) >> 1;
     61         p = newNode(rec[mid].val), ptr[mid] = p;
     62         p->ch[0] = built(l, mid - 1);
     63         if (p->ch[0] != null) p->ch[0]->pre = p;
     64         p->ch[1] = built(mid + 1, r);
     65         if (p->ch[1] != null) p->ch[1]->pre = p;
     66         p->push_up();
     67         return p;
     68     }
     69     inline Node *newNode(int v){
     70         Node *p = null;
     71         if (!top) p = tail++;
     72         else p = store[--top];
     73         p->set(v, 1, null);
     74         return p;
     75     }
     76     inline void rotate(Node *x, int d){
     77         Node *y = x->pre;
     78         y->push_down(), x->push_down();
     79         y->ch[!d] = x->ch[d];
     80         if (x->ch[d] != null) x->ch[d]->pre = y;
     81         x->pre = y->pre;
     82         if (y->pre != null) y->pre->ch[y->pre->ch[0] != y] = x;
     83         x->ch[d] = y;
     84         y->pre = x;
     85         y->push_up();
     86         if (y == root) root = x;
     87     }
     88     inline void splay(Node *x, Node *f){
     89         for (; x->pre != f; x->push_down()){
     90             if (x->pre->pre == f){
     91                 rotate(x, x->pre->ch[0] == x);
     92             } else {
     93                 Node *y = x->pre, *z = y->pre;
     94                 if (z->ch[0] == y){
     95                     if (y->ch[0] == x) rotate(y, 1), rotate(x, 1);
     96                     else rotate(x, 0), rotate(x, 1);
     97                 } else {
     98                     if (y->ch[1] == x) rotate(y, 0), rotate(x, 0);
     99                     else rotate(x, 1), rotate(x, 0);
    100                 }
    101             }
    102         }
    103         x->push_up();
    104     }
    105     inline Node *select(Node *x, int k){
    106         int t = 0;
    107         for (;;){
    108             x->push_down();
    109             t = x->ch[0]->s;
    110             if (k == t + 1) break;
    111             else if (k < t + 1) x = x->ch[0];
    112             else k -= t + 1, x = x->ch[1];
    113         }
    114         return x;
    115     }
    116     inline void del(){
    117         Node *x = root;
    118         store[top++] = x;
    119         root = root->ch[1];
    120         root->pre = null;
    121         splay(select(root, 1), null);
    122         root->ch[0] = x->ch[0];
    123         root->ch[0]->pre = root;
    124         root->push_up();
    125     }
    126     inline void solve(int n) {
    127         const int N = n + 2;
    128         for (int i = 1; i <= n; ++i) {
    129             scanf("%d", &rec[i].val);
    130             rec[i].pos = i;
    131         }
    132         init(n), sort(rec + 1, rec + n + 1, cmp);
    133         splay(root->ch[1], null);
    134         for (int i = 1; i <= n; i++){
    135             splay(ptr[rec[i].pos], null);
    136             root->ch[0]->update();
    137             printf("%d", root->ch[0]->rsz + i);
    138             if (i != n) printf(" ");
    139             del();
    140         }
    141         printf("
    ");
    142     }
    143 }spt;
    144 int main(){
    145 #ifdef LOCAL
    146     freopen("in.txt", "r", stdin);
    147     freopen("out.txt", "w+", stdout);
    148 #endif
    149     int n;
    150     while (~scanf("%d", &n) && n) {
    151         spt.solve(n);
    152     }
    153     return 0;
    154 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    Git
    canvas画布
    Node.js
    node的consoidate的插件统一
    使用nodejs去做一个验证码
    node的cookie-parser和express-session
    node的router路由。
    node的经典事件监听
    使用node去爬虫
    node的读写流
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4472269.html
Copyright © 2020-2023  润新知