Submission #1145418

#TimeUsernameProblemLanguageResultExecution timeMemory
1145418Trisanu_DasTwo Antennas (JOI19_antennas)C++20
100 / 100
490 ms59472 KiB
#include <bits/stdc++.h>
using namespace std;

const int Z = 2e5, INF = 1e9+1;

int sL, sR, sV;
struct segtree{
	int l, r, x = INF, y = -INF, res = -INF;
	segtree *L, *R;
	void pull() {
		x = min(L->x, R->x);
		res = max({L->res, R->res, y - x});
	}
	void apply(int val) {
		y = max(y, val);
		res = max(res, y - x);
	}
	void push() {
		L->apply(y), R->apply(y);
		y = -INF;
	}
	segtree(int lx, int rx) : l(lx), r(rx) {
		if(r - l < 2) return;
		int m = (l + r) / 2;
		L = new segtree(l, m);
		R = new segtree(m, r);
	}
	void update0() {
		if(sL<=l && r<=sR) return apply(sV);
		if(sR<=l || r<=sL) return;
		push();
		L->update0(), R->update0();
		pull();
	}
	void update1() {
		if(r - l < 2)
			y = -INF, x = sV, res = max(res, y - x);
		else
			push(), (sL < L->r ? L : R)->update1(), pull();
	}
	int query() {
		if(sL<=l && r<=sR) return res;
		if(sR<=l || r<=sL) return -INF;
		push();
		return max(L->query(), R->query());
	}
};

int N, Q, H[Z], A[Z], B[Z], qL[Z], ans[Z];
vector<int> add[Z+1], qAt[Z];

signed main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> N;
	for(int i = 0; i < N; ++i) {
		cin >> H[i] >> A[i] >> B[i];
		if(i + A[i] <  N) {
			add[i + A[i]].push_back(i);
			add[min(N, i + B[i] + 1)].push_back(-(i + 1));
		}
	}
	cin >> Q;
	for(int i = 0, qR; i < Q; ++i) {
		cin >> qL[i] >> qR;
		--qL[i];
		qAt[--qR].push_back(i);
		ans[i] = -1;
	}
	for(int _ : {1, 0}) {
		segtree st(0, N);
		for(int i = 0; i < N; ++i) {
			for(int &j : add[i])
				sL = max(j, -j-1), sV = (j < 0 ? INF : H[j]), st.update1();
			if(i - A[i] >= 0) 
				sL = max(0, i-B[i]), sR = i-A[i]+1, sV = H[i], st.update0();
			for(int &j : qAt[i])
				sL = qL[j], sR = N, ans[j] = max(ans[j], st.query());
		}
		if(_) for(int &i : H) i = -i;
	}
	for(int i = 0; i < Q; ++i) cout << ans[i] << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...