#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second
struct node {
int mval, ans, push;
node() = default;
node(int mval_, int ans_) {
mval = mval_;
ans = ans_;
push = 0;
}
};
node operator+(node x, node y) {
return node(max(x.mval, y.mval), max(x.ans, y.ans));
}
struct segtree {
int n;
vector<node> t;
vector<int> a;
segtree() = default;
segtree(int n_, vector<int> a_) {
n = n_;
a = a_;
t.resize(4 * n);
build(1, 0, n);
}
void build(int v, int tl, int tr) {
if (tl + 1 == tr) {
t[v] = node(a[tl], 0);
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm); build(v * 2 + 1, tm, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
void push(int v, int tl, int tr) {
if (t[v].push > 0) {
t[v].ans = t[v].mval + t[v].push;
if (tl + 1 < tr) {
t[v * 2].push = t[v].push;
t[v * 2 + 1].push = t[v].push;
}
t[v].push = 0;
return;
}
if (tl + 1 == tr) return;
int vll = (t[v * 2].push ? t[v * 2].push + t[v * 2].mval : t[v * 2].ans), vlr = (t[v * 2 + 1].push ? t[v * 2 + 1].push + t[v * 2 + 1].mval : t[v * 2 + 1].ans);
t[v].ans = max(vll, vlr);
}
void upd(int v, int tl, int tr, int l, int r, int x) {
push(v, tl, tr);
if (l <= tl && tr <= r) {
t[v].push = x;
return;
}
if (tr <= l || tl >= r) return;
int tm = (tl + tr) / 2;
upd(v * 2, tl, tm, l, r, x); upd(v * 2 + 1, tm, tr, l, r, x);
push(v, tl, tr);
}
node get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (tr <= l || tl >= r) return node(0, 0);
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2;
return get(v * 2, tl, tm, l, r) + get(v * 2 + 1, tm, tr, l, r);
}
};
void solve() {
int n, q; cin >> n >> q;
vector<int> w(n);
for (int i = 0; i < n; i++) cin >> w[i];
vector<vector<vector<int>>> reqs(n);
for (int i = 0; i < q; i++) {
int l, r, k; cin >> l >> r >> k;
l--; r--;
reqs[l].pb({i, r, k});
}
vector<int> ans(q, 1);
segtree sg(n, w);
stack<int> st;
st.push(n - 1);
for (int i = n - 2; i >= 0; i--) {
while (!st.empty() && w[i] > w[st.top()]) {
st.pop();
}
int nxt = (st.empty() ? n : st.top());
if (nxt - i > 1) {
sg.upd(1, 0, n, i + 1, nxt, w[i]);
}
st.push(i);
for (auto u : reqs[i]) {
int mx_n = sg.get(1, 0, n, i, u[1] + 1).ans;
ans[u[0]] = (mx_n <= u[2]);
}
}
for (auto &u : ans) cout << u << '\n';
}
signed main() {
int tt = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
while (tt--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
388 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
388 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1136 KB |
Output is correct |
15 |
Correct |
5 ms |
1372 KB |
Output is correct |
16 |
Correct |
4 ms |
1320 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1796 ms |
182056 KB |
Output is correct |
2 |
Correct |
1914 ms |
183180 KB |
Output is correct |
3 |
Correct |
1822 ms |
183224 KB |
Output is correct |
4 |
Correct |
1865 ms |
183380 KB |
Output is correct |
5 |
Correct |
1660 ms |
185248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
17236 KB |
Output is correct |
2 |
Correct |
108 ms |
17336 KB |
Output is correct |
3 |
Correct |
96 ms |
17612 KB |
Output is correct |
4 |
Correct |
98 ms |
17608 KB |
Output is correct |
5 |
Correct |
94 ms |
17596 KB |
Output is correct |
6 |
Correct |
97 ms |
17232 KB |
Output is correct |
7 |
Correct |
90 ms |
17096 KB |
Output is correct |
8 |
Correct |
75 ms |
16900 KB |
Output is correct |
9 |
Correct |
35 ms |
8196 KB |
Output is correct |
10 |
Correct |
73 ms |
16996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
388 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1136 KB |
Output is correct |
15 |
Correct |
5 ms |
1372 KB |
Output is correct |
16 |
Correct |
4 ms |
1320 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
1116 KB |
Output is correct |
19 |
Correct |
284 ms |
36936 KB |
Output is correct |
20 |
Correct |
282 ms |
36884 KB |
Output is correct |
21 |
Correct |
245 ms |
37200 KB |
Output is correct |
22 |
Correct |
249 ms |
37200 KB |
Output is correct |
23 |
Correct |
288 ms |
37204 KB |
Output is correct |
24 |
Correct |
213 ms |
37696 KB |
Output is correct |
25 |
Correct |
233 ms |
37652 KB |
Output is correct |
26 |
Correct |
241 ms |
37548 KB |
Output is correct |
27 |
Correct |
231 ms |
37332 KB |
Output is correct |
28 |
Correct |
240 ms |
37456 KB |
Output is correct |
29 |
Correct |
223 ms |
37180 KB |
Output is correct |
30 |
Correct |
248 ms |
37184 KB |
Output is correct |
31 |
Correct |
228 ms |
37284 KB |
Output is correct |
32 |
Correct |
229 ms |
37192 KB |
Output is correct |
33 |
Correct |
234 ms |
37188 KB |
Output is correct |
34 |
Correct |
191 ms |
36568 KB |
Output is correct |
35 |
Correct |
204 ms |
36628 KB |
Output is correct |
36 |
Correct |
205 ms |
36416 KB |
Output is correct |
37 |
Correct |
196 ms |
36492 KB |
Output is correct |
38 |
Correct |
197 ms |
36672 KB |
Output is correct |
39 |
Correct |
208 ms |
35320 KB |
Output is correct |
40 |
Correct |
140 ms |
28676 KB |
Output is correct |
41 |
Correct |
185 ms |
34820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
388 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
3 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1136 KB |
Output is correct |
15 |
Correct |
5 ms |
1372 KB |
Output is correct |
16 |
Correct |
4 ms |
1320 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
1116 KB |
Output is correct |
19 |
Correct |
1796 ms |
182056 KB |
Output is correct |
20 |
Correct |
1914 ms |
183180 KB |
Output is correct |
21 |
Correct |
1822 ms |
183224 KB |
Output is correct |
22 |
Correct |
1865 ms |
183380 KB |
Output is correct |
23 |
Correct |
1660 ms |
185248 KB |
Output is correct |
24 |
Correct |
133 ms |
17236 KB |
Output is correct |
25 |
Correct |
108 ms |
17336 KB |
Output is correct |
26 |
Correct |
96 ms |
17612 KB |
Output is correct |
27 |
Correct |
98 ms |
17608 KB |
Output is correct |
28 |
Correct |
94 ms |
17596 KB |
Output is correct |
29 |
Correct |
97 ms |
17232 KB |
Output is correct |
30 |
Correct |
90 ms |
17096 KB |
Output is correct |
31 |
Correct |
75 ms |
16900 KB |
Output is correct |
32 |
Correct |
35 ms |
8196 KB |
Output is correct |
33 |
Correct |
73 ms |
16996 KB |
Output is correct |
34 |
Correct |
284 ms |
36936 KB |
Output is correct |
35 |
Correct |
282 ms |
36884 KB |
Output is correct |
36 |
Correct |
245 ms |
37200 KB |
Output is correct |
37 |
Correct |
249 ms |
37200 KB |
Output is correct |
38 |
Correct |
288 ms |
37204 KB |
Output is correct |
39 |
Correct |
213 ms |
37696 KB |
Output is correct |
40 |
Correct |
233 ms |
37652 KB |
Output is correct |
41 |
Correct |
241 ms |
37548 KB |
Output is correct |
42 |
Correct |
231 ms |
37332 KB |
Output is correct |
43 |
Correct |
240 ms |
37456 KB |
Output is correct |
44 |
Correct |
223 ms |
37180 KB |
Output is correct |
45 |
Correct |
248 ms |
37184 KB |
Output is correct |
46 |
Correct |
228 ms |
37284 KB |
Output is correct |
47 |
Correct |
229 ms |
37192 KB |
Output is correct |
48 |
Correct |
234 ms |
37188 KB |
Output is correct |
49 |
Correct |
191 ms |
36568 KB |
Output is correct |
50 |
Correct |
204 ms |
36628 KB |
Output is correct |
51 |
Correct |
205 ms |
36416 KB |
Output is correct |
52 |
Correct |
196 ms |
36492 KB |
Output is correct |
53 |
Correct |
197 ms |
36672 KB |
Output is correct |
54 |
Correct |
208 ms |
35320 KB |
Output is correct |
55 |
Correct |
140 ms |
28676 KB |
Output is correct |
56 |
Correct |
185 ms |
34820 KB |
Output is correct |
57 |
Correct |
1754 ms |
183440 KB |
Output is correct |
58 |
Correct |
1884 ms |
183432 KB |
Output is correct |
59 |
Correct |
1713 ms |
184144 KB |
Output is correct |
60 |
Correct |
1711 ms |
184144 KB |
Output is correct |
61 |
Correct |
1728 ms |
184152 KB |
Output is correct |
62 |
Correct |
1716 ms |
184080 KB |
Output is correct |
63 |
Correct |
1237 ms |
182328 KB |
Output is correct |
64 |
Correct |
1270 ms |
182296 KB |
Output is correct |
65 |
Correct |
1570 ms |
186432 KB |
Output is correct |
66 |
Correct |
1473 ms |
186436 KB |
Output is correct |
67 |
Correct |
1542 ms |
186328 KB |
Output is correct |
68 |
Correct |
1574 ms |
185616 KB |
Output is correct |
69 |
Correct |
1602 ms |
185652 KB |
Output is correct |
70 |
Correct |
1616 ms |
185864 KB |
Output is correct |
71 |
Correct |
1700 ms |
185916 KB |
Output is correct |
72 |
Correct |
1603 ms |
185804 KB |
Output is correct |
73 |
Correct |
1188 ms |
175996 KB |
Output is correct |
74 |
Correct |
1257 ms |
176728 KB |
Output is correct |
75 |
Correct |
1173 ms |
175888 KB |
Output is correct |
76 |
Correct |
1215 ms |
176012 KB |
Output is correct |
77 |
Correct |
1294 ms |
176204 KB |
Output is correct |
78 |
Correct |
1443 ms |
176380 KB |
Output is correct |
79 |
Correct |
883 ms |
131340 KB |
Output is correct |
80 |
Correct |
1165 ms |
174028 KB |
Output is correct |