C. Willem, Chtholly and Seniorious
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
— Willem...
— What's the matter?
— It seems that there's something wrong with Seniorious...
— I'll have a look...
Seniorious is made by linking special talismans in particular order.
After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.
Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is an integer ai.
In order to maintain it, Willem needs to perform m operations.
There are four types of operations:
- 1 l r x: For each i such that l ≤ i ≤ r, assign ai + x to ai.
- 2 l r x: For each i such that l ≤ i ≤ r, assign x to ai.
- 3 l r x: Print the x-th smallest number in the index range [l, r], i.e. the element at the x-th position if all the elements ai such that l ≤ i ≤ r are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1 ≤ x ≤ r - l + 1.
- 4 l r x y: Print the sum of the x-th power of ai such that l ≤ i ≤ r, modulo y, i.e. .
Input
The only line contains four integers n, m, seed, vmax (1 ≤ n, m ≤ 105, 0 ≤ seed < 109 + 7, 1 ≤ vmax ≤ 109).
The initial values and operations are generated using following pseudo code:
def rnd(): ret = seed seed = (seed * 7 + 13) mod 1000000007 return ret for i = 1 to n: a[i] = (rnd() mod vmax) + 1 for i = 1 to m: op = (rnd() mod 4) + 1 l = (rnd() mod n) + 1 r = (rnd() mod n) + 1 if (l > r): swap(l, r) if (op == 3): x = (rnd() mod (r - l + 1)) + 1 else: x = (rnd() mod vmax) + 1 if (op == 4): y = (rnd() mod vmax) + 1
Here op is the type of the operation mentioned in the legend.
Output
For each operation of types 3 or 4, output a line containing the answer.
Examples
input
Copy
10 10 7 9
output
Copy
2 1 0 3
input
Copy
10 10 9 9
output
Copy
1 1 3 3
Note
In the first example, the initial array is {8, 9, 7, 2, 3, 1, 5, 6, 4, 8}.
The operations are:
- 2 6 7 9
- 1 3 10 8
- 4 4 6 2 4
- 1 4 5 8
- 2 1 7 1
- 4 7 9 4 4
- 1 2 7 9
- 4 5 8 1 1
- 2 5 7 5
- 4 3 10 8 5
思路:对于本题的四种操作,我们发现,普通额树状数组、线段树等都难以解决,或者说实现起来极其复杂,在出题人的题解中,他发明了一种数据结构实现相对较简单,并且有些许暴力,并将其命名为珂朵莉树。
本文章不再讲解珂朵莉树的具体原理,网上已经有许多不错的讲解啦,我们只需要知道在随机数据下,珂朵莉树的时间复杂度能做到O(nloglogn)即可,详细证明在珂朵莉树的复杂度分析中。
#include<set>
#include<queue>
#include<vector>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 600005
#define ll long long
struct node{
ll l,r;
mutable ll v;
node(ll l,ll r,ll v):l(l),r(r),v(v){}
bool operator <(const node &o) const {
return l<o.l;
}
};
set<node> tree;
ll n,m,seed,vmax;
ll rnd(){
ll ret=seed;
seed=(seed*7+13)%1000000007;
return ret;
}
ll q(ll x,ll y,ll mod){
ll res=1;
x%=mod;
while(y){
if(y&1)
res=res*x%mod;
x=x*x%mod;
y/=2;
}
return res;
}
set<node>::iterator split(ll pos){
auto it=tree.lower_bound(node(pos,0,0));
if(it!=tree.end() && it->l==pos)
return it;
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 add(ll l,ll r,ll v){
auto end=split(r+1);
for(auto it=split(l);it!=end;it++)
it->v+=v;
}
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));
}
ll kth(ll l,ll r,ll k){
auto end=split(r+1);
vector<pair<ll,ll>> v;
for(auto it=split(l);it!=end;it++)
v.push_back(make_pair(it->v,it->r-it->l+1));
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
k-=v[i].second;
if(k<=0)
return v[i].first;
}
}
ll sumOfPow(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+q(it->v,x,y)*(it->r-it->l+1))%y;
return tot;
}
int main(void){
ll k,l,r,x,y;
scanf("%lld%lld%lld%lld",&n,&m,&seed,&vmax);
for(int i=1;i<=n;i++){
int r=rnd();
tree.insert(node(i,i,r%vmax+1));
}
for(int i=1;i<=m;i++){
k=rnd()%4+1;
l=rnd()%n+1;
r=rnd()%n+1;
if(l>r)
swap(l,r);
if(k==3)
x=rnd()%(r-l+1)+1;
else
x=rnd()%vmax+1;
if(k==4)
y=rnd()%vmax+1;
switch (k){
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",sumOfPow(l,r,x,y));
}
}
return 0;
}