This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define X       first
#define Y       second
#define lch     i << 1
#define rch     i << 1 | 1
const int   N   = 4e5 + 1;
const int   inf = 1e9 + 7;
typedef pair<int,int>   ii;
ii  t1[N << 1];
ii  t2[N << 1];
int t3[N << 1];
ii  lz[N << 1];
ii  operator + (const ii &a,const ii &b)    {
    return  ii(max(a.X,b.X),max(a.Y,b.Y));
}
int pus(int i,int l,int r)  {
    t2[i] = t2[i] + lz[i];
    if (l < r)
        lz[lch] = lz[lch] + lz[i],
        lz[rch] = lz[rch] + lz[i];
    t3[i] = max(t3[i],t1[i].X + lz[i].Y);
    t3[i] = max(t3[i],t1[i].Y + lz[i].X);
    t3[i] = max(t3[i],-1);
    lz[i] = ii(-inf,-inf);
}
int upd(int i,int l,int r,int L,int R,int a,int b)  {
    pus(i,l,r);
    if (l > R)  return  0;
    if (L > r)  return  0;
    if (L <= l && r <= R)   {
        if (a == 0) lz[i] = ii(-b,b);
        if (b == 0) t1[i] = ii(a < 0 ? a : -a,a);
        pus(i,l,r);
        return  1;
    }
    int m = (l + r) / 2;
    upd(lch,l,m,L,R,a,b);   ++m;
    upd(rch,m,r,L,R,a,b);
    t1[i] = t1[lch] + t1[rch];
    t2[i] = t2[lch] + t2[rch];
    t3[i] = max(t3[lch],t3[rch]);
}
int get(int i,int l,int r,int L,int R)  {
    pus(i,l,r);
    if (l > R)  return  -1;
    if (L > r)  return  -1;
    if (L <= l && r <= R)
        return  t3[i];
    int m = (l + r) / 2;
    return  max(get(lch,l,m,L,R),get(rch,m + 1,r,L,R));
}
vector<int> add[N];
vector<int> rem[N];
vector<ii>  Q[N];
int l[N], r[N];
int h[N];
int ans[N];
int 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 >> h[i];
        int a;  cin >> a;
        int b;  cin >> b;   ++b;
        add[i + a].push_back(i);
        rem[i + b].push_back(i);
        l[i] = max(i - b,0) + 1;
        r[i] = max(i - a,0);
    }
    int m;  cin >> m;
    for(int i = 1 ; i <= m ; ++i)   {
        int L;  cin >> L;
        int R;  cin >> R;
        Q[R].push_back({L,i});
    }
    fill(t1 + 1,t1 + 4 * n,ii(-inf,-inf));
    fill(t2 + 1,t2 + 4 * n,ii(-inf,-inf));
    fill(t3 + 1,t3 + 4 * n,-1);
    fill(lz + 1,lz + 4 * n,ii(-inf,-inf));
    for(int i = 1 ; i <= n ; ++i)   {
        for(int x : add[i]) upd(1,1,n,x,x,h[x],0);
        for(int x : rem[i]) upd(1,1,n,x,x,-inf,0);
        upd(1,1,n,l[i],r[i],0,h[i]);
        for(ii  q : Q[i])
            ans[q.Y] = get(1,1,n,q.X,i);
    }
    for(int i = 1 ; i <= m ; ++i)
        cout << ans[i] << "\n";
}
Compilation message (stderr)
antennas.cpp: In function 'int pus(int, int, int)':
antennas.cpp:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
antennas.cpp: In function 'int upd(int, int, int, int, int, int, int)':
antennas.cpp:50:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |