#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
const int inf=1e9+7;
struct event{
int r,k,id;
};
int n,w[MAXN],q,l,r,k[MAXN],ans[MAXN];
vector<event> qs[MAXN];
int tree[4*MAXN],maxs[4*MAXN];
void build(int v,int l,int r){
if(l==r){
tree[v]=w[l];
}else{
int tt=(l+r)/2;
build(2*v,l,tt);
build(2*v+1,tt+1,r);
tree[v]=max(tree[2*v],tree[2*v+1]);
}
}
int getmax(int v,int l,int r,int ll,int rr){
if(ll>rr)return 0;
if(l==ll and r==rr)return tree[v];
int tt=(l+r)/2;
return max( getmax(2*v,l,tt,ll,min(tt,rr)) , getmax(2*v+1,tt+1,r,max(tt+1,ll),rr) );
}
void update(int v,int l,int r,int pos,int val){
if(l==r){
maxs[v]=val;
}else{
int tt=(l+r)/2;
if(pos<=tt)update(2*v,l,tt,pos,val);
else update(2*v+1,tt+1,r,pos,val);
maxs[v]=max(maxs[2*v],maxs[2*v+1]);
}
}
int getval(int v,int l,int r,int ll,int rr){
if(ll>rr)return 0;
if(l==ll and r==rr)return maxs[v];
int tt=(l+r)/2;
return max( getval(2*v,l,tt,ll,min(tt,rr)) , getval(2*v+1,tt+1,r,max(tt+1,ll),rr) );
}
vector< pair<int,int> > st;
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>w[i];
}
build(1,1,n);
for(int i=1;i<=q;i++){
cin>>l>>r>>k[i];
qs[l].push_back({r,k[i],i});
}
st.push_back({n+1,0});
w[n+1]=inf;
for(int i=n;i>=1;i--){
pair<int,int> curr={i,0};
while(!st.empty() and w[i]>w[st.back().first]){
curr.second=w[i]+w[st.back().first];
st.pop_back();
}
st.push_back(curr);
update(1,0,n,st.size()-1,curr.second);
for(int f=0;f<qs[i].size();f++){
int lt=0,rt=st.size(),tt;
while(lt+1<rt){
tt=(lt+rt)/2;
if(st[tt-1].first<=qs[i][f].r){
rt=tt;
}else{
lt=tt;
}
}
if(rt<st.size()) ans[qs[i][f].id]=getval(1,0,n,rt,st.size()-1);
if(rt>1){
rt--;
if(st[rt].first+1<=qs[i][f].r){
int s=getmax(1,1,n,st[rt].first+1,qs[i][f].r);
ans[qs[i][f].id]=max(ans[qs[i][f].id],w[st[rt].first]+s);
}
}
}
}
for(int i=1;i<=q;i++){
if(ans[i]>k[i])cout<<"0\n";
else cout<<"1\n";
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int f=0;f<qs[i].size();f++){
| ~^~~~~~~~~~~~~
sortbooks.cpp:99:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | if(rt<st.size()) ans[qs[i][f].id]=getval(1,0,n,rt,st.size()-1);
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23736 KB |
Output is correct |
5 |
Correct |
11 ms |
23920 KB |
Output is correct |
6 |
Correct |
11 ms |
23984 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23736 KB |
Output is correct |
5 |
Correct |
11 ms |
23920 KB |
Output is correct |
6 |
Correct |
11 ms |
23984 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23896 KB |
Output is correct |
11 |
Correct |
13 ms |
24152 KB |
Output is correct |
12 |
Correct |
14 ms |
24052 KB |
Output is correct |
13 |
Correct |
14 ms |
24152 KB |
Output is correct |
14 |
Correct |
16 ms |
24156 KB |
Output is correct |
15 |
Correct |
16 ms |
24156 KB |
Output is correct |
16 |
Correct |
16 ms |
24152 KB |
Output is correct |
17 |
Correct |
14 ms |
24260 KB |
Output is correct |
18 |
Correct |
14 ms |
24156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1511 ms |
67408 KB |
Output is correct |
2 |
Correct |
1696 ms |
100432 KB |
Output is correct |
3 |
Correct |
1666 ms |
100180 KB |
Output is correct |
4 |
Correct |
1642 ms |
100436 KB |
Output is correct |
5 |
Correct |
1700 ms |
116508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
28564 KB |
Output is correct |
2 |
Correct |
113 ms |
30288 KB |
Output is correct |
3 |
Correct |
118 ms |
32208 KB |
Output is correct |
4 |
Correct |
126 ms |
32280 KB |
Output is correct |
5 |
Correct |
118 ms |
32392 KB |
Output is correct |
6 |
Correct |
99 ms |
31428 KB |
Output is correct |
7 |
Correct |
103 ms |
31428 KB |
Output is correct |
8 |
Correct |
119 ms |
30984 KB |
Output is correct |
9 |
Correct |
80 ms |
27748 KB |
Output is correct |
10 |
Correct |
120 ms |
31228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23736 KB |
Output is correct |
5 |
Correct |
11 ms |
23920 KB |
Output is correct |
6 |
Correct |
11 ms |
23984 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23896 KB |
Output is correct |
11 |
Correct |
13 ms |
24152 KB |
Output is correct |
12 |
Correct |
14 ms |
24052 KB |
Output is correct |
13 |
Correct |
14 ms |
24152 KB |
Output is correct |
14 |
Correct |
16 ms |
24156 KB |
Output is correct |
15 |
Correct |
16 ms |
24156 KB |
Output is correct |
16 |
Correct |
16 ms |
24152 KB |
Output is correct |
17 |
Correct |
14 ms |
24260 KB |
Output is correct |
18 |
Correct |
14 ms |
24156 KB |
Output is correct |
19 |
Correct |
303 ms |
39856 KB |
Output is correct |
20 |
Correct |
284 ms |
39764 KB |
Output is correct |
21 |
Correct |
280 ms |
39252 KB |
Output is correct |
22 |
Correct |
282 ms |
38992 KB |
Output is correct |
23 |
Correct |
276 ms |
38996 KB |
Output is correct |
24 |
Correct |
265 ms |
42684 KB |
Output is correct |
25 |
Correct |
261 ms |
42736 KB |
Output is correct |
26 |
Correct |
297 ms |
42944 KB |
Output is correct |
27 |
Correct |
280 ms |
42936 KB |
Output is correct |
28 |
Correct |
295 ms |
42944 KB |
Output is correct |
29 |
Correct |
301 ms |
43456 KB |
Output is correct |
30 |
Correct |
295 ms |
43432 KB |
Output is correct |
31 |
Correct |
299 ms |
43464 KB |
Output is correct |
32 |
Correct |
293 ms |
43392 KB |
Output is correct |
33 |
Correct |
298 ms |
43508 KB |
Output is correct |
34 |
Correct |
253 ms |
41908 KB |
Output is correct |
35 |
Correct |
253 ms |
41828 KB |
Output is correct |
36 |
Correct |
243 ms |
41664 KB |
Output is correct |
37 |
Correct |
254 ms |
41928 KB |
Output is correct |
38 |
Correct |
254 ms |
41912 KB |
Output is correct |
39 |
Correct |
259 ms |
41116 KB |
Output is correct |
40 |
Correct |
203 ms |
37888 KB |
Output is correct |
41 |
Correct |
268 ms |
39596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
23736 KB |
Output is correct |
5 |
Correct |
11 ms |
23920 KB |
Output is correct |
6 |
Correct |
11 ms |
23984 KB |
Output is correct |
7 |
Correct |
12 ms |
23804 KB |
Output is correct |
8 |
Correct |
11 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23896 KB |
Output is correct |
11 |
Correct |
13 ms |
24152 KB |
Output is correct |
12 |
Correct |
14 ms |
24052 KB |
Output is correct |
13 |
Correct |
14 ms |
24152 KB |
Output is correct |
14 |
Correct |
16 ms |
24156 KB |
Output is correct |
15 |
Correct |
16 ms |
24156 KB |
Output is correct |
16 |
Correct |
16 ms |
24152 KB |
Output is correct |
17 |
Correct |
14 ms |
24260 KB |
Output is correct |
18 |
Correct |
14 ms |
24156 KB |
Output is correct |
19 |
Correct |
1511 ms |
67408 KB |
Output is correct |
20 |
Correct |
1696 ms |
100432 KB |
Output is correct |
21 |
Correct |
1666 ms |
100180 KB |
Output is correct |
22 |
Correct |
1642 ms |
100436 KB |
Output is correct |
23 |
Correct |
1700 ms |
116508 KB |
Output is correct |
24 |
Correct |
120 ms |
28564 KB |
Output is correct |
25 |
Correct |
113 ms |
30288 KB |
Output is correct |
26 |
Correct |
118 ms |
32208 KB |
Output is correct |
27 |
Correct |
126 ms |
32280 KB |
Output is correct |
28 |
Correct |
118 ms |
32392 KB |
Output is correct |
29 |
Correct |
99 ms |
31428 KB |
Output is correct |
30 |
Correct |
103 ms |
31428 KB |
Output is correct |
31 |
Correct |
119 ms |
30984 KB |
Output is correct |
32 |
Correct |
80 ms |
27748 KB |
Output is correct |
33 |
Correct |
120 ms |
31228 KB |
Output is correct |
34 |
Correct |
303 ms |
39856 KB |
Output is correct |
35 |
Correct |
284 ms |
39764 KB |
Output is correct |
36 |
Correct |
280 ms |
39252 KB |
Output is correct |
37 |
Correct |
282 ms |
38992 KB |
Output is correct |
38 |
Correct |
276 ms |
38996 KB |
Output is correct |
39 |
Correct |
265 ms |
42684 KB |
Output is correct |
40 |
Correct |
261 ms |
42736 KB |
Output is correct |
41 |
Correct |
297 ms |
42944 KB |
Output is correct |
42 |
Correct |
280 ms |
42936 KB |
Output is correct |
43 |
Correct |
295 ms |
42944 KB |
Output is correct |
44 |
Correct |
301 ms |
43456 KB |
Output is correct |
45 |
Correct |
295 ms |
43432 KB |
Output is correct |
46 |
Correct |
299 ms |
43464 KB |
Output is correct |
47 |
Correct |
293 ms |
43392 KB |
Output is correct |
48 |
Correct |
298 ms |
43508 KB |
Output is correct |
49 |
Correct |
253 ms |
41908 KB |
Output is correct |
50 |
Correct |
253 ms |
41828 KB |
Output is correct |
51 |
Correct |
243 ms |
41664 KB |
Output is correct |
52 |
Correct |
254 ms |
41928 KB |
Output is correct |
53 |
Correct |
254 ms |
41912 KB |
Output is correct |
54 |
Correct |
259 ms |
41116 KB |
Output is correct |
55 |
Correct |
203 ms |
37888 KB |
Output is correct |
56 |
Correct |
268 ms |
39596 KB |
Output is correct |
57 |
Correct |
1771 ms |
101788 KB |
Output is correct |
58 |
Correct |
1796 ms |
101972 KB |
Output is correct |
59 |
Correct |
1614 ms |
99716 KB |
Output is correct |
60 |
Correct |
1582 ms |
99764 KB |
Output is correct |
61 |
Correct |
1443 ms |
99668 KB |
Output is correct |
62 |
Correct |
1478 ms |
99924 KB |
Output is correct |
63 |
Correct |
1220 ms |
109996 KB |
Output is correct |
64 |
Correct |
1273 ms |
110372 KB |
Output is correct |
65 |
Correct |
1603 ms |
115984 KB |
Output is correct |
66 |
Correct |
1595 ms |
115824 KB |
Output is correct |
67 |
Correct |
1541 ms |
115860 KB |
Output is correct |
68 |
Correct |
1802 ms |
117888 KB |
Output is correct |
69 |
Correct |
2021 ms |
118104 KB |
Output is correct |
70 |
Correct |
1958 ms |
117540 KB |
Output is correct |
71 |
Correct |
2005 ms |
117616 KB |
Output is correct |
72 |
Correct |
2093 ms |
117516 KB |
Output is correct |
73 |
Correct |
1480 ms |
107916 KB |
Output is correct |
74 |
Correct |
1426 ms |
108780 KB |
Output is correct |
75 |
Correct |
1357 ms |
107804 KB |
Output is correct |
76 |
Correct |
1351 ms |
107756 KB |
Output is correct |
77 |
Correct |
1410 ms |
107872 KB |
Output is correct |
78 |
Correct |
1833 ms |
104428 KB |
Output is correct |
79 |
Correct |
1138 ms |
89344 KB |
Output is correct |
80 |
Correct |
1401 ms |
100880 KB |
Output is correct |