【数据结构】Venice技巧

原文链接:https://codeforces.com/blog/entry/58316

更多数据结构技巧:https://codeforces.com/search?query=%23data+structure

维护一个类似mutiset的数据结构,支持以下操作:

1、向数据结构中添加c个元素x。
2、从数据结构中删除至多c个元素x。
3、查询数据结构中的元素数量
4、查询数据结构中某个元素x的个数c
5、把数据结构中的每个元素x改为元素x+k

这个操作5是比较复杂的,对此要维护一个全局的tag。然后发现它其实除了支持加法也支持异或,事实上它支持任意一个tag存在逆元的群。

解释:tag指示了内部的坐标加上tag之后对应于正确的外部坐标。所以Insert、Remove、Count这些操作是从外部对应到内部,是减去tag。Min、Max这些操作是从内部对应到外部,是加上tag。

struct VeniceTechnique {

    typedef map<ll, ll> mt;

    mt M;
    ll siz, tag;

    void Init() {
        M.clear();
        siz = 0, tag = 0;
    }

    void Insert(ll x, ll c = 1) {
        x -= tag;
        siz += c;
        M[x] += c;
    }

    void Remove(ll x, ll c = 1) {
        x -= tag;
        mt::iterator it = M.find(x);
        if(it == M.end())
            return;
        if(it->second <= c) {
            siz -= it->second;
            M.erase(it);
        } else {
            siz -= c;
            it->second -= c;
        }
    }

    void UpdateAll(ll k) {
        tag += k;
    }

    ll Count(ll x) {
        x -= tag;
        mt::iterator it = M.find(x);
        if(it == M.end())
            return 0;
        return it->second;
    }

    ll Min() {
        assert(siz);
        return M.begin()->first + tag;
    }

    ll Max() {
        assert(siz);
        return prev(M.end())->first + tag;
    }

    ll Size() {
        return siz;
    }

} vt;
posted @ 2021-02-14 13:42  purinliang  阅读(61)  评论(0编辑  收藏  举报