Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MN = 300010;

struct CHT {
    vector<ll> M, B;
    void init() {
        M.clear();
        B.clear();
    }
    bool bad(int l1, int l2, int l3) {
        return (M[l1] - M[l2]) * (B[l3] - B[l1]) <= (M[l1] - M[l3]) * (B[l2] - B[l1]);
    }
    void add(ll m, ll b) {
        M.push_back(m);
        B.push_back(b);
        while(M.size() >= 3 && bad(M.size() - 3, M.size() - 2, M.size() - 1)) {
            M.erase(M.end() - 2);
            B.erase(B.end() - 2);
        }
        if(M.size() == 2 && M[0] == M[1]) {
            M.erase(M.end() - 2);
            B.erase(B.end() - 2);
        }
    }
    ll query(ll x) {
        if(M.size() == 0) return 1e18;
        int s = 0, e = (int)M.size() - 2, p = 0;
        while(s <= e) {
            int m = (s + e)>>1;

            if((M[m] - M[m + 1]) * x >= B[m + 1] - B[m]) {
                p = m + 1;
                s = m + 1;
            }
            else e = m - 1;
        }
        return M[p] * x + B[p];
    }
} cht;

int N, M;
ll T[MN], psum[MN], dp1[MN], dp2[MN], ans[MN];

struct BIT {
    vector<ll> tree;
    void init() {
        tree = vector<ll>(4 * N, -1e18);
    }
    void upd(int a, int b, ll val, int l, int r, int n) {
        if(b < l || r < a) return;
        if(a <= l && r <= b) {
            tree[n] = max(tree[n], val);
            return;
        }
        int m = (l + r)>>1;
        upd(a, b, val, l, m, 2*n);
        upd(a, b, val, m + 1, r, 2*n + 1);
    }
    ll quer(int idx, int l, int r, int n) {
        if(idx < l || r < idx) return -1e18;
        ll ret = tree[n];
        if(l == r) return ret;
        int m = (l + r)>>1;
        ret = max(ret, quer(idx, l, m, 2*n));
        ret = max(ret, quer(idx, m + 1, r, 2*n + 1));
        return ret;
    }
} bit1, bit2;

void solve(int l, int r, int t, BIT &bit, ll *dp1, ll *dp2) {
    if(l > r) return;
    int m = (l + r + t)>>1;

    solve(m + 1, r, bit, dp1, dp2);

    for(int i = m; i >= l; i--) {
        ll t = 1LL * i * i - i + (i? psum[i - 1] : 0) - cht.query(i) + (i? dp2[i - 1] : 0);
        bit.upd(i, m, t, 0, N - 1, 1);
    }

    cht.add(2 * m, - 1LL * m * m - m + (m? psum[m - 1] : 0) - dp1[m]);
    solve(l, m - 1, bit, dp1, dp2);

    for(int i = m - 1; i >= l; i--) {
        cht.add(2 * i, - 1LL * i * i - i + (i? psum[i - 1] : 0) - dp1[i]);
    }
}

int main() {
    scanf("%d", &N);

    for(int i = 0; i < N; i++) {
        scanf("%lld", &T[i]);
        T[i] *= 2;
    }
    for(int i = 0; i < N; i++) {
        psum[i] = T[i];
        if(i) psum[i] += psum[i - 1];
    }

    cht.init();
    cht.add(2 * N, - 1LL * N * N - N + psum[N - 1]);

    for(int i = N - 1; i >= 0; i--) {
        dp1[i] = 1LL * i * i - i + (i? psum[i - 1] : 0) - cht.query(i);
        dp1[i] = max(dp1[i], dp1[i + 1]);
        cht.add(2 * i, - 1LL * i * i - i + (i? psum[i - 1] : 0) - dp1[i]);
    }

    reverse(T, T + N);

    for(int i = 0; i < N; i++) {
        psum[i] = T[i];
        if(i) psum[i] += psum[i - 1];
    }

    cht.init();
    cht.add(2 * N, - 1LL * N * N - N + psum[N - 1]);

    for(int i = N - 1; i >= 0; i--) {
        dp2[i] = 1LL * i * i - i + (i? psum[i - 1] : 0) - cht.query(i);
        dp2[i] = max(dp2[i], dp2[i + 1]);
        cht.add(2 * i, - 1LL * i * i - i + (i? psum[i - 1] : 0) - dp2[i]);
    }

    bit1.init();
    cht.init();
    cht.add(2 * N, - 1LL * N * N - N + psum[N - 1]);
    reverse(dp1, dp1 + N);
    solve(0, N - 1, 1, bit1, dp2, dp1);

    reverse(T, T + N);
    reverse(dp1, dp1 + N);
    reverse(dp2, dp2 + N);

    for(int i = 0; i < N; i++) {
        psum[i] = T[i];
        if(i) psum[i] += psum[i - 1];
    }

    bit2.init();
    cht.init();
    cht.add(2 * N, - 1LL * N * N - N + psum[N - 1]);
    solve(0, N - 1, 0, bit2, dp1, dp2);

    scanf("%d", &M);

    for(int i = 0; i < M; i++) {
        int p, x; scanf("%d %d", &p, &x);
        p--;
        x *= 2;

        ll tmp = (p? dp2[p - 1] : 0) + dp1[p + 1];
        tmp = max(tmp, max(bit1.quer(N - 1 - p, 0, N - 1, 1), bit2.quer(p, 0, N - 1, 1)) + T[p] - x);
        printf("%lld\n", tmp / 2);
    }
}


Compiler output:
a.cc: In function 'void solve(int, int, int, BIT&, ll*, ll*)':
a.cc:78:21: error: cannot convert 'BIT' to 'int'
   78 |     solve(m + 1, r, bit, dp1, dp2);
      |                     ^~~
      |                     |
      |                     BIT
a.cc:74:30: note:   initializing argument 3 of 'void solve(int, int, int, BIT&, ll*, ll*)'
   74 | void solve(int l, int r, int t, BIT &bit, ll *dp1, ll *dp2) {
      |                          ~~~~^
a.cc:86:21: error: cannot convert 'BIT' to 'int'
   86 |     solve(l, m - 1, bit, dp1, dp2);
      |                     ^~~
      |                     |
      |                     BIT
a.cc:74:30: note:   initializing argument 3 of 'void solve(int, int, int, BIT&, ll*, ll*)'
   74 | void solve(int l, int r, int t, BIT &bit, ll *dp1, ll *dp2) {
      |                          ~~~~^