Language: C++

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rall(x) (x).rbegin() , (x).rend()
#define len(x) (int)(x).size()



using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int N = (int)3e5 + 100 , inf = 1e9;
int t[N];
ll pref[N] , ansPref[N] , ansSuf[N] , answer[N];
vector<pii>que[N];

struct line{
    ll k , b , x;
    ll get(ll _x){return k * _x + b;}
};
ll cross(line a , line b){
    return ceil((a.b - b.b) / ld(b.k - a.k));
}
vector<line>cht;
void add(line nw){
    while(!cht.empty() && cht.back().k == nw.k && cht.back().b < nw.b)
        cht.pop_back();
    if(!cht.empty() && cht.back().k == nw.k)
        return;
    while(!cht.empty() && cross(nw, cht.back()) <= cht.back().x)
        cht.pop_back();
    if(cht.empty())
        nw.x = -inf;
    else
    nw.x = cross(nw , cht.back());
    cht.pb(nw);
}
ll get(ll x){
    int l = 0 , r = len(cht) - 1;
    while(l != r){
        int mid = (l + r) >> 1;
        if(x >= cht[mid].x){
            if(l + 1 == r) l = r;
            else l = mid;
        }else
            r = mid;
    }
    if(x >= cht[r].x) ++r;
    --r;
    return cht[r].get(x);
}
void clr(){
    cht.clear();
}
void solve(int l , int r){
    if(l == r){
        for(auto u : que[l]){
            answer[u.se] = max(answer[u.se] , ansPref[l - 1] + ansSuf[l + 1] + max(0 , 1 - u.fi));
        }
        return;
    }
    int mid = (l + r) >> 1;
    solve(l , mid);
    solve(mid + 1 , r);
    clr();
    /// we wonder to update for [l , mid]
    for(int i = r + 1; i >= mid + 1; --i){
        add({-i , 1ll*i*(i+1)/2+ansSuf[i]-pref[i-1]});
    }
    ll curbest = 0;
    for(int i = l; i <= mid; ++i){
        curbest = max(curbest , get(i) + 1ll*i*(i-1)/2 + pref[i-1]);
        for(auto u : que[i]){
            answer[u.se] = max(answer[u.se] , curbest + t[i] - u.fi);
        }
    }
    clr();
    for(int i = l - 1; i <= mid; ++i){
        add({i , 1ll*i*(i-1)/2+ansPref[i]+pref[i]});
    }
    curbest = 0;
    for(int i = r; i >= mid + 1; --i){
        curbest = max(curbest , get(-i)+1ll*i*(i+1)/2-pref[i]);
        for(auto u : que[i]){
            answer[u.se] = max(answer[u.se] , curbest + t[i] - u.fi);
        }
    }
    /// omamamamagat

}
signed main(){
    ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    int n;cin >> n;
    for(int i = 1 ; i <= n; ++i){
        cin >> t[i] , pref[i] = pref[i - 1] + t[i];
    }
    int m;cin >> m;
    for(int i = 0 ; i < m; ++i){
        int p , x;cin >> p >> x, p;
        que[p].pb({x , i});
    }
    /// calculating ansPref
    add({0 , 0});
    for(int i = 1; i <= n; ++i){
        /// firstly if we have
        ll cur = get(-i)+1ll*i*(i+1)/2-pref[i];
        if(i)
        ansPref[i] = max(ansPref[i-1],cur);
        /// update cht
        add({i , 1ll*i*(i-1)/2+ansPref[i]+pref[i]});
    }
    /// next is mem
    clr();
    /// calculate for suf
    pref[n + 1] = pref[n];
    add({-(n+1),1ll*(n+1)*(n+2)/2+ansSuf[n+1]-pref[n]});
    for(int i = n; i >= 0; --i){
        ll cur = get(i) + 1ll*i*(i-1)/2 + pref[i-1];
        ansSuf[i] = max(ansSuf[i + 1] , cur);
        add({-i , 1ll*i*(i+1)/2+ansSuf[i]-pref[i-1]});
    }
    clr();
    solve(1 , n);
    for(int i = 0 ; i < m; ++i)
        cout << answer[i] << '\n';
    return 0;
}



Compiler output:
a.cc: In function 'void solve(long long int, long long int)':
a.cc:62:83: error: no matching function for call to 'max(int, long long int)'
   62 |             answer[u.se] = max(answer[u.se] , ansPref[l - 1] + ansSuf[l + 1] + max(0 , 1 - u.fi));
      |                                                                                ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:62:83: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   62 |             answer[u.se] = max(answer[u.se] , ansPref[l - 1] + ansSuf[l + 1] + max(0 , 1 - u.fi));
      |                                                                                ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303: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:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:62:83: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   62 |             answer[u.se] = max(answer[u.se] , ansPref[l - 1] + ansSuf[l + 1] + max(0 , 1 - u.fi));
      |                                                                                ~~~^~~~~~~~~~~~~~