#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"cmath" #include"vector" #include"map" #include"set" #include"queue" usingnamespace std; #define MAXN 1000006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedeflonglong ll; int n , q; int A[MAXN] , p[19][MAXN] , L[MAXN] , R[MAXN] , lg[MAXN]; ll f[MAXN] , F[MAXN] , g[MAXN] , G[MAXN]; int stk[MAXN] , top;
namespace gen{ typedefunsignedlonglong ull; ull s,a,b,c,lastans=0; voidin( ){ cin >> s >> a >> b >> c; } ull rand(){ return s^=(a+b*lastans)%c; } };
voidsolve(){ int typ; cin >> n >> q >> typ; lg[0] = -1; rep( i , 1 , n ) scanf("%d",&A[i]) , p[0][i] = i , lg[i] = lg[i >> 1] + 1; rep( i , 1 , 18 ) for( int j = 1 ; j + ( 1 << i ) - 1 <= n ; ++ j ) p[i][j] = ( A[p[i - 1][j]] < A[p[i - 1][j + ( 1 << i - 1 )]] ? p[i - 1][j] : p[i - 1][j + ( 1 << i - 1 )] ); rep( i , 1 , n ) { while( top && A[stk[top]] > A[i] ) R[stk[top]] = i , -- top; stk[++ top] = i; } while( top ) R[stk[top]] = n + 1 , -- top; per( i , n , 1 ) { while( top && A[stk[top]] > A[i] ) L[stk[top]] = i , -- top; stk[++ top] = i; } while( top ) L[stk[top]] = 0 , -- top; per( i , n , 1 ) F[i] = F[R[i]] + A[i] * 1ll * ( R[i] - i ) , f[i] = f[i + 1] + F[i]; rep( i , 1 , n ) G[i] = G[L[i]] + A[i] * 1ll * ( i - L[i] ) , g[i] = g[i - 1] + G[i]; int l , r , pos , len; unsignedlonglong re = 0 , res = 0; if( !typ ) { while( q-- ) { scanf("%d%d",&l,&r); len = lg[r - l + 1]; pos = ( A[p[len][l]] < A[p[len][r - ( 1 << len ) + 1]] ? p[len][l] : p[len][r - ( 1 << len ) + 1] ); res = f[l] - f[pos] - ( pos - l ) * F[pos] + g[r] - g[pos] - ( r - pos ) * G[pos] + ( pos - l + 1 ) * 1ll * ( r - pos + 1 ) * A[pos]; re ^= res; } } else { gen::in(); while( q-- ) { l = gen::rand() % n + 1 , r = gen::rand() % n + 1; if( l > r ) swap( l , r ); len = lg[r - l + 1]; pos = ( A[p[len][l]] < A[p[len][r - ( 1 << len ) + 1]] ? p[len][l] : p[len][r - ( 1 << len ) + 1] ); res = f[l] - f[pos] - ( pos - l ) * F[pos] + g[r] - g[pos] - ( r - pos ) * G[pos] + ( pos - l + 1 ) * 1ll * ( r - pos + 1 ) * A[pos]; gen::lastans = res; re ^= res; } } cout << re << endl; }
/* void solve() { srand( (long long) new char ); n = 10 , m = 10; cout << n << ' ' << m << endl; rep( i , 1 , n ) printf("%d ",rand() % 2000000000 - 1000000000 + 1); puts(""); rep( i , 1 , m ) { int l = rand() % n + 1 , r = rand() % n + 1; if( l > r ) swap( l , r ); printf("%d %d\n",l,r); } } */