【模板】权值线段树

#define lc(x) (x<<1)
#define rc(x) (x<<1|1)

struct tree
{
    int num;
    int l, r;
}node[maxn];

inline void build(int x, int l, int r)
{
    node[x].num = 0;
    node[x].l = l;
    node[x].r = r;
    if (l == r) return;
    int mid = (node[x].l + node[x].r) >> 1;
    build(lc(x), l, mid);
    build(rc(x), mid + 1, r);
}

inline void add(int x, const int& k)
{
    if (node[x].l == node[x].r) node[x].num++;
    else
    {
        int mid = (node[x].l + node[x].r) >> 1;
        if (k <= mid) add(lc(x), k);
        else add(rc(x), k);
        node[x].num = node[lc(x)].num + node[rc(x)].num;
    }
}

inline int eixcount(const int& left, const int& right, int x)
{
    if (left <= node[x].l && node[x].r <= right) return node[x].num;
    else
    {
        int mid = (node[x].l + node[x].r) >> 1;
        if (right <= mid) return eixcount(left, right, lc(x));
        else if (left > mid) return eixcount(left, right, rc(x));
        else return eixcount(left, right, lc(x)) + eixcount(left, right, rc(x));
    }
}

inline int kthmin(int x, int k)
{
    if (node[x].l == node[x].r) return node[x].l;
    int mid = (node[x].l + node[x].r) >> 1;
    if (k <= node[lc(x)].num) return kthmin(lc(x), k);
    else return kthmin(rc(x), k - node[lc(x)].num);
}

 

posted on 2019-12-04 14:34  thjkhdf12  阅读(178)  评论(0)    收藏  举报