c++ 珂朵莉树

using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <utility>
#include <map>
#include <vector>
#include <cmath>
#include <numeric>
#include <iostream>
 
using namespace std;
 
const auto _ = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();
 
#define ll long long
 
ll sum;
 
struct node {
    ll l, r;
    mutable ll v;
    node(ll l=0, ll r=0, ll v=0) : l(l), r(r), v(v) {}
    bool operator < (const node &a) const {
        return l < a.l;
    }
};
 
set<node> tree;
 
auto split(ll pos) {
    auto it = tree.lower_bound(pos);
    if (it != tree.end() and it->l == pos) {
        return it;
    }
    it = prev(it);
    ll l = it->l, r = it->r, v = it->v;
    tree.erase(it);
    tree.insert(node(l, pos - 1, v));
    return tree.insert(node(pos, r, v)).first;
}
 
void assign(ll l, ll r, ll v) {
    auto end = split(r + 1), begin = split(l);
    tree.erase(begin, end);
    tree.insert(node(l, r, v));
}
 
void add(ll l, ll r, ll x) {
    auto end = split(r + 1), beg = split(l);
    for (auto it = beg; it != end; it++) {
        it->v += x;
    }
}
 
ll kth(ll l, ll r, ll k) {
    auto end = split(r + 1), beg = split(l);
    vector<pair<ll, ll>> v_t;
    for (auto it = beg; it != end; it++) {
        v_t.push_back({it->v, it->r - it->l + 1});
    }
    ranges::sort(v_t);
    for (auto [v, t] : v_t) {
        k -= t;
        if (k <= 0) {
            return v;
        }
    }
    return 0;
}
 
ll qpow(ll a, ll n, ll p) {
    ll ans = 1;
    a %= p;
    while (n)
    {
        if (n & 1)
            ans = ans * a % p;
        n >>= 1;
        a = a * a % p;
    }
    return ans;
}
 
ll sum_of_pow(ll l, ll r, ll x, ll y) {
    ll tot = 0;
    auto end = split(r + 1);
    for (auto it = split(l); it != end; it++)
        tot = (tot + qpow(it->v, x, y) * (it->r - it->l + 1)) % y;
    return tot;
}
 
void solve() {
    ll n, m, seed, vmax;
    cin >> n >> m >> seed >> vmax;
    auto rnd = [&]() {
        ll ret = seed;
        seed = (seed * 7 + 13) % 1000000007;
        return ret;
    };
 
    for (int i = 1; i <= n; i++) {
        ll r = rnd();
        tree.insert(node(i, i, r % vmax + 1));
    }
    for (int i = 1; i <= m; i++) {
        int op = rnd() % 4 + 1;
        ll l = rnd() % n + 1;
        ll r = rnd() % n + 1;
        if (l > r) swap(l, r);
        ll x, y;
        if (op == 3) {
            x = rnd() % (r - l + 1) + 1;
        } else {
            x = rnd() % vmax + 1;
        }
        if (op == 4) {
            y = rnd() % vmax + 1;
        }
        switch (op) {
            case 1:
                add(l, r, x);
                break;
            case 2:
                assign(l, r, x);
                break;
            case 3:
                printf("%lld\n", kth(l, r, x));
                break;
            case 4:
                printf("%lld\n", sum_of_pow(l, r, x, y));
        }
    }
}
 
 int main() {
    solve();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值