珂朵莉树(odt老司机树)

传送门

题意:对于一段区间一共有四种操作:

题解:珂朵莉树板题

珂朵莉树,又称Old Driver Tree(ODT)。是一种基于std::set的暴力数据结构。

关键操作:推平一段区间,使一整段区间内的东西变得一样。保证数据随机。

这道题里,这样定义珂朵莉树的节点:

struct node
{
    int l,r;
    mutable LL v;
    node(int L, int R=-1, LL V=0):l(L), r(R), v(V) {}
    bool operator<(const node& o) const
    {
        return l < o.l;
    }
};

 这样的一个节点表示[l,r]内的所有数都是v。需要注意的是mutable,意为易变的,不定的。它对v的修饰,使得可以在add操作中修改v的值。没有它的修饰会在add函数里导致CE。

核心操作:split

#define IT set<node>::iterator
IT split(int pos)
{
    IT it = s.lower_bound(node(pos));
    if (it != s.end() && it->l == pos) return it;
    --it;
    int L = it->l, R = it->r;
    LL V = it->v;
    s.erase(it);
    s.insert(node(L, pos-1, V));
    return s.insert(node(pos, R, V)).first;
}

 最后一句插入后半段,返回后半段的迭代器。这里利用了pair<iterator,bool> insert (const value_type& val)的返回值。

推平操作:assign

void assign(int l, int r, LL val=0)
{
    IT itl = split(l),itr = split(r+1);
    s.erase(itl, itr);
    s.insert(node(l, r, val));
}

附上代码:

#include<cstdio>
#include<set>
#include<vector>
#include<utility>
#include<algorithm>
#define IT set<node>::iterator

using std::set;
using std::vector;
using std::pair;

typedef long long LL;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int imax_n = 1e5 + 7;

LL pow(LL a, LL b, LL mod)
{
    LL res = 1;
    LL ans = a % mod;
    while (b)
    {
        if (b&1) res = res * ans % mod;
        ans = ans * ans % mod;
        b>>=1;
    }
    return res;
}

struct node
{
    int l,r;
    mutable LL v;
    node(int L, int R=-1, LL V=0):l(L), r(R), v(V) {}
    bool operator<(const node& o) const
    {
        return l < o.l;
    }
};

set<node> s;

IT split(int pos)
{
    IT it = s.lower_bound(node(pos));
    if (it != s.end() && it->l == pos) return it;
    --it;
    int L = it->l, R = it->r;
    LL V = it->v;
    s.erase(it);
    s.insert(node(L, pos-1, V));
    return s.insert(node(pos, R, V)).first;
}

void add(int l, int r, LL val=1)
{
    IT itl = split(l),itr = split(r+1);
    for (; itl != itr; ++itl) itl->v += val;
}

void assign_val(int l, int r, LL val=0)
{
    IT itl = split(l),itr = split(r+1);
    s.erase(itl, itr);
    s.insert(node(l, r, val));
}

LL rank(int l, int r, int k)
{
    vector<pair<LL, int> > vp;
    IT itl = split(l),itr = split(r+1);
    vp.clear();
    for (; itl != itr; ++itl)
        vp.push_back(pair<LL,int>(itl->v, itl->r - itl->l + 1));
    std::sort(vp.begin(), vp.end());
    for (vector<pair<LL,int> >::iterator it=vp.begin();it!=vp.end();++it)
    {
        k -= it->second;
        if (k <= 0) return it->first;
    }
    return -1LL;
}

LL sum(int l, int r, int ex, int mod)
{
    IT itl = split(l),itr = split(r+1);
    LL res = 0;
    for (; itl != itr; ++itl)
        res = (res + (LL)(itl->r - itl->l + 1) * pow(itl->v, LL(ex), LL(mod))) % mod;
    return res;
}

int n, m;
LL seed, vmax;

LL rnd()
{
    LL ret = seed;
    seed = (seed * 7 + 13) % MOD7;
    return ret;
}

LL a[imax_n];

int main()
{
    scanf("%d %d %lld %lld",&n,&m,&seed,&vmax);
    for (int i=1; i<=n; ++i)
    {
        a[i] = (rnd() % vmax) + 1;
        s.insert(node(i,i,a[i]));
    }
    s.insert(node(n+1, n+1, 0));
    int lines = 0;
    for (int i =1; i <= m; ++i)
    {
        int op = int(rnd() % 4) + 1;
        int l = int(rnd() % n) + 1;
        int r = int(rnd() % n) + 1;
        if (l > r)
            std::swap(l,r);
        int x, y;
        if (op == 3)
            x = int(rnd() % (r-l+1)) + 1;
        else
            x = int(rnd() % vmax) +1;
        if (op == 4)
            y = int(rnd() % vmax) + 1;
        if (op == 1)
            add(l, r, LL(x));
        else if (op == 2)
            assign_val(l, r, LL(x));
        else if (op == 3)
            printf("%lld\n",rank(l, r, x));
        else
            printf("%lld\n",sum(l, r, x, y));
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值