BZOJ 2770: YY of Treap

Balanced tree traversal sequence after sequence is incremented, then certainly the LCA key plus key between two nodes
and the priority is the smallest, otherwise you can go up
so that the interval to find the best value, the dynamic point to open
input will be negative number. .
Constant large. .
Because it is the log int size log. .

#include <bits/stdc++.h>

const int N = 1e5 + 7;
const int INF = 0x3f3f3f3f;

struct Node {
    int pri, key;
    Node(int _ = INF, int __ = 0): pri(_), key(__) {}
    bool operator < (const Node &p) const {
        return pri < p.pri;
    }
};

struct Seg {
    #define lp ch[p][0]
    #define rp ch[p][1]
    #define mid ((l + r) >> 1)
    Node tree[N * 20];
    int ch[N * 20][2];
    int tol;
    void pushup(int p) {
        tree[p] = std::min(tree[lp], tree[rp]);
        //printf("%d %d %d\n", tree[lp].pri, tree[rp].pri, tree[p].pri);
    }
    void update(int &p, int l, int r, int pos, int v) {
        if (!p) p = ++tol;
        //printf("%d %d %d\n", p, l, r);
        if (l == r) {
            tree[p] = Node(v, l);
            //printf("%d %d %d\n", p, l, tree[p].pri);
            return;
        }
        if (pos <= mid) update(lp, l, mid, pos, v);
        else update(rp, mid + 1, r, pos, v);
        pushup(p);
        //printf("%d %d %d\n", l, r, tree[p].pri);
    }
    Node query(int p, int l, int r, int x, int y) {
        //printf("%d %d %d %d\n", p, l, r, tree[p].pri);
        if (x <= l && y >= r) return tree[p];
        Node lans, rans;
        if (x <= mid) lans = query(lp, l, mid, x, y);
        if (y > mid) rans = query(rp, mid + 1, r, x, y);
        return std::min(lans, rans);
    }
} seg;

int ke[N], pri[N], root;
const int ALL = 1e9 + 1;

int main() {
    //tree[0] = Node(INF, INF);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", ke + i);
    for (int i = 1; i <= n; i++)
        scanf("%d", pri + i);
    for (int i = 1; i <= n; i++)
        seg.update(root, -ALL, ALL, ke[i], pri[i]);
    for (int l, r; m--; ) {
        static char s[10];
        scanf("%s", s);
        if (s[0] == 'I') {
            scanf("%d%d", &l, &r);
            seg.update(root, -ALL, ALL, l, r);
        } else if (s[0] == 'D') {
            scanf("%d", &l);
            seg.update(root, -ALL, ALL, l, INF);
        } else {
            scanf("%d%d", &l, &r);
            if (l > r) std::swap(l, r);
            printf("%d\n", seg.query(root, -ALL, ALL, l, r).key);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Mrzdtz220/p/12306120.html