Submission #560672

# Submission time Handle Problem Language Result Execution time Memory
560672 2022-05-11T20:09:39 Z aryan12 Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1416 ms 162112 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e6 + 5;
vector<int> a(N), val(N, -1);
vector<array<int, 2> > g[N];
int seg[N * 4];

void Update(int l, int r, int pos, int qpos, int qval)
{
	seg[pos] = max(seg[pos], qval);
	if(l == r)
	{
		return;
	}
	int mid = (l + r) >> 1;
	if(qpos <= mid) Update(l, mid, pos * 2, qpos, qval);
	else Update(mid + 1, r, pos * 2 + 1, qpos, qval);
}

int Query(int l, int r, int pos, int ql, int qr)
{
	if(ql <= l && r <= qr)
	{
		return seg[pos];
	}
	if(ql > r || l > qr)
	{
		return 0;
	}
	int mid = (l + r) >> 1;
	return max(Query(l, mid, pos * 2, ql, qr), Query(mid + 1, r, pos * 2 + 1, ql, qr));
}

struct query
{
	int l, r, idx, ans, value;
};

vector<query> queries(N);

bool cmp(query x, query y)
{
	if(x.r != y.r)
	{
		return x.r < y.r;
	}
	return x.l < y.l;
}

bool cmp2(query x, query y)
{
	return x.idx < y.idx; 
}

void Solve() 
{
	int n, q;
	cin >> n >> q;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for(int i = 1; i <= q; i++)
	{
		cin >> queries[i].l >> queries[i].r >> queries[i].value;
		queries[i].idx = i;
		queries[i].ans = 0;
	}
	stack<pair<int, int> > s;
	s.push({val[1], 1});
	for(int i = 2; i <= n; i++)
	{
		while(!s.empty() && s.top().first <= a[i])
		{
			s.pop();
		}
		if(!s.empty())
		{
			val[i] = s.top().second;
		}
		s.push({a[i], i});
	}
	for(int i = 1; i <= n; i++)
	{
		if(val[i] != -1)
		{
			// cout << i << " -> " << val[i] << ", with wt -> " << a[val[i]] + a[i] << "\n";
			g[i].push_back({val[i], a[val[i]] + a[i]});
		}
	}
	sort(queries.begin() + 1, queries.begin() + 1 + q, cmp);
	int pos = 1;
	for(int i = 1; i <= n; i++)
	{
		for(auto [to, wt]: g[i])
		{
			Update(1, n, 1, to, wt);
		}
		while(pos != q + 1 && queries[pos].r == i)
		{
			// cout << "pos = " << pos << ", idx = " << queries[pos].idx << "\n";
			queries[pos].ans = Query(1, n, 1, queries[pos].l, queries[pos].r);
			// cout << "queries[i].ans = " << queries[pos].ans << "\n";
			// cout << "queries[i].l = " << queries[pos].l << ", queries[i].r = " << queries[pos].r << "\n";
			pos++;
		}
	}
	sort(queries.begin() + 1, queries.begin() + 1 + q, cmp2);
	for(int i = 1; i <= q; i++)
	{
		// cout << "queries[i].ans = " << queries[i].ans << "\n";
		if(queries[i].ans <= queries[i].value)
		{
			cout << "1\n";
		}
		else
		{
			cout << "0\n";
		}
	}
}

