2018 Northeast Agricultural University Spring School race: L-wyh Swan (TREAP)

Source: Internet
Author: User

Link: 2018 Northeast Agricultural University Spring School race: L-wyh's Swan

Test Instructions: insert element, delete element, find K large.

Puzzle : Treap.

#include <bits/stdc++.h> using namespace std;  #define LC (O-ch[0]) #define RC (O-and ch[1]) #define VAL (o-V) #define PRE (O-P) #define SIZ (O-
S) Const Double EPS = 1e-8;
const INT mod = 1e9 + 7;
const int INF = 0X3F3F3F3F;

const int MAXN = 5e5 + 10;
    struct Treap {treap* ch[2];
int p, V, S;

};

int root = 0, n, m, X;
    void Update (treap* &o) {siz = 1;
    if (LC! = NULL) Siz + = LC-S;
if (rc! = NULL) Siz + = RC-S;
    } void Rotate (treap* &o, int d) {treap* P = o-ch[d ^ 1];
    O-ch[d ^ 1] = P--ch[d];
    P--ch[d] = O; Update (o);
    Update (P);
o = P;
        } void Insert (treap* &o, int x) {if (o = = NULL) {o = new treap ();
        Lc = Rc = NULL;
        Pre = rand ();
    val = x;
        } else{int d = x < val;
        Insert (O-ch[d], x);
    if (Pre > O-ch[d], p) Rotate (o, d^1);
} Update (o);
 } void Delete (treap* &o, int x) {   if (val = = x) {if (Lc = = NULL) o = Rc;
        else if (Rc = = NULL) o = Lc;
            else{int T = (Lc, p) < (RC-p);
            Rotate (o, T);
        Delete (O-ch[t], x);
    }} else Delete (O-ch[x < Val], X);
if (o! = NULL) Update (o);
    } int Kth_max (treap* o, int k) {if (o = = NULL | | k <= 0 | | k > siz) return 0; int S = (Lc = = NULL)?
    0: (Lc-S);
    if (k = = S + 1) return Val;
    else if (k <= S) return Kth_max (Lc, k);
else return Kth_max (Rc, k-s-1);
    } int main () {int T;
    scanf ("%d", &t);
    Char s[10];
        while (t--) {Srand (Time (NULL));
        scanf ("%d%d", &n, &m);
        treap* Root = NULL;
            for (int i = 1, x; i <= n; i++) {scanf ("%d", &x);
        Insert (Root, x);
            } while (m--) {scanf ("%s%d", S, &x);
            if (s[0] = = ' Q ') printf ("%d\n", Kth_max (Root, x)); else if (s[0] = = ' I ')Insert (Root, x);
        else Delete (Root, x);
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.