#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
const int mod = 1e9+7, N = 1e6+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int a[N], n, k, sz=1;
pii Q[N];
vector<int> L[N];
int t[N*2];
void update(int p, int val){
p+=n;
for(t[p]=max(val,t[p]);p>1;p>>=1)t[p>>1]=max(t[p], t[p^1]);
}
int get(int l, int r){
int res = 0;
for(l+=n,r+=n;l<=r;l>>=1,r>>=1){
if(l%2==1)res=max(t[l++],res);
if(r%2==0)res=max(t[r--],res);
}return res;
}
void solve(int test_case){
int i, j, q;
cin >> n >> q;
while(sz<n)sz<<=1;sz=n;
for(i=0;i<n;i++){
cin >> a[i];
}
vector<int> ans(q);
for(i=0;i<q;i++){
int l, r, k;
cin >> l >> r >> k;
l--,r--;
L[r].pb(i);
Q[i] = {l, k};
}
vector<pii> v;
for(i=0;i<n;i++){
while(v.size() && v.back().ff < a[i])v.pop_back();
bool f=0;
if(!v.size()){
v.pb({a[i], i});
f=1;
}
if(v.back().ff > a[i])update(v.back().ss, v.back().ff+a[i]);
if(!f)v.pb({a[i],i});
for(auto idx : L[i]){
ans[idx] = (get(Q[idx].ff, i) <= Q[idx].ss);
}
v.pb({a[i], i});
}
for(i=0;i<q;i++)cout << ans[i] << '\n';
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int ___T;
cin >> ___T;
for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
Compilation message
sortbooks.cpp: In function 'void solve(long long int)':
sortbooks.cpp:32:2: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
32 | while(sz<n)sz<<=1;sz=n;
| ^~~~~
sortbooks.cpp:32:20: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
32 | while(sz<n)sz<<=1;sz=n;
| ^~
sortbooks.cpp:30:9: warning: unused variable 'j' [-Wunused-variable]
30 | int i, j, q;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23916 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
15 ms |
23916 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
15 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23916 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
15 ms |
23916 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
15 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
18 ms |
24044 KB |
Output is correct |
14 |
Correct |
20 ms |
24172 KB |
Output is correct |
15 |
Correct |
19 ms |
24172 KB |
Output is correct |
16 |
Correct |
18 ms |
24172 KB |
Output is correct |
17 |
Correct |
18 ms |
24172 KB |
Output is correct |
18 |
Correct |
18 ms |
24300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1383 ms |
92744 KB |
Output is correct |
2 |
Correct |
1410 ms |
92908 KB |
Output is correct |
3 |
Correct |
1392 ms |
93036 KB |
Output is correct |
4 |
Correct |
1377 ms |
92908 KB |
Output is correct |
5 |
Correct |
1221 ms |
77420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
30572 KB |
Output is correct |
2 |
Correct |
93 ms |
30084 KB |
Output is correct |
3 |
Correct |
93 ms |
28908 KB |
Output is correct |
4 |
Correct |
97 ms |
29036 KB |
Output is correct |
5 |
Correct |
98 ms |
29036 KB |
Output is correct |
6 |
Correct |
75 ms |
28396 KB |
Output is correct |
7 |
Correct |
75 ms |
28524 KB |
Output is correct |
8 |
Correct |
89 ms |
30944 KB |
Output is correct |
9 |
Correct |
57 ms |
27364 KB |
Output is correct |
10 |
Correct |
96 ms |
30948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23916 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
15 ms |
23916 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
15 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
18 ms |
24044 KB |
Output is correct |
14 |
Correct |
20 ms |
24172 KB |
Output is correct |
15 |
Correct |
19 ms |
24172 KB |
Output is correct |
16 |
Correct |
18 ms |
24172 KB |
Output is correct |
17 |
Correct |
18 ms |
24172 KB |
Output is correct |
18 |
Correct |
18 ms |
24300 KB |
Output is correct |
19 |
Correct |
246 ms |
37484 KB |
Output is correct |
20 |
Correct |
244 ms |
37484 KB |
Output is correct |
21 |
Correct |
189 ms |
36332 KB |
Output is correct |
22 |
Correct |
193 ms |
36332 KB |
Output is correct |
23 |
Correct |
197 ms |
36332 KB |
Output is correct |
24 |
Correct |
174 ms |
33132 KB |
Output is correct |
25 |
Correct |
173 ms |
33052 KB |
Output is correct |
26 |
Correct |
201 ms |
33516 KB |
Output is correct |
27 |
Correct |
197 ms |
33516 KB |
Output is correct |
28 |
Correct |
203 ms |
33772 KB |
Output is correct |
29 |
Correct |
216 ms |
34284 KB |
Output is correct |
30 |
Correct |
231 ms |
34284 KB |
Output is correct |
31 |
Correct |
219 ms |
34412 KB |
Output is correct |
32 |
Correct |
217 ms |
34156 KB |
Output is correct |
33 |
Correct |
221 ms |
34156 KB |
Output is correct |
34 |
Correct |
157 ms |
32876 KB |
Output is correct |
35 |
Correct |
162 ms |
32876 KB |
Output is correct |
36 |
Correct |
163 ms |
32876 KB |
Output is correct |
37 |
Correct |
156 ms |
32876 KB |
Output is correct |
38 |
Correct |
159 ms |
32880 KB |
Output is correct |
39 |
Correct |
200 ms |
35172 KB |
Output is correct |
40 |
Correct |
165 ms |
33120 KB |
Output is correct |
41 |
Correct |
197 ms |
37856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23916 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
15 ms |
23916 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
15 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
17 ms |
24044 KB |
Output is correct |
12 |
Correct |
17 ms |
24044 KB |
Output is correct |
13 |
Correct |
18 ms |
24044 KB |
Output is correct |
14 |
Correct |
20 ms |
24172 KB |
Output is correct |
15 |
Correct |
19 ms |
24172 KB |
Output is correct |
16 |
Correct |
18 ms |
24172 KB |
Output is correct |
17 |
Correct |
18 ms |
24172 KB |
Output is correct |
18 |
Correct |
18 ms |
24300 KB |
Output is correct |
19 |
Correct |
1383 ms |
92744 KB |
Output is correct |
20 |
Correct |
1410 ms |
92908 KB |
Output is correct |
21 |
Correct |
1392 ms |
93036 KB |
Output is correct |
22 |
Correct |
1377 ms |
92908 KB |
Output is correct |
23 |
Correct |
1221 ms |
77420 KB |
Output is correct |
24 |
Correct |
107 ms |
30572 KB |
Output is correct |
25 |
Correct |
93 ms |
30084 KB |
Output is correct |
26 |
Correct |
93 ms |
28908 KB |
Output is correct |
27 |
Correct |
97 ms |
29036 KB |
Output is correct |
28 |
Correct |
98 ms |
29036 KB |
Output is correct |
29 |
Correct |
75 ms |
28396 KB |
Output is correct |
30 |
Correct |
75 ms |
28524 KB |
Output is correct |
31 |
Correct |
89 ms |
30944 KB |
Output is correct |
32 |
Correct |
57 ms |
27364 KB |
Output is correct |
33 |
Correct |
96 ms |
30948 KB |
Output is correct |
34 |
Correct |
246 ms |
37484 KB |
Output is correct |
35 |
Correct |
244 ms |
37484 KB |
Output is correct |
36 |
Correct |
189 ms |
36332 KB |
Output is correct |
37 |
Correct |
193 ms |
36332 KB |
Output is correct |
38 |
Correct |
197 ms |
36332 KB |
Output is correct |
39 |
Correct |
174 ms |
33132 KB |
Output is correct |
40 |
Correct |
173 ms |
33052 KB |
Output is correct |
41 |
Correct |
201 ms |
33516 KB |
Output is correct |
42 |
Correct |
197 ms |
33516 KB |
Output is correct |
43 |
Correct |
203 ms |
33772 KB |
Output is correct |
44 |
Correct |
216 ms |
34284 KB |
Output is correct |
45 |
Correct |
231 ms |
34284 KB |
Output is correct |
46 |
Correct |
219 ms |
34412 KB |
Output is correct |
47 |
Correct |
217 ms |
34156 KB |
Output is correct |
48 |
Correct |
221 ms |
34156 KB |
Output is correct |
49 |
Correct |
157 ms |
32876 KB |
Output is correct |
50 |
Correct |
162 ms |
32876 KB |
Output is correct |
51 |
Correct |
163 ms |
32876 KB |
Output is correct |
52 |
Correct |
156 ms |
32876 KB |
Output is correct |
53 |
Correct |
159 ms |
32880 KB |
Output is correct |
54 |
Correct |
200 ms |
35172 KB |
Output is correct |
55 |
Correct |
165 ms |
33120 KB |
Output is correct |
56 |
Correct |
197 ms |
37856 KB |
Output is correct |
57 |
Correct |
1353 ms |
91556 KB |
Output is correct |
58 |
Correct |
1346 ms |
91756 KB |
Output is correct |
59 |
Correct |
1283 ms |
89020 KB |
Output is correct |
60 |
Correct |
1283 ms |
88940 KB |
Output is correct |
61 |
Correct |
1282 ms |
89068 KB |
Output is correct |
62 |
Correct |
1281 ms |
88940 KB |
Output is correct |
63 |
Correct |
842 ms |
69612 KB |
Output is correct |
64 |
Correct |
855 ms |
69740 KB |
Output is correct |
65 |
Correct |
1153 ms |
73192 KB |
Output is correct |
66 |
Correct |
1161 ms |
72940 KB |
Output is correct |
67 |
Correct |
1144 ms |
73196 KB |
Output is correct |
68 |
Correct |
1200 ms |
76012 KB |
Output is correct |
69 |
Correct |
1209 ms |
76100 KB |
Output is correct |
70 |
Correct |
1204 ms |
75452 KB |
Output is correct |
71 |
Correct |
1202 ms |
75556 KB |
Output is correct |
72 |
Correct |
1203 ms |
75372 KB |
Output is correct |
73 |
Correct |
758 ms |
68204 KB |
Output is correct |
74 |
Correct |
781 ms |
68160 KB |
Output is correct |
75 |
Correct |
749 ms |
68332 KB |
Output is correct |
76 |
Correct |
761 ms |
68332 KB |
Output is correct |
77 |
Correct |
765 ms |
67948 KB |
Output is correct |
78 |
Correct |
1065 ms |
79456 KB |
Output is correct |
79 |
Correct |
823 ms |
68180 KB |
Output is correct |
80 |
Correct |
1014 ms |
90288 KB |
Output is correct |