[UNR #1] Fight for the Holy Grail

SOL:

   This problem looks like a multi-order difference, and in fact this problem does just that.

   We run through the monotonic stack twice to find that the farthest left is smaller than it and the farthest right is smaller than it.

   Calm down and find that if a point is larger than both its left and right sides, it will contribute more to the next line. If only one side is larger, it will remain unchanged, and if it is smaller than both sides, it will become smaller.

   Just make a difference.

  

#include<bits/stdc++.h>
#define sight(c) ('0'<=c&&c<='9')
#define N 2000007
#define LL long long
#define inf (INT_MAX)
#define mo 998244353
//#define int LL
using namespace std;
inline void read(int &x){
    static char c;
    for (c=getchar();!sight(c);c=getchar());
    for (x=0;sight(c);c=getchar())x=x*10+c-48;
}
void write(LL x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
struct Node{
    int id,key;
    inline bool operator <(const Node&X)const{
        return key<X.key;
    }
}qjj[N];
int sta[N],top,p[N],g[N],n,a[N];
LL ans,anw,sum,xxoo[N],dla[N];
signed main () {
    read(n);
    for (int i=1;i<=n;i++) read(a[i]),ans+=a[i];
    a[0]=inf; a[n+1]=inf;
    for (int i=1;i<=n+1;i++) {
        while (top&&a[sta[top]]<a[i]) p[sta[top--]]=i;
        sta[++top]=i; 
    } top=0;
    for (int i=n;~i;i--) {
        while (top&&a[sta[top]]<=a[i]) g[sta[top--]]=i;
        sta[++top]=i; 
    }
    dla[0]=ans;
    for (int i=1;i<=n;i++) {
        xxoo[min(p[i]-i-1,i-g[i]-1)]-=a[i]; 
        xxoo[max(p[i]-i-1,i-g[i]-1)]-=a[i];
        xxoo[p[i]-g[i]-1]+=a[i];
        
    }
    for (int i=1;i<=n;i++) dla[i]+=dla[i-1]+xxoo[i-1];
    anw=ans%mo;
    for (int i=1;i<n;i++) ans=ans+dla[i],anw^=ans%mo;
    writeln (anw); return  0 ;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726786&siteId=291194637