珂朵莉树

比较暴力的数据结构,可以非常方便地解决含有区间赋值操作的几乎所有区间修改和查询问题,但可以被刻意构造数据hack到 \(\mathcal{O}(n^2)\) 的时间复杂度,\(set\) 实现的期望时间复杂度为 \(\mathcal{O}(n\log \log n)\) 。


https://codeforces.com/contest/896/problem/C

https://www.luogu.com.cn/problem/CF896C


//author:kzssCCC

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

const int MOD = 1e9+7;

class node{
public:
	int l,r;
	mutable ll v;

	node(int l,int r,ll v):l(l),r(r),v(v){}

	bool operator<(const node& other) const{
		return l < other.l;
	}
};

void solve(){
	int n,m;
	ll seed,vmx;
	cin >> n >> m >> seed >> vmx;

	auto rng = [&](){
		ll res = seed;
		seed = (seed*7+13)%MOD;
		return res;
	};

	vector<ll> a(n+1);
	for (int i=1;i<=n;i++){
		a[i] = rng()%vmx+1;
	}

	set<node> st;

    //区间左闭右开
	st.insert({1,n+1,-1});
  
    //把当前[l,r)的区间分割成[l,x)和[x,r),返回后一个区间的迭代器
	auto split = [&](int x){
		auto it = st.lower_bound({x,-1,-1});

		if (it!=st.end() && x==it->l){
			return it;
		}

		it--;
		int l=it->l,r=it->r;
		ll v = it->v;

		st.erase(it);
		st.insert({l,x,v});

		return st.insert({x,r,v}).first;
	};
  
    //区间赋值操作,注意一定要先split(r+1)
	auto assign = [&](int l,int r, ll v){
		auto itr = split(r+1);
		auto itl = split(l);

		st.erase(itl,itr);
		st.insert({l,r+1,v});
	};

	for (int i=1;i<=n;i++){
		assign(i,i,a[i]);
	}

	while (m--){
		int op = rng()%4+1;
		int l = rng()%n+1;
		int r = rng()%n+1;
		ll x,y;

		if (l>r){
			swap(l,r);
		}

		if (op==3){
			x = rng()%(r-l+1)+1;
		}
		else{
			x = rng()%vmx+1;
		}

		if (op==4){
			y = rng()%vmx+1;
		}

		if (op==1){
			auto itr = split(r+1);
			auto itl = split(l);

			while (itl!=itr){
				itl->v += x;
				itl++;
			}		
		}
		else if (op==2){
			assign(l,r,x);			
		}
		else if (op==3){
			vector<pair<ll,int>> wait;
			auto itr = split(r+1); 	
			auto itl = split(l);

			while (itl!=itr){
				wait.emplace_back(itl->v,itl->r-itl->l);
				itl++;
			}

			sort(wait.begin(),wait.end());
			ll sum = 0;

			for (auto& [v,c]:wait){
				if (sum+c>=x){
					cout << v << '\n';
					break;
				}
				else{
					sum += c;
				}
			}		
		}
		else{
			auto qpow = [&](ll a,ll b){
				ll res = 1;

				while (b){
					if (b&1) res = res*a%y;

					a = a*a%y;
					b >>= 1;
				}

				return res;
			};

			auto itr = split(r+1); 	
			auto itl = split(l);
			ll res = 0;

			while (itl!=itr){
				res = (res+qpow(itl->v%y,x)*(itl->r-itl->l)%y)%y;

				itl++;
			}

			cout << res << '\n';
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int t = 1;
	// cin >> t;
	while (t--) solve();

	return 0;
}
posted @ 2026-04-19 12:19  kzssCCC  阅读(18)  评论(0)    收藏  举报