RT,和这篇帖子的写法差不多(92pts),但是死活卡不过去……
/*
* @ Author: Xiaohuba
* @ Usage: OI Problem
* @ Language: C++
*/
#include<bits/stdc++.h>
using namespace std;
/* ---File Head Begin--- */
namespace Xiaohuba_File_Head
{
//def
#define ll long long
#define lll __int128
#define pii pair<int,int>
#define mkp make_pair
#define vi vector<int>
#define vs vector<string>
#define viit vector<int>::iterator
#define pb push_back
#define il inline
#define pch putchar
#define gch getchar
#define Endl putchar('\n')
#define Space putchar(' ')
#define For(x,st,ed) for(int x=(st),END=(ed);x<=END;++x)
#define ForDown(x,st,ed) for(int x=(st),END=(ed);x>=END;--x)
#define sq(x) (x)*(x)
#define Set(a,b) memset(a,b,sizeof(a))
#define Cpy(a,b) memcpy(a,b,sizeof(a))
#define PRIME_MOD 100000007ll
#ifdef ONLINE_JUDGE
#define log2 __lg
#define gcd __gcd
#endif
//io
template <typename T>
il void read(T & tmp){ tmp=0;char c=getchar();bool flg=0;while(!isdigit(c)) flg=c=='-',c=getchar();while(isdigit(c)) tmp=(tmp<<3)+(tmp<<1)+c-'0',c=getchar();if(flg) tmp*=-1; }
template <typename T, typename... Args>
il void read(T &tmp, Args &...tmps){ read(tmp);read(tmps...); }
template <typename T>
il void __write(const T &x){ if(x==0) return;__write(x/10);putchar(x%10+'0'); }
template <typename T>
il void write(const T &x){ if(x==0) putchar('0');if(x<0) putchar('-');__write((x<0 ? -x : x)); }
template <typename T>
il void write_with_space(const T &x){ write(x);Space; }
template <typename T>
il void write_with_endl(const T &x){ write(x);Endl; }
template <typename T, typename... Args>
il void write_with_space(const T &x, const Args &...y){ write_with_space(x);write_with_space(y...); }
il void __getline(istream & istr, string & str){ getline(istr,str);if(*(str.end()-1)=='\r') str.erase(str.end()-1); }
#define getline __getline
};
using namespace Xiaohuba_File_Head;
/* ---File Head End--- */
int n,type;
int tmp,b[40000001],q[40000001],front=1,tail=0;
ll s[40000001];//改成ll or unsigned ll 后 WA #23 24 25,似乎溢出了???
int p[100001],l[100001],r[100001];//输入用,看不出怎么复用数组
int opt[40000001];
il lll cal(int x)
{
return (s[x]<<1) - s[opt[x]];
}
signed main()
{
read(n,type);
if(!type)
{
For(i,1,n) read(tmp),s[i]=s[i-1]+tmp;
} else {
int x,y,z,m;
read(x,y,z,b[1],b[2],m);
For(i,3,n) b[i]=(1ll*b[i-1]*x + 1ll*b[i-2]*y + 1ll*z) % (1ll<<30);
For(i,1,m) read(p[i],l[i],r[i]);
p[0]=0;
For(j,1,m)
For(i,p[j-1]+1,p[j])
tmp=(b[i] % (r[j]-l[j]+1))+l[j],s[i]=s[i-1]+tmp;
}
q[++tail]=0;opt[0]=0;
For(i,1,n)
{
while(front < tail && cal(q[front+1])<=s[i]) ++front;
opt[i]=q[front];
while(front < tail && cal(q[tail])>=cal(i)) --tail;
q[++tail]=i;
}
lll res=0;
for(int i=n; i; i=opt[i])
{
res+=sq(s[i]-s[opt[i]]);
}
write(res);
return 0;
}