Submission #489624

# Submission time Handle Problem Language Result Execution time Memory
489624 2021-11-23T12:38:57 Z 8e7 Triple Jump (JOI19_jumps) C++17
100 / 100
778 ms 29460 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
using namespace std;
void debug(){cout << endl;}
template<class T, class ...U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary (T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#define ll long long
#define maxn 500005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
int a[maxn];
int ans[maxn];
struct query{
	int l, r, id;
	query(){l = r = id = 0;}
	query(int x, int y, int z){l = x, r = y, id = z;}
} que[maxn];
struct segtree{
	int ma[4*maxn], tag[4*maxn], my[4*maxn];
	void init(int cur, int l, int r) {
		if (r <= l) return;
		if (r - l == 1) {
			ma[cur] = a[l];
			my[cur] = a[l];
			return;
		}
		int m = (l + r) / 2;
		init(cur*2, l, m), init(cur*2+1, m, r);
		ma[cur] = max(ma[cur*2], ma[cur*2+1]);
		my[cur] = max(my[cur*2], my[cur*2+1]);
	}
	void push(int cur, int l, int r) {
		if (tag[cur] == 0) return;
		ma[cur] = max(ma[cur], my[cur] + tag[cur]);
		if (r - l > 1) {
			tag[cur*2] = max(tag[cur*2], tag[cur]);
			tag[cur*2+1] = max(tag[cur*2+1], tag[cur]);
		}
		tag[cur] = 0;
	}
	void pull(int cur, int l, int r) {
		if (r - l > 1) {
			int m = (l + r) / 2;
			push(cur*2, l, m), push(cur*2+1, m, r);
			ma[cur] = max(ma[cur*2], ma[cur*2+1]);
		}
	}
	void modify(int cur, int l, int r, int ql, int qr, int x) {
		if (r <= l || ql >= r || qr <= l) return;
		push(cur, l, r);
		if (ql <= l && qr >= r) {
			tag[cur] = max(tag[cur], x);
			push(cur, l, r);
			return;
		}
		int m = (l + r) / 2;
		modify(cur*2, l, m, ql, qr, x), modify(cur*2+1, m, r, ql, qr, x);
		pull(cur, l, r);
	}
	int query(int cur,int l, int r, int ql, int qr) {
		if (r <= l || ql >= r || qr <= l) return 0;
		push(cur, l, r);
		if (ql <= l && qr >= r) {
			pull(cur, l, r);	
			return ma[cur];
		}
		int m = (l + r) / 2;
		return max(query(cur*2, l, m, ql, qr), query(cur*2+1, m, r, ql, qr));
	}
} seg;
int main() {
	io
	int n, q;
	cin >> n;
	for (int i = 1;i <= n;i++) cin >> a[i];
	cin >> q;
	for (int i = 0;i < q;i++) {
		cin >> que[i].l >> que[i].r;
		que[i].id = i;
	}
	sort(que, que + q, [&](query x, query y){return x.l > y.l;});
	stack<int> stk;	
	int ind = 0;
	seg.init(1, 1, n+1);
	for (int i = n;i >= 1;i--) {
		while (stk.size() && a[i] >= a[stk.top()]) {
			int val = a[i] + a[stk.top()];
			seg.modify(1, 1, n+1, stk.top()*2-i, n+1, val);	
			stk.pop();
		}
		if (stk.size()) {
			int val = a[i] + a[stk.top()];
			seg.modify(1, 1, n+1, stk.top()*2-i, n+1, val);	
		}
		stk.push(i);
		while (ind < q && que[ind].l == i) {
			ans[que[ind].id] = seg.query(1, 1, n+1, que[ind].l, que[ind].r+1);
			ind++;
		}
	}
	for (int i = 0;i < q;i++) cout << ans[i] << "\n";
}
/*
5
5 2 1 5 3
1
1 5


5
5 4 4 5 4
1
1 5

15
12 96 100 61 54 66 37 34 58 21 21 1 13 50 81
12
1 15
3 12
11 14
1 13
5 9
4 6
6 14
2 5
4 15
1 7
1 10
8 13
*/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6220 KB Output is correct
2 Correct 3 ms 6220 KB Output is correct
3 Correct 3 ms 6220 KB Output is correct
4 Correct 4 ms 6220 KB Output is correct
5 Correct 4 ms 6220 KB Output is correct
6 Correct 3 ms 6220 KB Output is correct
7 Correct 4 ms 6200 KB Output is correct
8 Correct 3 ms 6220 KB Output is correct
9 Correct 3 ms 6220 KB Output is correct
10 Correct 3 ms 6196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6220 KB Output is correct
2 Correct 3 ms 6220 KB Output is correct
3 Correct 3 ms 6220 KB Output is correct
4 Correct 4 ms 6220 KB Output is correct
5 Correct 4 ms 6220 KB Output is correct
6 Correct 3 ms 6220 KB Output is correct
7 Correct 4 ms 6200 KB Output is correct
8 Correct 3 ms 6220 KB Output is correct
9 Correct 3 ms 6220 KB Output is correct
10 Correct 3 ms 6196 KB Output is correct
11 Correct 258 ms 13636 KB Output is correct
12 Correct 250 ms 13508 KB Output is correct
13 Correct 264 ms 13608 KB Output is correct
14 Correct 256 ms 13616 KB Output is correct
15 Correct 259 ms 13612 KB Output is correct
16 Correct 256 ms 12848 KB Output is correct
17 Correct 265 ms 12848 KB Output is correct
18 Correct 251 ms 12880 KB Output is correct
19 Correct 258 ms 13484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 13296 KB Output is correct
2 Correct 64 ms 14100 KB Output is correct
3 Correct 67 ms 13232 KB Output is correct
4 Correct 109 ms 13296 KB Output is correct
5 Correct 107 ms 13336 KB Output is correct
6 Correct 104 ms 13268 KB Output is correct
7 Correct 104 ms 13168 KB Output is correct
8 Correct 103 ms 13140 KB Output is correct
9 Correct 105 ms 13196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6220 KB Output is correct
2 Correct 3 ms 6220 KB Output is correct
3 Correct 3 ms 6220 KB Output is correct
4 Correct 4 ms 6220 KB Output is correct
5 Correct 4 ms 6220 KB Output is correct
6 Correct 3 ms 6220 KB Output is correct
7 Correct 4 ms 6200 KB Output is correct
8 Correct 3 ms 6220 KB Output is correct
9 Correct 3 ms 6220 KB Output is correct
10 Correct 3 ms 6196 KB Output is correct
11 Correct 258 ms 13636 KB Output is correct
12 Correct 250 ms 13508 KB Output is correct
13 Correct 264 ms 13608 KB Output is correct
14 Correct 256 ms 13616 KB Output is correct
15 Correct 259 ms 13612 KB Output is correct
16 Correct 256 ms 12848 KB Output is correct
17 Correct 265 ms 12848 KB Output is correct
18 Correct 251 ms 12880 KB Output is correct
19 Correct 258 ms 13484 KB Output is correct
20 Correct 107 ms 13296 KB Output is correct
21 Correct 64 ms 14100 KB Output is correct
22 Correct 67 ms 13232 KB Output is correct
23 Correct 109 ms 13296 KB Output is correct
24 Correct 107 ms 13336 KB Output is correct
25 Correct 104 ms 13268 KB Output is correct
26 Correct 104 ms 13168 KB Output is correct
27 Correct 103 ms 13140 KB Output is correct
28 Correct 105 ms 13196 KB Output is correct
29 Correct 778 ms 27712 KB Output is correct
30 Correct 627 ms 29460 KB Output is correct
31 Correct 615 ms 27444 KB Output is correct
32 Correct 731 ms 27504 KB Output is correct
33 Correct 744 ms 27472 KB Output is correct
34 Correct 742 ms 26812 KB Output is correct
35 Correct 720 ms 26820 KB Output is correct
36 Correct 734 ms 26812 KB Output is correct
37 Correct 736 ms 27368 KB Output is correct
38 Correct 570 ms 27588 KB Output is correct
39 Correct 583 ms 27484 KB Output is correct
40 Correct 577 ms 25916 KB Output is correct
41 Correct 575 ms 25592 KB Output is correct
42 Correct 568 ms 25772 KB Output is correct
43 Correct 565 ms 26436 KB Output is correct
44 Correct 612 ms 27496 KB Output is correct
45 Correct 609 ms 27504 KB Output is correct
46 Correct 596 ms 26056 KB Output is correct
47 Correct 591 ms 25944 KB Output is correct
48 Correct 587 ms 25812 KB Output is correct
49 Correct 632 ms 26940 KB Output is correct
50 Correct 638 ms 27340 KB Output is correct
51 Correct 656 ms 27332 KB Output is correct
52 Correct 657 ms 26600 KB Output is correct
53 Correct 636 ms 26448 KB Output is correct
54 Correct 657 ms 26468 KB Output is correct
55 Correct 650 ms 27460 KB Output is correct