如果我们能求出来每个区间个数的最大分值,那就可以用线段树维护这个东西 然后出答案了
然后这个的求法和(luogu4269)Snow Boots G非常类似,就是我们把数大小排个序,每次都拿<=x的位置去合并那个并查集,同时维护个数和大小
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define pa pair<double,int>
#define CLR(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e6+; inline char gc(){
return getchar();
static const int maxs=<<;static char buf[maxs],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,maxs,stdin),p1==p2)?EOF:*p1++;
}
inline ll rd(){
ll x=;char c=gc();bool neg=;
while(c<''||c>''){if(c=='-') neg=;c=gc();}
while(c>=''&&c<='') x=(x<<)+(x<<)+c-'',c=gc();
return neg?(~x+):x;
} struct Node{
int v,i;
}p[maxn];
int fa[maxn],siz[maxn],N,T;
pa ma[maxn<<];
bool flag[maxn];
int nowcnt;
ll nowsum; inline bool cmp(Node a,Node b){return a.v<b.v;} inline int getf(int x){return x==fa[x]?x:getf(fa[x]);} inline void uni(int x){
flag[x]=;siz[x]=;
nowcnt++;
if(flag[x-]){
int a=getf(x-);
nowsum-=1ll*siz[a]*siz[a],siz[x]+=siz[a],fa[a]=x;
nowcnt--;
}if(flag[x+]){
int b=getf(x+);
nowsum-=1ll*siz[b]*siz[b],siz[x]+=siz[b],fa[b]=x;
nowcnt--;
}nowsum+=1ll*siz[x]*siz[x];
} inline void update(int p){ma[p]=max(ma[p<<],ma[p<<|]);} inline void change(int p,int l,int r,int x,pa y){
if(l==r) ma[p]=max(ma[p],y);
else{
int m=l+r>>;
if(x<=m) change(p<<,l,m,x,y);
else change(p<<|,m+,r,x,y);
update(p);
}
} inline pa query(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return ma[p];
int m=l+r>>;pa re=make_pair(,);
if(x<=m) re=query(p<<,l,m,x,y);
if(y>=m+) re=max(re,query(p<<|,m+,r,x,y));
return re;
} int main(){
//freopen("","r",stdin);
int i,j,k;
N=rd(),T=rd();
for(i=;i<=N;i++){
p[i].v=rd(),p[i].i=i;
}sort(p+,p+N+,cmp);
for(i=;i<=N;i++) fa[i]=i;
for(i=;i<=N;i++){
uni(p[i].i);
if(p[i].v!=p[i+].v) change(,,N,nowcnt,make_pair(1.0*nowsum/p[i].v,p[i].v));
}
ll lastans=;
for(i=;i<=T;i++){
ll a=rd(),b=rd(),x=rd(),y=rd();
int l=(a*lastans+x-)%N+,r=(b*lastans+y-)%N+;
if(l>r) swap(l,r);
pa re=query(,,N,l,r);
if(re.first==){
printf("-1 -1\n");
}else
printf("%lld %d\n",(ll)(re.first*re.second+0.5),re.second);
printf("%d %d %d\n",l,r,lastans);
if(re.first==) lastans=;
else lastans=((ll)(re.first*re.second+0.5))%N*re.second%N;
}
return ;
}