Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define maxn 300005
#define LL long long
using namespace std;

int n,m;
template<class T>inline void read(T &res)
{
	char ch;
	for(;!isdigit(ch=getchar(););
	for(res=ch-'0';isdigit(ch=getchar());res=res*10+ch-'0');
}

LL sum[maxn],dp[2][maxn];
int RMQ[19][maxn],Q[3][maxn],rpt[3][maxn],L[3],R[3],lg[maxn];
inline int Query(const int &u,const int &v)
{ if(u>v) return 0;int tmp = lg[v-u+1] ;return min(RMQ[tmp][u] , RMQ[tmp][v-(1<<tmp)+1]); }

inline bool check0(int a,int pt,int b)
{
    return dp[a][0] + 1ll * (pt - a) * (pt - a + 1) / 2 - sum[pt] + sum[a] < dp[b][0] + 1ll * (pt - b) * (pt - b + 1) / 2 - sum[pt] + sum[b];
}

inline bool check1(int a,int pt,int b)
{
	return dp[a][0] + 1ll * (pt - a) * (pt - a + 1) / 2 - sum[pt] + sum[a] + Query(a+1,pt) < dp[b][0] + 1ll * (pt - b) * (pt - b + 1) / 2 - sum[pt] + sum[b] + Query(b+1, pt);
}

inline bool check2(int a,int pt,int b)
{
    return dp[a][1] + 1ll * (pt - a) * (pt - a + 1) / 2 - sum[pt] + sum[a] < dp[b][1] + 1ll * (pt - b) * (pt - b + 1) / 2 - sum[pt] + sum[b];
}

int main()
{
	read(n);
	for(int i=1;i<=n;i++) read(sum[i]) ,sum[i]+=sum[i-1];
	memset(dp,-0x3f,sizeof dp);
	memset(RMQ,0x3f,sizeof RMQ);
	for(int sum=1,i=0;sum<maxn;sum<<=1,i++) lg[sum]=i;
	for(int i=1;i<maxn;i++) lg[i] = max(lg[i],lg[i-1]);
	read(m);
	for(int i=1,a,b;i<=m;i++)
	{
		read(a),read(b);
		RMQ[0][a] = min(RMQ[0][a] , b - sum[a] + sum[a+1]);
	}
	for(int j=1;j<19;j++)
		for(int i=1;i<=n;i++)
			RMQ[j][i] = min(RMQ[j-1][i] , RMQ[j-1][i+(1<<j-1)]);

	for(int i=0;i<3;i++)
		Q[i][R[i]]] = 0 , rpt[i][R[i]++] = n;
    for(int i=1;i<=n;i++)
	{
		for(int j=0;j<3;j++)
			(rpt[j][L[j]] < i) && (L[j] ++);
        dp[i][0] = dp[rpt[Q[0][L[0]]]][0] + 1ll * (i - Q[0][L[0]]) * (i - Q[0][L[0]] + 1) / 2 - sum[i] + sum[Q[0][L[0]]];
        dp[i][1] = max(dp[rpt[Q[1][L[1]]]][0] + 1ll * (i - Q[1][L[1]]) * (i - Q[1][L[1]] + 1) / 2 - sum[i] + sum[Q[1][L[1]]] + Query(Q[1][L[1]]+1 , i),
		dp[rpt[Q[2][L[2]]]][1] + 1ll * (i - Q[2][L[2]]) * (i - Q[2][L[2]] + 1) / 2 - sum[i] + sum[Q[2][L[2]]]);

        for(;L[0] < R[0] - 1 && check0(Q[0][R[0]] , rpt[0][R[0]-1]+1 , i);) R[0] --;
        for(;L[1] < R[1] - 1 && check0(Q[1][R[1]] , rpt[1][R[1]-1]+1 , i);) R[1] --;
        for(;L[2] < R[2] - 1 && check0(Q[2][R[2]] , rpt[2][R[2]-1]+1 , i);) R[2] --;
	}

}


Compiler output:
a.cc: In function 'void read(T&)':
a.cc:13:35: error: expected ')' before ';' token
   13 |         for(;!isdigit(ch=getchar(););
      |                      ~            ^
      |                                   )
a.cc: In function 'int main()':
a.cc:49:32: error: no matching function for call to 'min(int&, long long int)'
   49 |                 RMQ[0][a] = min(RMQ[0][a] , b - sum[a] + sum[a+1]);
      |                             ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:49:32: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   49 |                 RMQ[0][a] = min(RMQ[0][a] , b - sum[a] + sum[a+1]);
      |                             ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:49:32: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   49 |                 RMQ[0][a] = min(RMQ[0][a] , b - sum[a] + sum[a+1]);
      |                             ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:56:27: error: expected ';' before ']' token
   56 |                 Q[i][R[i]]] = 0 , rpt[i][R[i]++] = n;
      |                           ^
      |                           ;
a.cc:61:22: error: invalid types 'long long int [2][300005][int [300005]]' for array subscript
   61 |         dp[i][0] = dp[rpt[Q[0][L[0]]]][0] + 1ll * (i - Q[0][L[0]]) * (i - Q[0][L[0]] + 1) / 2 - sum[i] + sum[Q[0][L[0]]];
      |                      ^
a.cc:62:26: error: invalid types 'long long int [2][300005][int [300005]]' for array subscript
   62 |         dp[i][1] = max(dp[rpt[Q[1][L[1]]]][0] + 1ll * (i - Q[1][L[1]]) * (i - Q[1][L[1]] + 1) / 2 - sum[i] + sum[Q[1][L[1]]] + Query(Q[1][L[1]]+1 , i),
      |                          ^
a.cc:63:19: error: invalid types 'long long int [2][300005][int [300005]]' for array subscript
   63 |                 dp[rpt[Q[2][L[2]]]][1] + 1ll * (i - Q[2][L[2]]) * (i - Q[2][L[2]] + 1) / 2 - sum[i] + sum[Q[2][L[2]]]);
      |                   ^