#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define dbg(x) cerr << #x << " = " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vvi = vt< vt<int> >;
const int N = 1e6 + 5, mod = 1e9 + 7, inf = 1e18 + 7, B = 500, LIM = (1ll << 60);
const double eps = 1e-6;
struct Event {
int l, r, k;
};
vt<Event> g[N];
int n, m, t[4 * N], w[N], ans[N], L[N];
stack<int> s;
void upd (int pos, int val, int x = 1, int tl = 1, int tr = n) {
if (tl == tr) {
t[x] = max(t[x], val);
return;
}
int tm = tl + tr >> 1, v = x + 1, u = x + ((tm - tl + 1) << 1);
if (pos <= tm) upd(pos, val, v, tl, tm);
else upd(pos, val, u, tm + 1, tr);
t[x] = max(t[v], t[u]);
}
int get (int l, int r, int x = 1, int tl = 1, int tr = n) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[x];
int tm = tl + tr >> 1, v = x + 1, u = x + ((tm - tl + 1) << 1);
int a = get(l, r, v, tl, tm);
int b = get(l, r, u, tm + 1, tr);
return max(a, b);
}
void solve () {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
for (int q = 1; q <= m; q++) {
int l, r, k;
cin >> l >> r >> k;
g[r].pb({l, k, q});
}
for (int i = 1; i <= n; i++) {
L[i] = i;
while (!s.empty() && w[s.top()] <= w[i]) {
L[i] = L[s.top()];
s.pop();
}
s.push(i);
}
for (int i = 1; i <= n; i++) {
int j = L[i] - 1;
if (j) upd(j, w[i] + w[j]);
for (auto [l, k, id]: g[i]) ans[id] = (get(l, n) <= k);
}
for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
cout << '\n';
}
bool testcases = 0;
signed main() {
#ifdef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
cin.tie(0) -> sync_with_stdio(0);
int test = 1;
if (testcases) cin >> test;
for (int cs = 1; cs <= test; cs++) {
solve();
}
}
Compilation message
sortbooks.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
sortbooks.cpp:36:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int tm = tl + tr >> 1, v = x + 1, u = x + ((tm - tl + 1) << 1);
| ~~~^~~~
sortbooks.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
sortbooks.cpp:45:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int tm = tl + tr >> 1, v = x + 1, u = x + ((tm - tl + 1) << 1);
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23884 KB |
Output is correct |
6 |
Correct |
14 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
23884 KB |
Output is correct |
8 |
Correct |
14 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23884 KB |
Output is correct |
6 |
Correct |
14 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
23884 KB |
Output is correct |
8 |
Correct |
14 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
15 ms |
24084 KB |
Output is correct |
12 |
Correct |
16 ms |
24124 KB |
Output is correct |
13 |
Correct |
18 ms |
24200 KB |
Output is correct |
14 |
Correct |
16 ms |
24356 KB |
Output is correct |
15 |
Correct |
17 ms |
24332 KB |
Output is correct |
16 |
Correct |
16 ms |
24192 KB |
Output is correct |
17 |
Correct |
15 ms |
24140 KB |
Output is correct |
18 |
Correct |
16 ms |
24292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1065 ms |
97516 KB |
Output is correct |
2 |
Correct |
1097 ms |
97552 KB |
Output is correct |
3 |
Correct |
1108 ms |
97420 KB |
Output is correct |
4 |
Correct |
1097 ms |
97532 KB |
Output is correct |
5 |
Correct |
885 ms |
81856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
31220 KB |
Output is correct |
2 |
Correct |
87 ms |
33216 KB |
Output is correct |
3 |
Correct |
82 ms |
31628 KB |
Output is correct |
4 |
Correct |
71 ms |
31624 KB |
Output is correct |
5 |
Correct |
69 ms |
31552 KB |
Output is correct |
6 |
Correct |
63 ms |
31560 KB |
Output is correct |
7 |
Correct |
62 ms |
31556 KB |
Output is correct |
8 |
Correct |
67 ms |
31004 KB |
Output is correct |
9 |
Correct |
48 ms |
29024 KB |
Output is correct |
10 |
Correct |
66 ms |
30916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23884 KB |
Output is correct |
6 |
Correct |
14 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
23884 KB |
Output is correct |
8 |
Correct |
14 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
15 ms |
24084 KB |
Output is correct |
12 |
Correct |
16 ms |
24124 KB |
Output is correct |
13 |
Correct |
18 ms |
24200 KB |
Output is correct |
14 |
Correct |
16 ms |
24356 KB |
Output is correct |
15 |
Correct |
17 ms |
24332 KB |
Output is correct |
16 |
Correct |
16 ms |
24192 KB |
Output is correct |
17 |
Correct |
15 ms |
24140 KB |
Output is correct |
18 |
Correct |
16 ms |
24292 KB |
Output is correct |
19 |
Correct |
187 ms |
45060 KB |
Output is correct |
20 |
Correct |
189 ms |
45124 KB |
Output is correct |
21 |
Correct |
158 ms |
45572 KB |
Output is correct |
22 |
Correct |
157 ms |
45500 KB |
Output is correct |
23 |
Correct |
152 ms |
45480 KB |
Output is correct |
24 |
Correct |
129 ms |
42392 KB |
Output is correct |
25 |
Correct |
131 ms |
42436 KB |
Output is correct |
26 |
Correct |
150 ms |
42368 KB |
Output is correct |
27 |
Correct |
152 ms |
42244 KB |
Output is correct |
28 |
Correct |
158 ms |
42124 KB |
Output is correct |
29 |
Correct |
164 ms |
41992 KB |
Output is correct |
30 |
Correct |
165 ms |
42092 KB |
Output is correct |
31 |
Correct |
167 ms |
42112 KB |
Output is correct |
32 |
Correct |
165 ms |
41980 KB |
Output is correct |
33 |
Correct |
162 ms |
41940 KB |
Output is correct |
34 |
Correct |
124 ms |
41792 KB |
Output is correct |
35 |
Correct |
132 ms |
41864 KB |
Output is correct |
36 |
Correct |
116 ms |
41540 KB |
Output is correct |
37 |
Correct |
120 ms |
41620 KB |
Output is correct |
38 |
Correct |
123 ms |
41772 KB |
Output is correct |
39 |
Correct |
146 ms |
41184 KB |
Output is correct |
40 |
Correct |
116 ms |
37860 KB |
Output is correct |
41 |
Correct |
138 ms |
39536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
13 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23884 KB |
Output is correct |
6 |
Correct |
14 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
23884 KB |
Output is correct |
8 |
Correct |
14 ms |
23812 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
15 ms |
24084 KB |
Output is correct |
12 |
Correct |
16 ms |
24124 KB |
Output is correct |
13 |
Correct |
18 ms |
24200 KB |
Output is correct |
14 |
Correct |
16 ms |
24356 KB |
Output is correct |
15 |
Correct |
17 ms |
24332 KB |
Output is correct |
16 |
Correct |
16 ms |
24192 KB |
Output is correct |
17 |
Correct |
15 ms |
24140 KB |
Output is correct |
18 |
Correct |
16 ms |
24292 KB |
Output is correct |
19 |
Correct |
1065 ms |
97516 KB |
Output is correct |
20 |
Correct |
1097 ms |
97552 KB |
Output is correct |
21 |
Correct |
1108 ms |
97420 KB |
Output is correct |
22 |
Correct |
1097 ms |
97532 KB |
Output is correct |
23 |
Correct |
885 ms |
81856 KB |
Output is correct |
24 |
Correct |
82 ms |
31220 KB |
Output is correct |
25 |
Correct |
87 ms |
33216 KB |
Output is correct |
26 |
Correct |
82 ms |
31628 KB |
Output is correct |
27 |
Correct |
71 ms |
31624 KB |
Output is correct |
28 |
Correct |
69 ms |
31552 KB |
Output is correct |
29 |
Correct |
63 ms |
31560 KB |
Output is correct |
30 |
Correct |
62 ms |
31556 KB |
Output is correct |
31 |
Correct |
67 ms |
31004 KB |
Output is correct |
32 |
Correct |
48 ms |
29024 KB |
Output is correct |
33 |
Correct |
66 ms |
30916 KB |
Output is correct |
34 |
Correct |
187 ms |
45060 KB |
Output is correct |
35 |
Correct |
189 ms |
45124 KB |
Output is correct |
36 |
Correct |
158 ms |
45572 KB |
Output is correct |
37 |
Correct |
157 ms |
45500 KB |
Output is correct |
38 |
Correct |
152 ms |
45480 KB |
Output is correct |
39 |
Correct |
129 ms |
42392 KB |
Output is correct |
40 |
Correct |
131 ms |
42436 KB |
Output is correct |
41 |
Correct |
150 ms |
42368 KB |
Output is correct |
42 |
Correct |
152 ms |
42244 KB |
Output is correct |
43 |
Correct |
158 ms |
42124 KB |
Output is correct |
44 |
Correct |
164 ms |
41992 KB |
Output is correct |
45 |
Correct |
165 ms |
42092 KB |
Output is correct |
46 |
Correct |
167 ms |
42112 KB |
Output is correct |
47 |
Correct |
165 ms |
41980 KB |
Output is correct |
48 |
Correct |
162 ms |
41940 KB |
Output is correct |
49 |
Correct |
124 ms |
41792 KB |
Output is correct |
50 |
Correct |
132 ms |
41864 KB |
Output is correct |
51 |
Correct |
116 ms |
41540 KB |
Output is correct |
52 |
Correct |
120 ms |
41620 KB |
Output is correct |
53 |
Correct |
123 ms |
41772 KB |
Output is correct |
54 |
Correct |
146 ms |
41184 KB |
Output is correct |
55 |
Correct |
116 ms |
37860 KB |
Output is correct |
56 |
Correct |
138 ms |
39536 KB |
Output is correct |
57 |
Correct |
1123 ms |
131124 KB |
Output is correct |
58 |
Correct |
1130 ms |
131196 KB |
Output is correct |
59 |
Correct |
1038 ms |
131952 KB |
Output is correct |
60 |
Correct |
1037 ms |
131892 KB |
Output is correct |
61 |
Correct |
1038 ms |
131756 KB |
Output is correct |
62 |
Correct |
1037 ms |
131904 KB |
Output is correct |
63 |
Correct |
644 ms |
116076 KB |
Output is correct |
64 |
Correct |
652 ms |
116216 KB |
Output is correct |
65 |
Correct |
871 ms |
116536 KB |
Output is correct |
66 |
Correct |
873 ms |
116308 KB |
Output is correct |
67 |
Correct |
868 ms |
116316 KB |
Output is correct |
68 |
Correct |
927 ms |
115512 KB |
Output is correct |
69 |
Correct |
919 ms |
115388 KB |
Output is correct |
70 |
Correct |
922 ms |
111308 KB |
Output is correct |
71 |
Correct |
932 ms |
114780 KB |
Output is correct |
72 |
Correct |
906 ms |
109276 KB |
Output is correct |
73 |
Correct |
581 ms |
86888 KB |
Output is correct |
74 |
Correct |
596 ms |
87304 KB |
Output is correct |
75 |
Correct |
595 ms |
87404 KB |
Output is correct |
76 |
Correct |
560 ms |
87760 KB |
Output is correct |
77 |
Correct |
571 ms |
87024 KB |
Output is correct |
78 |
Correct |
803 ms |
91732 KB |
Output is correct |
79 |
Correct |
589 ms |
75612 KB |
Output is correct |
80 |
Correct |
782 ms |
85268 KB |
Output is correct |