Chtholly Tree (珂朵莉树) ODT

ODT,OldDriverTreeChthollyTree

1. CF 896C Willem, Chtholly and Seniorious

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int MOD7 = 1e9 + 7;
const int MOD9 = 1e9 + 9;
const int imax_n = 1e5 + 7;
//大数相加
LL add(LL a, LL b, LL mod)
{
    LL res = 0;
    LL ans = a;
    while (b)
    {
        if (b&1) res = (res + ans) % mod;
        ans = (ans + ans) % mod;
        b>>=1;
    }
    return res;
}
//快速幂
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;


// 拆分操作
set<node>::iterator split(int pos)
{
    auto it = s.lower_bound(node(pos));
    if (it != s.end() && it->l == pos) return it;
    --it;
    if (pos > it->r) return s.end();
    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);
}


//区间加操作
void add(int l, int r, LL val=1)
{
    split(l);
    auto itr = split(r+1), itl = split(l);
    for (; itl != itr; ++itl) itl->v += val;
}

//区间赋值操作
void assign_val(int l, int r, LL val=0)
{
    split(l);
    auto itr = split(r+1), itl = split(l);
    s.erase(itl, itr);
    s.insert(node(l, r, val));
}


//求区间第K大值,reversed控制顺序与逆序
LL rank_(int l, int r, int k, bool reversed=0)
{
    vector<pair<LL, int>> vp;
    if (reversed) k = r - l + 2 - k;
    split(l);
    auto itr = split(r+1), itl = split(l);
    vp.clear();
    for (; itl != itr; ++itl)
        vp.push_back({itl->v, itl->r - itl->l + 1});
    sort(vp.begin(), vp.end());
    for (auto i: vp)
    {
        k -= i.second;
        if (k <= 0) return i.first;
    }
    return -1LL;
}


//区间值求和操作
LL sum(int l, int r, int ex, int mod)
{
    split(l);
    auto itr = split(r+1), itl = split(l);
    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()
{
    cin>>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)
            swap(l,r);
        int x, y;
        // cout<<i<<"   op = "<<op<<"    ";
        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)
        {
            // cout<<"lines"<<++lines<<endl;
            cout<<rank_(l, r, x)<<endl;
        }
        else
        {
            // cout<<"lines"<<++lines<<endl;
            cout<<sum(l, r, x, y)<<endl;
        }
    }
    return 0;
}
posted @ 2018-04-12 12:21  Bryce1010  阅读(143)  评论(0编辑  收藏  举报