이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//# include <x86intrin.h>
# include <bits/stdc++.h>
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
 
template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red
inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}
const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 1e9 + 11;
const int mxn = 1e6 + 9;
const int N = 3e5 + 123;                                          
const int M = 22;
const int pri = 997;
const int Magic = 2101;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
 
int n, m, k;
struct ana {
	int h, a, b;
} a[N];
int ans[N];
int ql[N], qr[N];
struct SegTree {
	int t[N * 4];
	int mx[N * 4];
	int add[N * 4];
	inline void clear() {
		for (int i = 1; i < N * 4; ++i) {
			t[i] = -1;
			add[i] = inf;
			mx[i] = 0;
		}
	}
	inline void push (int v, int tl, int tr) {
		if (add[v] == inf) return;
		if (tl != tr) {
			add[v << 1] = min(add[v<< 1], add[v]);
			add[v << 1 | 1] = min(add[v << 1 | 1], add[v]);
		}
		t[v] = max(t[v], mx[v] - add[v]);
		add[v] = inf;
	}
	inline void recalc (int v) {
		t[v] = max(t[v<<1], t[v << 1 | 1]);
		mx[v] = max(mx[v << 1], mx[v << 1 | 1]);
	}
	void upd (int p, int x, int v = 1, int tl = 1, int tr = n) {
		push(v, tl, tr);
		if (tl > p || p > tr) return;
		if (tl == tr) {
			mx[v] = x;
			return;
		}
		int tm = (tl + tr) >> 1;
		upd (p, x, v << 1, tl, tm);
		upd (p, x, v << 1 | 1, tm + 1, tr);
		recalc(v);
	} 
	void segDec (int l, int r, int x, int v = 1, int tl = 1, int tr = n) {
		push(v, tl,  tr);
		if (l > tr || tl > r) return;
		if (tl >= l && tr <= r) {
			add[v] = x;
			push(v, tl, tr);
			return;
		}
		int tm = (tl + tr) >> 1;
		segDec (l, r, x, v << 1, tl, tm);
		segDec (l, r, x, v << 1 | 1, tm + 1, tr);
		recalc(v);
	} 
	int get (int l, int r, int v = 1, int tl = 1, int tr = n) {
		push(v, tl, tr);
		if (tl > r || l > tr) return -1;
		if (tl >= l && tr <= r) return t[v];
		int tm = (tl + tr) >> 1;
		return max (
			get (l, r, v << 1, tl, tm)
				, get (l, r, v << 1 | 1, tm + 1, tr)
		);
	}
} T;
vector < int > st[N], en[N], q[N];
inline void solve () {
	T.clear();
	for (int i = 1; i <= n; i ++) {
		st[i].clear();
		en[i].clear();
		q[i].clear();
	}
	for (int i = 1; i <= m; i ++) {
		q[qr[i]].pb(i);
	}
	for (int i = 1; i <= n; i ++) {
		if (i + a[i].a <= n) {
			st[i + a[i].a].pb(i);
		}
		if (i + a[i].b <= n) {
			en[i + a[i].b].pb(i);
		}
		for (auto it : st[i])
			T.upd(it, a[it].h);
		if (i > a[i].a) {
			T.segDec(i - a[i].b, i - a[i].a, a[i].h);
		}
		for (auto j : q[i]) {
			ans[j] = max(ans[j], T.get(ql[j], qr[j]));
		}
		for (auto it : en[i])
			T.upd(it, 0);
	}
	
}
int main () {
   	SpeedForce;
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i].h >> a[i].a >> a[i].b;
	}
	cin >> m;
	for (int i = 1; i <= m; ++i) {
		ans[i] = -1;
		cin >> ql[i] >> qr[i];
	}
	solve();
	reverse(a + 1, a + n + 1);
	for (int i = 1; i <= m; i ++) {
		tie(ql[i], qr[i]) = mp(n - qr[i] + 1, n - ql[i] + 1);
	}
	solve();
	for (int i = 1; i <= m; i ++)
		cout << ans[i] << '\n';
   	return Accepted;
}
// B...a
| # | 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... |