#include <bits/stdc++.h>
#define N 1000005
#define K 20
using namespace std;
int len[N];
int maxi[N][K];
int val[N][K];
int nxt[N];
int a[N];
struct node{
int val,maxi;
};
int get(int l,int r){
if(r < l)
return 0;
int ln = r-l+1;
if(a[maxi[l][len[ln]]] > a[maxi[r - (1<<len[ln]) + 1][len[ln]]])
return maxi[l][len[ln]];
else return maxi[r - (1<<len[ln]) + 1][len[ln]];
}
void solve(){
int n,m;
cin >> n >> m;
for(int i = 1;i<=n;i++){
len[i] = len[i-1];
if((1<<(len[i]+1)) < i)
len[i]++;
cin >> a[i];
maxi[i][0] = i;
}
for(int j = 1;j<K;j++){
for(int i = 1;i<=n;i++){
if(i + (1<<j) - 1 <= n){
maxi[i][j] = maxi[i][j-1];
if(a[maxi[i][j]] <= a[maxi[i + (1<<(j-1))][j-1]]){
maxi[i][j] = maxi[i + (1<<(j-1))][j-1];
}
}
}
}
stack<int> st;
st.push(n+1);
a[n+1] = 1e9 + 5;
a[0] = -1e9;
for(int i =n;i>=1;i--){
while(a[st.top()] < a[i])
st.pop();
assert(st.size());
nxt[i] = st.top();
st.push(i);
}
for(int j = 1;j<K;j++){
for(int i =1;i<=n;i++){
if(i + (1<<j) - 1 <= n){
val[i][j] = max(val[i][j-1],val[i + (1<<(j-1))][j-1]);
val[i][j] = max(val[i][j],a[maxi[i][j-1]] + a[get(i + (1<<(j-1)),min(nxt[maxi[i][j-1]]-1,i + (1<<j) - 1))]);
}
}
}
for(int i = 1;i<=m;i++){
int l,r,k;
cin >> l >> r >> k;
int now = 0;
int tmp = l;
for(int j = K-1;j>=0;j--){
if(tmp + (1<<j) - 1 <= r){
now = max(now,val[tmp][j]);
if(tmp != l)
now = max(now,a[get(l,tmp-1)] + a[get(tmp,min(nxt[get(l,tmp-1)]-1,tmp + (1<<j) - 1))]);
tmp += (1<<j);
}
}
cout << (now <= k) << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1296 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
6 ms |
1236 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
5 ms |
1240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2332 ms |
171024 KB |
Output is correct |
2 |
Correct |
2258 ms |
170716 KB |
Output is correct |
3 |
Correct |
2251 ms |
203268 KB |
Output is correct |
4 |
Correct |
2351 ms |
203528 KB |
Output is correct |
5 |
Correct |
1959 ms |
207700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
17848 KB |
Output is correct |
2 |
Correct |
164 ms |
17580 KB |
Output is correct |
3 |
Correct |
136 ms |
18036 KB |
Output is correct |
4 |
Correct |
111 ms |
17976 KB |
Output is correct |
5 |
Correct |
107 ms |
17992 KB |
Output is correct |
6 |
Correct |
86 ms |
17996 KB |
Output is correct |
7 |
Correct |
84 ms |
17904 KB |
Output is correct |
8 |
Correct |
129 ms |
17792 KB |
Output is correct |
9 |
Correct |
32 ms |
860 KB |
Output is correct |
10 |
Correct |
116 ms |
17808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1296 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
6 ms |
1236 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
5 ms |
1240 KB |
Output is correct |
19 |
Correct |
354 ms |
35132 KB |
Output is correct |
20 |
Correct |
341 ms |
34900 KB |
Output is correct |
21 |
Correct |
347 ms |
34904 KB |
Output is correct |
22 |
Correct |
333 ms |
34856 KB |
Output is correct |
23 |
Correct |
295 ms |
34808 KB |
Output is correct |
24 |
Correct |
253 ms |
35748 KB |
Output is correct |
25 |
Correct |
217 ms |
35728 KB |
Output is correct |
26 |
Correct |
267 ms |
35668 KB |
Output is correct |
27 |
Correct |
248 ms |
35656 KB |
Output is correct |
28 |
Correct |
275 ms |
35664 KB |
Output is correct |
29 |
Correct |
278 ms |
35800 KB |
Output is correct |
30 |
Correct |
301 ms |
35816 KB |
Output is correct |
31 |
Correct |
297 ms |
35776 KB |
Output is correct |
32 |
Correct |
295 ms |
35808 KB |
Output is correct |
33 |
Correct |
279 ms |
35632 KB |
Output is correct |
34 |
Correct |
200 ms |
35720 KB |
Output is correct |
35 |
Correct |
183 ms |
35732 KB |
Output is correct |
36 |
Correct |
194 ms |
35732 KB |
Output is correct |
37 |
Correct |
200 ms |
35724 KB |
Output is correct |
38 |
Correct |
191 ms |
35732 KB |
Output is correct |
39 |
Correct |
273 ms |
35484 KB |
Output is correct |
40 |
Correct |
186 ms |
23712 KB |
Output is correct |
41 |
Correct |
354 ms |
35412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1296 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
6 ms |
1236 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
5 ms |
1240 KB |
Output is correct |
19 |
Correct |
2332 ms |
171024 KB |
Output is correct |
20 |
Correct |
2258 ms |
170716 KB |
Output is correct |
21 |
Correct |
2251 ms |
203268 KB |
Output is correct |
22 |
Correct |
2351 ms |
203528 KB |
Output is correct |
23 |
Correct |
1959 ms |
207700 KB |
Output is correct |
24 |
Correct |
169 ms |
17848 KB |
Output is correct |
25 |
Correct |
164 ms |
17580 KB |
Output is correct |
26 |
Correct |
136 ms |
18036 KB |
Output is correct |
27 |
Correct |
111 ms |
17976 KB |
Output is correct |
28 |
Correct |
107 ms |
17992 KB |
Output is correct |
29 |
Correct |
86 ms |
17996 KB |
Output is correct |
30 |
Correct |
84 ms |
17904 KB |
Output is correct |
31 |
Correct |
129 ms |
17792 KB |
Output is correct |
32 |
Correct |
32 ms |
860 KB |
Output is correct |
33 |
Correct |
116 ms |
17808 KB |
Output is correct |
34 |
Correct |
354 ms |
35132 KB |
Output is correct |
35 |
Correct |
341 ms |
34900 KB |
Output is correct |
36 |
Correct |
347 ms |
34904 KB |
Output is correct |
37 |
Correct |
333 ms |
34856 KB |
Output is correct |
38 |
Correct |
295 ms |
34808 KB |
Output is correct |
39 |
Correct |
253 ms |
35748 KB |
Output is correct |
40 |
Correct |
217 ms |
35728 KB |
Output is correct |
41 |
Correct |
267 ms |
35668 KB |
Output is correct |
42 |
Correct |
248 ms |
35656 KB |
Output is correct |
43 |
Correct |
275 ms |
35664 KB |
Output is correct |
44 |
Correct |
278 ms |
35800 KB |
Output is correct |
45 |
Correct |
301 ms |
35816 KB |
Output is correct |
46 |
Correct |
297 ms |
35776 KB |
Output is correct |
47 |
Correct |
295 ms |
35808 KB |
Output is correct |
48 |
Correct |
279 ms |
35632 KB |
Output is correct |
49 |
Correct |
200 ms |
35720 KB |
Output is correct |
50 |
Correct |
183 ms |
35732 KB |
Output is correct |
51 |
Correct |
194 ms |
35732 KB |
Output is correct |
52 |
Correct |
200 ms |
35724 KB |
Output is correct |
53 |
Correct |
191 ms |
35732 KB |
Output is correct |
54 |
Correct |
273 ms |
35484 KB |
Output is correct |
55 |
Correct |
186 ms |
23712 KB |
Output is correct |
56 |
Correct |
354 ms |
35412 KB |
Output is correct |
57 |
Correct |
2309 ms |
204128 KB |
Output is correct |
58 |
Correct |
2192 ms |
204116 KB |
Output is correct |
59 |
Correct |
2339 ms |
204020 KB |
Output is correct |
60 |
Correct |
2278 ms |
204104 KB |
Output is correct |
61 |
Correct |
2373 ms |
204276 KB |
Output is correct |
62 |
Correct |
2424 ms |
204120 KB |
Output is correct |
63 |
Correct |
1283 ms |
206312 KB |
Output is correct |
64 |
Correct |
1301 ms |
206272 KB |
Output is correct |
65 |
Correct |
1887 ms |
208176 KB |
Output is correct |
66 |
Correct |
1839 ms |
208288 KB |
Output is correct |
67 |
Correct |
1864 ms |
208200 KB |
Output is correct |
68 |
Correct |
1866 ms |
208228 KB |
Output is correct |
69 |
Correct |
1830 ms |
208308 KB |
Output is correct |
70 |
Correct |
1941 ms |
208152 KB |
Output is correct |
71 |
Correct |
2008 ms |
208236 KB |
Output is correct |
72 |
Correct |
1903 ms |
208316 KB |
Output is correct |
73 |
Correct |
1078 ms |
204764 KB |
Output is correct |
74 |
Correct |
1053 ms |
205848 KB |
Output is correct |
75 |
Correct |
1046 ms |
204784 KB |
Output is correct |
76 |
Correct |
1111 ms |
204712 KB |
Output is correct |
77 |
Correct |
1038 ms |
204700 KB |
Output is correct |
78 |
Correct |
1835 ms |
200856 KB |
Output is correct |
79 |
Correct |
1088 ms |
114916 KB |
Output is correct |
80 |
Correct |
1784 ms |
197436 KB |
Output is correct |