Submission #132140

#TimeUsernameProblemLanguageResultExecution timeMemory
132140KewoTwo Antennas (JOI19_antennas)C++17
2 / 100
3033 ms111632 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define mid ((x + y) / 2)
#define mid2 ((xx + yy) / 2)
#define left (ind * 2)
#define right (ind * 2 + 1)
#define spc " "
#define endl "\n"
#define fast_io() cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
using namespace std;

typedef pair<int, int> ii;
typedef long long int lli;

const int N = (2e3 + 5);
const int ST = (1e7 + 5);
const int LOG = (20);

int n, q, h[N], l[N], r[N], ar[N][N], tree[ST];

void build(int ind, int x, int y, int xx, int yy) {
	// cerr << x << spc << y << spc << xx << spc << yy << endl;
	if(x == y && xx == yy) {
		tree[ind] = ar[x][xx];
		return;
	}
	if(x == y) {
		build(ind * 4 - 2, x, x, xx, mid2);
		build(ind * 4, x, x, mid2 + 1, yy);
	}
	else if(xx == yy) {
		build(ind * 4 - 2, x, mid, xx, xx);
		build(ind * 4 - 1, mid + 1, y, xx, xx);
	}
	else {
		build(ind * 4 - 2, x, mid, xx, mid2);
		build(ind * 4 - 1, mid + 1, y, xx, mid2);
		build(ind * 4, x, mid, mid2 + 1, yy);
		build(ind * 4 + 1, mid + 1, y, mid2 + 1, yy);
	}
	tree[ind] = max(max(tree[ind * 4 - 2], tree[ind * 4 - 1]), max(tree[ind * 4], tree[ind * 4 + 1]));
}

int get(int ind, int x, int y, int xx, int yy, int a, int b, int c, int d) {
	if(y < a || b < x)
		return -1;
	if(yy < c || d < xx)
		return -1;
	if(a <= x && y <= b && c <= xx && yy <= d)
		return tree[ind];
	int re = -1;
	if(x == y) {
		re = max(re, get(ind * 4 - 2, x, x, xx, mid2, a, b, c, d));
		re = max(re, get(ind * 4, x, x, mid2 + 1, yy, a, b, c, d));
	}
	else if(xx == yy) {
		re = max(re, get(ind * 4 - 2, x, mid, xx, xx, a, b, c, d));
		re = max(re, get(ind * 4 - 1, mid + 1, y, xx, xx, a, b, c, d));
	}
	else {
		re = max(re, get(ind * 4 - 2, x, mid, xx, mid2, a, b, c, d));
		re = max(re, get(ind * 4 - 1, mid + 1, y, xx, mid2, a, b, c, d));
		re = max(re, get(ind * 4, x, mid, mid2 + 1, yy, a, b, c, d));
		re = max(re, get(ind * 4 + 1, mid + 1, y, mid2 + 1, yy, a, b, c, d));
	}
	return re;
}

int f(int x, int y) {
	return get(1, 1, n, 1, n, x, y, x, y);
}

int main() {
	fast_io();
	// freopen("inp.in", "r", stdin);
	
	memset(ar, -1, sizeof ar);
	memset(tree, -1, sizeof tree);
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> h[i] >> l[i] >> r[i];
	cin >> q;
	for(int i = 1; i <= n; i++)
		for(int j = i + 1; j <= n; j++)
			if(i + l[i] <= j && j <= i + r[i] && j - r[j] <= i && i <= j - l[j]) {
				ar[i][j] = abs(h[i] - h[j]);
				ar[j][i] = abs(h[i] - h[j]);
			}
	build(1, 1, n, 1, n);
	while(q--) {
		int a, b;
		cin >> a >> b;
		cout << f(a, b) << endl;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...