珂朵莉树是我校一位巨佬教我的,原理很简单,在set上维护一个三元组表示的区间信息,感觉和分块类似。在区间覆盖,区间第k小(大)的值等区间信息很方便。
前提条件是数据要随机!!
想要学习点这里
#pragma GCC optimize(2)
#include
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int maxn = 1e5+5;
int Case = 1;
ll power(ll a, ll b, ll mod) {
ll res = 1, base = a%mod;
while(b) {
if(b&1) res = res*base%mod;
base = base*base%mod;
b = 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 x)const{
return l < x.l;
}
};
class ODT{
private:
set<node>s;
vector<pair<ll, ll> >ve;
public:
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;
}
void add(int l, int r, ll val = 0) {
split(l);
auto itr = split(r+1), itl = split(l);
for(;itl!=itr; ++itl) itl->v+=val;
}
void assign(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});
}
ll rank(int l, int r, int k, bool reversed = 0) {
if(reversed)k = r-l+2-k;
split(l);
auto itr = split(r+1), itl = split(l);
ve.clear();
for(;itl!=itr; ++itl)
ve.push_back(make_pair(itl->v, itl->r-itl->l+1));
sort(ve.begin(),ve.end());
for(auto i : ve) {
k -= i.second;
if(k<=0)return i.first;
}
return -1;
}
ll sum(int l, int r, ll ex, ll mod) {
split(l);
auto itr = split(r+1), itl = split(l);
ll res = 0;
for(;itl!=itr;++itl) {
res = (res+1ll*(itl->r-itl->l+1)*power(itl->v, ex, mod)%mod)%mod;
}
return res;
}
void _insert(node x) {
s.insert(x);
}
}odt;
int n, m;
ll seed, vmax, cc[maxn];
ll rnd() {
ll ret = seed;
seed = (seed*7+13)%1000000007;
return ret;
}
void solve() {
scanf("%d%d%lld%lld", &n, &m, &seed, &vmax);
for(int i = 1; i <= n; i++) {
cc[i] = (rnd()%vmax)+1;
//printf("----");
odt._insert(node{i, i, cc[i]});
}
odt._insert(node{n+1, n+1, 0});
for(int i = 1 ;i <= m; i++) {
int op = (rnd()%4)+1;
int l = (rnd()%n)+1, r = (rnd()%n)+1;
ll x, y;
if(l>r) swap(l, r);
if(op == 3) x = (rnd()%(r-l+1))+1;
else x = (rnd()%vmax)+1;
if(op == 4) y = (rnd()%vmax)+1;
if(op == 1) odt.add(l, r, x);
else if(op == 2) odt.assign(l, r, x);
else if(op == 3) printf("%lld\n", odt.rank(l, r, x, 0));
else printf("%lld\n", odt.sum(l, r, x, y));
}
return;
}
int main() {
//ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
//freopen("/Users/hannibal_lecter/Desktop/code/in.txt", "r", stdin);
//freopen("/Users/hannibal_lecter/Desktop/code/out.txt","w",stdout);
#endif
while(Case--) {
solve();
}
return 0;
}