solution
- 对于每个i用单调栈求出小于i的第一个比他大的数l[i],以及大于i的第一个比他大的数r[i]。
- i一定是l[i]-r[i]中最大的数,1-a+1分别加(1-a+1)*a[i],a+2 - b+1加 a+1 a[i] a+2-a+b+1 递减加
- 用二阶差分o(1)完成修改
- 用两遍前缀和求出答案
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e7+100;
const int inf=0x3f3f3f3f;
int a[N];
int n,p;
void gen()
{
int x,y,z;
scanf("%d%d%d%d%d",&n,&p,&x,&y,&z);
int tmp=1;
for(int i=1;i<=n;++i)
{
tmp=((long long)tmp*x+y)%z;
a[i]=tmp;
}
}
int s[N],top;
int c[N];
int l[N],r[N];
int main()
{
freopen("max.in","r",stdin);
freopen("max.out","w",stdout);
gen();
int i,j;
a[n+1]=inf;
a[0]=inf;
top=1;
for(i=1;i<=n+1;i++)
{
while(top&&a[i]>=a[s[top]])
{
r[s[top--]]=i;
}
s[++top]=i;
}
top=1;
for(i=n;i>=0;i--)
{
while(top&&a[i]>a[s[top]])
{
l[s[top--]]=i;
}
s[++top]=i;
}
int x,y;
for(i=1;i<=n;i++)
{
x=i-l[i];
y=r[i]-i;
x--;y--;
if(x>y) swap(x,y);
c[1]=(c[1]+a[i])%p;
c[x+2]=(c[x+2]-a[i]+p)%p;
c[y+2]=(c[y+2]-a[i]+p)%p;
c[x+y+3]=(c[x+y+3]+a[i])%p;
}
for(int i=1;i<=n;++i)c[i]=(c[i]+p)%p;
for(i=1;i<=n;i++) c[i]=(c[i-1]+c[i])%p;
for(i=1;i<=n;i++) c[i]=(c[i-1]+c[i])%p;
int ans=0;
for(i=1;i<=n;i++) ans=ans^c[i];
cout<<ans<<endl;
return 0;
}