原题链接
题意:
长度为的序列,
次询问,求区间带权众数,强制在线。
思路:
与不带权的差别不大,多乘一个价值就好了。强制在线后每次计算完要判断一下两者的大小。
代码:
// Problem: 仓鼠与珂朵莉
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/problem/213938
// Memory Limit: 1024000 MB
// Time Limit: 6000 ms
// Author:Cutele
//
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
const int maxn=100000+7,maxm=2e5+7,inf=0x3f3f3f3f;
int a[maxn],belong[maxn],id[maxn];//a原数组,belong是属于哪个块,id表示离散化后对应的数值
int block,n,q,num,m;///块的大小,数组大小,询问次数,块的数量,去重后的数字个数
int mp[maxn];///标记数组
ll dp[1100][1100];//标记第i->j块之间价值的最大值
int L[1100],R[1100];//分块后每块的左端点跟右端点。
vector<int>g[maxn];
ll Find(int x,int l,int r){
return upper_bound(g[x].begin(), g[x].end(), r) - lower_bound(g[x].begin(), g[x].end(), l);
}
ll query(ll l,ll r){
ll res=0;
ll bl=belong[l],br=belong[r];
if(bl==br){
rep(i,l,r){
ll tmp=Find(id[i],l,r);
res=max(res,tmp*a[i]);
}
}
else{
res=dp[bl+1][br-1];
rep(i,l,R[bl]){
ll tmp=Find(id[i],l,r);
res=max(res,tmp*a[i]);
}
rep(i,L[br],r){
ll tmp=Find(id[i],l,r);
res=max(res,tmp*a[i]);
}
}
return res;
}
int main(){
n=read,q=read;
block=sqrt(n);
num=n/block+(n%block>0);
vector<int>v;//离散化用
rep(i,1,n) a[i]=read,v.push_back(a[i]);
//离散化过程
sort(v.begin(),v.end());
v.erase((unique(v.begin(),v.end())),v.end());
m=v.size();
rep(i,1,n){
belong[i]=(i-1)/block+1;//记录每个数所在的块的编号
id[i]=lower_bound(v.begin(),v.end(), a[i])-v.begin();///记录离散化后的数值。
g[id[i]].push_back(i);
}
//预处理
for(int i=1;i<=num;i++){
L[i]=(i-1)*block+1,R[i]=i*block;
memset(mp,0,sizeof mp);
ll tmp=0;
for(int j=(i-1)*block+1;j<=n;j++){
mp[id[j]]++;
tmp=max(tmp,mp[id[j]]*a[j]*1ll);
dp[i][belong[j]]=tmp;
}
}
ll las=0;
rep(i,1,q){
ll l=read,r=read;
l=(l^las)%n+1ll,r=(r^las)%n+1ll;
if(l>r) swap(l,r);
las=query(l,r);
printf("%lld\n",las);
}
return 0;
}