// Mivik 2019.10.24
// ***************
// 下文中 R 代表读入
// ***************
#include <iostream>
#include <cstdio>
#define endl '\n'
#define USING_R(fn,n) n fn() {static n _R;static char _C;static bool _F;_F=0;_R=0;_C=GG();while((_C<'0'||_C>'9')&&_C!='-'&&_C!='\0')_C=GG();if(_C=='-')_F=1,_C=GG();while(_C>='0'&&_C<='9')_R=(_R<<3)+(_R<<1)+_C-'0',_C=GG();if (_F) _R=-_R;return _R;}
#define USING_T(a) const char* __DATA=a;char TGC(){return *__DATA++;}
#define P(a) cout<<#a"="<<(a)<<endl
#define R Read()
#define GG getchar
#define nmax 500005
#define lmax 19
typedef long long qe;
using namespace std;
USING_R(Read,int)
int n,q;
int a[nmax];
int sta[nmax];
int l2[nmax];
int st[nmax][lmax+1];
qe sum[nmax];
int fa[nmax];
int main() {
int i,j,w,x,y;
n=R,q=R;
for (i=1;i<=n;i++) {
if (i!=1) l2[i]=l2[i>>1]+1;
a[i]=j=R;
st[i][0]=i;
while (sta[0]&&j>a[w=sta[sta[0]]]) fa[w]=i,--sta[0];
sta[++sta[0]]=i;
}
while (sta[0]) fa[w=sta[sta[0]]]=n+1,--sta[0];
fa[n+1]=n+1;
for (i=n;i>=1;i--) sum[i]=sum[fa[i]]+(qe)(fa[i]-i)*a[i];
int lim;
int cp;
for (j=1;j<=lmax;j++) {
cp=1<<(j-1);
lim=n-(cp<<1)+1;
if (lim<1) break;
for (i=1;i<=lim;i++) {
st[i][j]=st[i][j-1];
if (a[w=st[i+cp][j-1]]>a[st[i][j]]) st[i][j]=w;
}
}
qe lastans=0;
int target;
while (q--) {
x=1+(lastans^R)%n;
y=1+((lastans+1)^R)%(n-x+1);
w=l2[y];
target=st[x][w];
if (a[j=st[x+y-(1<<w)][w]]>a[target]) target=j;
lastans=sum[x]-sum[target]+(qe)a[target]*(x+y-target);
cout<<lastans<<endl;
}
return 0;
}剪贴板
剪贴板 trfv5br4
剪贴板信息
剪贴板信息
- 收录
- 更新