Submission #684222

# Submission time Handle Problem Language Result Execution time Memory
684222 2023-01-20T17:52:38 Z gagik_2007 Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
2368 ms 142900 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;

typedef long long ll;
typedef long double ld;

#define ff first
#define ss second

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 1000007;
ll n, m, k;
ll a[N];
ll lst[N];
ll val[N];
vector<int>d[N];
ll t[4 * N];
vector<pair<pair<int, int>, pair<ll, int>>>q;

void update(int v, int tl, int tr, int ind, ll vl) {
	if (tl == tr) {
		t[v] = vl;
	}
	else {
		int tm = (tl + tr) / 2;
		if (ind <= tm) {
			update(2 * v, tl, tm, ind, vl);
		}
		else update(2 * v + 1, tm + 1, tr, ind, vl);
		t[v] = max(t[2 * v], t[2 * v + 1]);
	}
}

ll get_max(int v, int tl, int tr, int l, int r) {
	//cout << v << " " << tl << " " << tr << " " << l << " " << r << endl;
	if (l > r)return 0;
	if (tl == l && tr == r)return t[v];
	else {
		int tm = (tl + tr) / 2;
		return max(get_max(2 * v, tl, tm, l, min(r, tm)),
			get_max(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
	}
}

void calc_lst() {
	stack<pair<ll, int>>st;
	for (int i = n - 1; i >= 0; i--) {
		pair<ll, int>pr = make_pair(a[i], 0);
		while (!st.empty() && st.top() < pr) {
			lst[st.top().ss] = i;
			st.pop();
		}
		st.push({ a[i],i });
	}
	while (!st.empty()) {
		lst[st.top().ss] = -1;
		st.pop();
	}
}

void calc_val() {
	//cout << "lst: ";
	for (int i = 0; i < n; i++) {
		//cout << lst[i] << " ";
		if (lst[i] == -1)val[i] = 0;
		else val[i] = a[i] + a[lst[i]];
	}
	//cout << endl;
}

void calc_d() {
	for (int i = 0; i < n; i++) {
		if (lst[i] != -1) {
			d[lst[i]].push_back(i);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	cin >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	calc_lst();
	calc_val();
	calc_d();
	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> l >> r >> k;
		l--, r--;
		q.push_back({ {l,r},{k,i} });
	}
	sort(q.begin(), q.end());
	reverse(q.begin(), q.end());
	vector<int>ans(m);
	int qi = 0;
	for (int i = n - 1; i >= 0; i--) {
		//cout << "FINDING NUMBERS FOR WHICH lst[i] == " << i << endl;
		for (auto j : d[i]) {
			//cout << "UPDATING: " << j << endl;
			update(1, 0, n - 1, j, val[j]);
		}
		while (qi < m && q[qi].ff.ff == i) {
			//cout << "ANSWERING: " << q[qi].ss.ss << endl;
			ll res = get_max(1, 0, n - 1, q[qi].ff.ff, q[qi].ff.ss);
			ans[q[qi].ss.ss] = (q[qi].ss.ff >= res);
			qi++;
		}
	}
	for (auto x : ans) {
		cout << x << endl;
	}
	return 0;
}

/// ---- - --------  ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - --------  ------ -------- -- - - -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23748 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 15 ms 23912 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 14 ms 23824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23748 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 15 ms 23912 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 14 ms 23824 KB Output is correct
11 Correct 18 ms 24148 KB Output is correct
12 Correct 18 ms 24328 KB Output is correct
13 Correct 19 ms 24432 KB Output is correct
14 Correct 22 ms 24528 KB Output is correct
15 Correct 25 ms 24472 KB Output is correct
16 Correct 22 ms 24364 KB Output is correct
17 Correct 27 ms 24540 KB Output is correct
18 Correct 22 ms 24428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2302 ms 141180 KB Output is correct
2 Correct 2294 ms 124520 KB Output is correct
3 Correct 2274 ms 142248 KB Output is correct
4 Correct 2272 ms 142212 KB Output is correct
5 Correct 2011 ms 121784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 262 ms 34684 KB Output is correct
2 Correct 212 ms 34884 KB Output is correct
3 Correct 198 ms 32544 KB Output is correct
4 Correct 192 ms 32488 KB Output is correct
5 Correct 199 ms 32648 KB Output is correct
6 Correct 185 ms 32264 KB Output is correct
7 Correct 200 ms 32204 KB Output is correct
8 Correct 189 ms 32972 KB Output is correct
9 Correct 156 ms 28056 KB Output is correct
10 Correct 186 ms 32432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23748 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 15 ms 23912 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 14 ms 23824 KB Output is correct
11 Correct 18 ms 24148 KB Output is correct
12 Correct 18 ms 24328 KB Output is correct
13 Correct 19 ms 24432 KB Output is correct
14 Correct 22 ms 24528 KB Output is correct
15 Correct 25 ms 24472 KB Output is correct
16 Correct 22 ms 24364 KB Output is correct
17 Correct 27 ms 24540 KB Output is correct
18 Correct 22 ms 24428 KB Output is correct
19 Correct 458 ms 48228 KB Output is correct
20 Correct 511 ms 48300 KB Output is correct
21 Correct 423 ms 48120 KB Output is correct
22 Correct 428 ms 48108 KB Output is correct
23 Correct 420 ms 48096 KB Output is correct
24 Correct 386 ms 43220 KB Output is correct
25 Correct 448 ms 43244 KB Output is correct
26 Correct 387 ms 43424 KB Output is correct
27 Correct 397 ms 43480 KB Output is correct
28 Correct 411 ms 43468 KB Output is correct
29 Correct 408 ms 43460 KB Output is correct
30 Correct 407 ms 43472 KB Output is correct
31 Correct 399 ms 43436 KB Output is correct
32 Correct 405 ms 43520 KB Output is correct
33 Correct 402 ms 43508 KB Output is correct
34 Correct 391 ms 43232 KB Output is correct
35 Correct 389 ms 43140 KB Output is correct
36 Correct 388 ms 43004 KB Output is correct
37 Correct 381 ms 43108 KB Output is correct
38 Correct 414 ms 43176 KB Output is correct
39 Correct 382 ms 42380 KB Output is correct
40 Correct 369 ms 38588 KB Output is correct
41 Correct 387 ms 43040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 13 ms 23892 KB Output is correct
4 Correct 14 ms 23748 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 15 ms 23912 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 14 ms 23824 KB Output is correct
11 Correct 18 ms 24148 KB Output is correct
12 Correct 18 ms 24328 KB Output is correct
13 Correct 19 ms 24432 KB Output is correct
14 Correct 22 ms 24528 KB Output is correct
15 Correct 25 ms 24472 KB Output is correct
16 Correct 22 ms 24364 KB Output is correct
17 Correct 27 ms 24540 KB Output is correct
18 Correct 22 ms 24428 KB Output is correct
19 Correct 2302 ms 141180 KB Output is correct
20 Correct 2294 ms 124520 KB Output is correct
21 Correct 2274 ms 142248 KB Output is correct
22 Correct 2272 ms 142212 KB Output is correct
23 Correct 2011 ms 121784 KB Output is correct
24 Correct 262 ms 34684 KB Output is correct
25 Correct 212 ms 34884 KB Output is correct
26 Correct 198 ms 32544 KB Output is correct
27 Correct 192 ms 32488 KB Output is correct
28 Correct 199 ms 32648 KB Output is correct
29 Correct 185 ms 32264 KB Output is correct
30 Correct 200 ms 32204 KB Output is correct
31 Correct 189 ms 32972 KB Output is correct
32 Correct 156 ms 28056 KB Output is correct
33 Correct 186 ms 32432 KB Output is correct
34 Correct 458 ms 48228 KB Output is correct
35 Correct 511 ms 48300 KB Output is correct
36 Correct 423 ms 48120 KB Output is correct
37 Correct 428 ms 48108 KB Output is correct
38 Correct 420 ms 48096 KB Output is correct
39 Correct 386 ms 43220 KB Output is correct
40 Correct 448 ms 43244 KB Output is correct
41 Correct 387 ms 43424 KB Output is correct
42 Correct 397 ms 43480 KB Output is correct
43 Correct 411 ms 43468 KB Output is correct
44 Correct 408 ms 43460 KB Output is correct
45 Correct 407 ms 43472 KB Output is correct
46 Correct 399 ms 43436 KB Output is correct
47 Correct 405 ms 43520 KB Output is correct
48 Correct 402 ms 43508 KB Output is correct
49 Correct 391 ms 43232 KB Output is correct
50 Correct 389 ms 43140 KB Output is correct
51 Correct 388 ms 43004 KB Output is correct
52 Correct 381 ms 43108 KB Output is correct
53 Correct 414 ms 43176 KB Output is correct
54 Correct 382 ms 42380 KB Output is correct
55 Correct 369 ms 38588 KB Output is correct
56 Correct 387 ms 43040 KB Output is correct
57 Correct 2368 ms 142832 KB Output is correct
58 Correct 2292 ms 142900 KB Output is correct
59 Correct 2320 ms 142788 KB Output is correct
60 Correct 2323 ms 142696 KB Output is correct
61 Correct 2184 ms 142828 KB Output is correct
62 Correct 2245 ms 142728 KB Output is correct
63 Correct 1964 ms 120620 KB Output is correct
64 Correct 1921 ms 120568 KB Output is correct
65 Correct 2021 ms 122380 KB Output is correct
66 Correct 2027 ms 122296 KB Output is correct
67 Correct 2040 ms 122416 KB Output is correct
68 Correct 2019 ms 122456 KB Output is correct
69 Correct 2041 ms 122684 KB Output is correct
70 Correct 1933 ms 122660 KB Output is correct
71 Correct 1937 ms 122520 KB Output is correct
72 Correct 2015 ms 122520 KB Output is correct
73 Correct 1909 ms 119108 KB Output is correct
74 Correct 1943 ms 120060 KB Output is correct
75 Correct 1892 ms 118960 KB Output is correct
76 Correct 1950 ms 119092 KB Output is correct
77 Correct 1935 ms 119364 KB Output is correct
78 Correct 1927 ms 116508 KB Output is correct
79 Correct 1702 ms 92624 KB Output is correct
80 Correct 1914 ms 117844 KB Output is correct