int32_t main() 
{
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++) 
	{
		//cout << "Case #" << i << ": ";
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 32 ms 78536 KB Output is correct
2 Correct 36 ms 78576 KB Output is correct
3 Correct 34 ms 78620 KB Output is correct
4 Correct 42 ms 78560 KB Output is correct
5 Correct 34 ms 78616 KB Output is correct
6 Correct 35 ms 78548 KB Output is correct
7 Correct 33 ms 78516 KB Output is correct
8 Correct 36 ms 78580 KB Output is correct
9 Correct 33 ms 78572 KB Output is correct
10 Correct 35 ms 78600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 78536 KB Output is correct
2 Correct 36 ms 78576 KB Output is correct
3 Correct 34 ms 78620 KB Output is correct
4 Correct 42 ms 78560 KB Output is correct
5 Correct 34 ms 78616 KB Output is correct
6 Correct 35 ms 78548 KB Output is correct
7 Correct 33 ms 78516 KB Output is correct
8 Correct 36 ms 78580 KB Output is correct
9 Correct 33 ms 78572 KB Output is correct
10 Correct 35 ms 78600 KB Output is correct
11 Correct 36 ms 78620 KB Output is correct
12 Correct 36 ms 78804 KB Output is correct
13 Correct 42 ms 78868 KB Output is correct
14 Correct 37 ms 78868 KB Output is correct
15 Correct 39 ms 78892 KB Output is correct
16 Correct 36 ms 78676 KB Output is correct
17 Correct 41 ms 78600 KB Output is correct
18 Correct 36 ms 78676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1337 ms 159268 KB Output is correct
2 Correct 1352 ms 161108 KB Output is correct
3 Correct 1331 ms 161040 KB Output is correct
4 Correct 1341 ms 161188 KB Output is correct
5 Correct 1228 ms 113464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 83920 KB Output is correct
2 Correct 136 ms 83956 KB Output is correct
3 Correct 121 ms 78740 KB Output is correct
4 Correct 122 ms 78796 KB Output is correct
5 Correct 123 ms 78752 KB Output is correct
6 Correct 132 ms 79128 KB Output is correct
7 Correct 117 ms 79052 KB Output is correct
8 Correct 115 ms 81048 KB Output is correct
9 Correct 97 ms 78740 KB Output is correct
10 Correct 117 ms 80660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 78536 KB Output is correct
2 Correct 36 ms 78576 KB Output is correct
3 Correct 34 ms 78620 KB Output is correct
4 Correct 42 ms 78560 KB Output is correct
5 Correct 34 ms 78616 KB Output is correct
6 Correct 35 ms 78548 KB Output is correct
7 Correct 33 ms 78516 KB Output is correct
8 Correct 36 ms 78580 KB Output is correct
9 Correct 33 ms 78572 KB Output is correct
10 Correct 35 ms 78600 KB Output is correct
11 Correct 36 ms 78620 KB Output is correct
12 Correct 36 ms 78804 KB Output is correct
13 Correct 42 ms 78868 KB Output is correct
14 Correct 37 ms 78868 KB Output is correct
15 Correct 39 ms 78892 KB Output is correct
16 Correct 36 ms 78676 KB Output is correct
17 Correct 41 ms 78600 KB Output is correct
18 Correct 36 ms 78676 KB Output is correct
19 Correct 282 ms 94780 KB Output is correct
20 Correct 282 ms 96008 KB Output is correct
21 Correct 255 ms 95804 KB Output is correct
22 Correct 281 ms 95740 KB Output is correct
23 Correct 259 ms 95788 KB Output is correct
24 Correct 240 ms 85320 KB Output is correct
25 Correct 231 ms 85328 KB Output is correct
26 Correct 247 ms 85508 KB Output is correct
27 Correct 242 ms 85508 KB Output is correct
28 Correct 252 ms 85500 KB Output is correct
29 Correct 240 ms 85592 KB Output is correct
30 Correct 266 ms 85596 KB Output is correct
31 Correct 240 ms 85580 KB Output is correct
32 Correct 247 ms 85500 KB Output is correct
33 Correct 245 ms 85580 KB Output is correct
34 Correct 237 ms 85200 KB Output is correct
35 Correct 254 ms 85248 KB Output is correct
36 Correct 235 ms 85052 KB Output is correct
37 Correct 233 ms 85040 KB Output is correct
38 Correct 248 ms 85252 KB Output is correct
39 Correct 236 ms 87648 KB Output is correct
40 Correct 186 ms 84480 KB Output is correct
41 Correct 223 ms 87304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 78536 KB Output is correct
2 Correct 36 ms 78576 KB Output is correct
3 Correct 34 ms 78620 KB Output is correct
4 Correct 42 ms 78560 KB Output is correct
5 Correct 34 ms 78616 KB Output is correct
6 Correct 35 ms 78548 KB Output is correct
7 Correct 33 ms 78516 KB Output is correct
8 Correct 36 ms 78580 KB Output is correct
9 Correct 33 ms 78572 KB Output is correct
10 Correct 35 ms 78600 KB Output is correct
11 Correct 36 ms 78620 KB Output is correct
12 Correct 36 ms 78804 KB Output is correct
13 Correct 42 ms 78868 KB Output is correct
14 Correct 37 ms 78868 KB Output is correct
15 Correct 39 ms 78892 KB Output is correct
16 Correct 36 ms 78676 KB Output is correct
17 Correct 41 ms 78600 KB Output is correct
18 Correct 36 ms 78676 KB Output is correct
19 Correct 1337 ms 159268 KB Output is correct
20 Correct 1352 ms 161108 KB Output is correct
21 Correct 1331 ms 161040 KB Output is correct
22 Correct 1341 ms 161188 KB Output is correct
23 Correct 1228 ms 113464 KB Output is correct
24 Correct 138 ms 83920 KB Output is correct
25 Correct 136 ms 83956 KB Output is correct
26 Correct 121 ms 78740 KB Output is correct
27 Correct 122 ms 78796 KB Output is correct
28 Correct 123 ms 78752 KB Output is correct
29 Correct 132 ms 79128 KB Output is correct
30 Correct 117 ms 79052 KB Output is correct
31 Correct 115 ms 81048 KB Output is correct
32 Correct 97 ms 78740 KB Output is correct
33 Correct 117 ms 80660 KB Output is correct
34 Correct 282 ms 94780 KB Output is correct
35 Correct 282 ms 96008 KB Output is correct
36 Correct 255 ms 95804 KB Output is correct
37 Correct 281 ms 95740 KB Output is correct
38 Correct 259 ms 95788 KB Output is correct
39 Correct 240 ms 85320 KB Output is correct
40 Correct 231 ms 85328 KB Output is correct
41 Correct 247 ms 85508 KB Output is correct
42 Correct 242 ms 85508 KB Output is correct
43 Correct 252 ms 85500 KB Output is correct
44 Correct 240 ms 85592 KB Output is correct
45 Correct 266 ms 85596 KB Output is correct
46 Correct 240 ms 85580 KB Output is correct
47 Correct 247 ms 85500 KB Output is correct
48 Correct 245 ms 85580 KB Output is correct
49 Correct 237 ms 85200 KB Output is correct
50 Correct 254 ms 85248 KB Output is correct
51 Correct 235 ms 85052 KB Output is correct
52 Correct 233 ms 85040 KB Output is correct
53 Correct 248 ms 85252 KB Output is correct
54 Correct 236 ms 87648 KB Output is correct
55 Correct 186 ms 84480 KB Output is correct
56 Correct 223 ms 87304 KB Output is correct
57 Correct 1416 ms 162112 KB Output is correct
58 Correct 1370 ms 161808 KB Output is correct
59 Correct 1331 ms 161688 KB Output is correct
60 Correct 1384 ms 161740 KB Output is correct
61 Correct 1365 ms 161780 KB Output is correct
62 Correct 1333 ms 161668 KB Output is correct
63 Correct 1127 ms 112128 KB Output is correct
64 Correct 1151 ms 112124 KB Output is correct
65 Correct 1191 ms 114096 KB Output is correct
66 Correct 1192 ms 113972 KB Output is correct
67 Correct 1204 ms 114000 KB Output is correct
68 Correct 1159 ms 114092 KB Output is correct
69 Correct 1198 ms 114084 KB Output is correct
70 Correct 1168 ms 114164 KB Output is correct
71 Correct 1168 ms 114080 KB Output is correct
72 Correct 1181 ms 114204 KB Output is correct
73 Correct 1110 ms 110656 KB Output is correct
74 Correct 1154 ms 111696 KB Output is correct
75 Correct 1114 ms 110720 KB Output is correct
76 Correct 1149 ms 110720 KB Output is correct
77 Correct 1154 ms 110656 KB Output is correct
78 Correct 1140 ms 123888 KB Output is correct
79 Correct 884 ms 107384 KB Output is correct
80 Correct 1052 ms 127588 KB Output is correct