zoukankan      html  css  js  c++  java
  • BZOJ 3196 二逼平衡树

    线段树套平衡树

    上次spaly头铁卡不过去。。leaf tree轻轻松松~

    #include <bits/stdc++.h>
    #define INF 2147483647
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    #define newNode(a, b, c, d) (&(*pool[cnt++] = Node(a, b, c, d)))
    #define merge(a, b) newNode(a->size + b->size, b->val, a, b)
    #define push_up(rt) if(rt->lf->size) rt->size = rt->lf->size + rt->rf->size, rt->val = rt->rf->val
    #define ratio 4
    const int N = 500005;
    struct Node{
        int size, val;
        Node *lf, *rf;
        Node(){}
        Node(int size, int val, Node *lf, Node *rf): size(size), val(val), lf(lf), rf(rf){}
    }*null, *root[N<<2], *pool[N<<2], t[N<<2];
    int cnt, n, m, a[N];
    
    inline void maintain(Node *cur){
        if(cur->lf->size > cur->rf->size * ratio)
            cur->rf = merge(cur->lf->rf, cur->rf), pool[--cnt] = cur->lf, cur->lf = cur->lf->lf;
        if(cur->rf->size > cur->lf->size * ratio)
            cur->lf = merge(cur->lf, cur->rf->lf), pool[--cnt] = cur->rf, cur->rf = cur->rf->rf;
    }
    
    void insert(Node *cur, int val){
        if(cur->size == 1){
            cur->lf = newNode(1, min(val, cur->val), null, null);
            cur->rf = newNode(1, max(val, cur->val), null, null);
        }
        else insert(val > cur->lf->val ? cur->rf : cur->lf, val);
        push_up(cur), maintain(cur);
    }
    
    void erase(Node *cur, int val){
        if(cur->lf->size == 1 && cur->lf->val == val)
            pool[--cnt] = cur->lf, pool[--cnt] = cur->rf, *cur = *cur->rf;
        else if(cur->rf->size == 1 && cur->rf->val == val)
            pool[--cnt] = cur->lf, pool[--cnt] = cur->rf, *cur = *cur->lf;
        else erase(val > cur->lf->val ? cur->rf : cur->lf, val);
        push_up(cur), maintain(cur);
    }
    
    int find(Node *cur, int val){
        if(cur->size == 1) return 1;
        return val > cur->lf->val ? cur->lf->size + find(cur->rf, val) : find(cur->lf, val);
    }
    
    int select(Node *cur, int k){
        if(cur->size == 1) return cur->val;
        return k > cur->lf->size ? select(cur->rf, k - cur->lf->size) : select(cur->lf, k);
    }
    
    int precursor(Node *rt, int val){
        return select(rt, find(rt, val) - 1);
    }
    
    int successor(Node *rt, int val){
        return select(rt, find(rt, val + 1));
    }
    
    void buildTree(int rt, int l, int r){
        root[rt] = new Node(1, INF, null, null);
        insert(root[rt], -INF);
        for(int i = l; i <= r; i ++){
            insert(root[rt], a[i]);
        }
        if(l == r) return;
        int mid = (l + r) >> 1;
        buildTree(rt << 1, l, mid);
        buildTree(rt << 1 | 1, mid + 1, r);
    }
    
    int queryRank(int rt, int l, int r, int ql, int qr, int val){
        if(l == ql && r == qr){
            return find(root[rt], val) - 2;
        }
        int mid = (l + r) >> 1;
        if(qr <= mid) return queryRank(rt << 1, l, mid, ql, qr, val);
        else if(ql > mid) return queryRank(rt << 1 | 1, mid + 1, r, ql, qr, val);
        else return queryRank(rt << 1, l, mid, ql, mid, val) + queryRank(rt << 1 | 1, mid + 1, r, mid + 1, qr, val);
    }
    
    int querySelect(int l, int r, int k){
        int L = 0, R = 1e8;
        while(L < R){
            int mid = (L + R + 1) >> 1;
            if(queryRank(1, 1, n, l, r, mid) >= k) R = mid - 1;
            else L = mid;
        }
        return L;
    }
    
    void modify(int rt, int l, int r, int pos, int val){
        insert(root[rt], val);
        erase(root[rt], a[pos]);
        if(l == r) return;
        int mid = (l + r) >> 1;
        if(pos <= mid) modify(rt << 1, l, mid, pos, val);
        else modify(rt << 1 | 1, mid + 1, r, pos, val);
    }
    
    int queryPre(int rt, int l, int r, int ql, int qr, int val){
        if(l == ql && r == qr){
            return precursor(root[rt], val);
        }
        int mid = (l + r) >> 1;
        if(qr <= mid) return queryPre(rt << 1, l, mid, ql, qr, val);
        else if(ql > mid) return queryPre(rt << 1 | 1, mid + 1, r, ql, qr, val);
        else return max(queryPre(rt << 1, l, mid, ql, mid, val), queryPre(rt << 1 | 1, mid + 1, r, mid + 1, qr, val));
    }
    
    int querySuc(int rt, int l, int r, int ql, int qr, int val){
        if(l == ql && r == qr){
            return successor(root[rt], val);
        }
        int mid = (l + r) >> 1;
        if(qr <= mid) return querySuc(rt << 1, l, mid, ql, qr, val);
        else if(ql > mid) return querySuc(rt << 1 | 1, mid + 1, r, ql, qr, val);
        else return min(querySuc(rt << 1, l, mid, ql, mid, val), querySuc(rt << 1 | 1, mid + 1, r, mid + 1, qr, val));
    }
    
    int main(){
    
        //freopen("data.txt", "r", stdin);
        
        for(int i = 0; i < (N<<2); i ++) pool[i] = &t[i];
        null = new Node(0, 0, nullptr, nullptr);
        n = read(), m = read();
        for(int i = 1; i <= n; i ++) a[i] = read();
        buildTree(1, 1, n);
        while(m --){
            int opt = read();
            if(opt == 1){
                int l = read(), r = read(), k = read();
                printf("%d
    ", queryRank(1, 1, n, l, r, k) + 1);
            }
            else if(opt == 2){
                int l = read(), r = read(), k = read();
                printf("%d
    ", querySelect(l, r, k));
            }
            else if(opt == 3){
                int pos = read(), k = read();
                modify(1, 1, n, pos, k);
                a[pos] = k;
            }
            else if(opt == 4){
                int l = read(), r = read(), k = read();
                printf("%d
    ", queryPre(1, 1, n, l, r, k));
            }
            else{
                int l = read(), r = read(), k = read();
                printf("%d
    ", querySuc(1, 1, n, l, r, k));
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    linux基础命令篇四
    linux基础命令篇三
    linux基础命令篇二
    msf测试
    msf
    [精品转载] [NoSaFe]KALI下免杀神器TheFatRat使用秘籍
    kali&BT安装好之后无法上网或者无法获得内网IP
    Kali Linux安装之后需要做的一些事
    在Ubuntu下解决E: 无法对目录 /var/lib/apt/lists/ 加锁的问题(转)
    xshell之类的软件第一次连接不上初次安装kali问题(转)
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/11028988.html
Copyright © 2011-2022 走看看