#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
struct node {
ll s,e,m,v;
node *l, *r;
node(ll _s,ll _e){
s = _s;
e = _e;
m = (s + e)>>1;
v = 0;
if(s != e){
l = new node(s,m);
r = new node(m + 1,e);
}
}
void update(ll x,ll val){
if(s == e){
v = max(v,val);
return;
}else{
if(x > m) r -> update(x,val);
else l -> update(x,val);
v = max(l -> v,r -> v);
}
}
ll rmaxq(ll x,ll y){
if(s == x && e == y){
return v;
}else{
if(x > m) return r -> rmaxq(x,y);
else if(y <= m) return l -> rmaxq(x,y);
else return max(l -> rmaxq(x,m),r -> rmaxq(m + 1,y));
}
}
} *root;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,M;
cin>>N>>M;
root = new node(0,N + 5);
ll W[N];
for(ll i = 0;i < N;i++){
cin>>W[i];
}
vector<pair<pair<ll,ll>,pair<ll,ll> > > queries; //{{R,L},{K,ith query}}
for(ll i = 0;i < M;i++){
ll L,R,K;
cin>>L>>R>>K;
L--;
R--;
queries.push_back({{R,L},{K,i}});
}
sort(queries.begin(),queries.end());
ll ans[M];
memset(ans,0,sizeof(ans));
ll Rptr = -1;
deque<pair<ll,ll> > dq;
for(ll q = 0;q < queries.size();q++){
ll L = queries[q].first.second;
ll R = queries[q].first.first;
ll K = queries[q].second.first;
ll index = queries[q].second.second;
while(Rptr < R){
Rptr++;
while(!dq.empty() && dq.front().first <= W[Rptr]){
dq.pop_front();
}
if(!dq.empty()){
ll Wj = dq.front().first;
ll j = -1 * dq.front().second;
root -> update(j,Wj + W[Rptr]);
}
while(!dq.empty() && dq.front().first <= W[Rptr]){
dq.pop_front();
}
dq.push_front(make_pair(W[Rptr],-Rptr));
}
ll maximum = root -> rmaxq(L,R);
if(maximum <= K){
ans[index] = 1;
}else{
ans[index] = 0;
}
}
for(ll i = 0;i < M;i++){
cout<<ans[i]<<'\n';
}
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:60:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(ll q = 0;q < queries.size();q++){
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
4 ms |
1324 KB |
Output is correct |
14 |
Correct |
5 ms |
1512 KB |
Output is correct |
15 |
Correct |
5 ms |
1476 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
4 ms |
1240 KB |
Output is correct |
18 |
Correct |
4 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1394 ms |
182204 KB |
Output is correct |
2 |
Correct |
1392 ms |
181408 KB |
Output is correct |
3 |
Correct |
1383 ms |
181788 KB |
Output is correct |
4 |
Correct |
1379 ms |
181372 KB |
Output is correct |
5 |
Correct |
1246 ms |
181044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
17796 KB |
Output is correct |
2 |
Correct |
105 ms |
19760 KB |
Output is correct |
3 |
Correct |
83 ms |
19796 KB |
Output is correct |
4 |
Correct |
86 ms |
19804 KB |
Output is correct |
5 |
Correct |
84 ms |
19780 KB |
Output is correct |
6 |
Correct |
83 ms |
19612 KB |
Output is correct |
7 |
Correct |
77 ms |
19708 KB |
Output is correct |
8 |
Correct |
80 ms |
19452 KB |
Output is correct |
9 |
Correct |
41 ms |
5864 KB |
Output is correct |
10 |
Correct |
99 ms |
19472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
4 ms |
1324 KB |
Output is correct |
14 |
Correct |
5 ms |
1512 KB |
Output is correct |
15 |
Correct |
5 ms |
1476 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
4 ms |
1240 KB |
Output is correct |
18 |
Correct |
4 ms |
1364 KB |
Output is correct |
19 |
Correct |
230 ms |
41864 KB |
Output is correct |
20 |
Correct |
229 ms |
41764 KB |
Output is correct |
21 |
Correct |
188 ms |
41712 KB |
Output is correct |
22 |
Correct |
196 ms |
41580 KB |
Output is correct |
23 |
Correct |
187 ms |
41704 KB |
Output is correct |
24 |
Correct |
164 ms |
41516 KB |
Output is correct |
25 |
Correct |
172 ms |
41576 KB |
Output is correct |
26 |
Correct |
187 ms |
41764 KB |
Output is correct |
27 |
Correct |
201 ms |
41772 KB |
Output is correct |
28 |
Correct |
201 ms |
41848 KB |
Output is correct |
29 |
Correct |
212 ms |
41820 KB |
Output is correct |
30 |
Correct |
223 ms |
41836 KB |
Output is correct |
31 |
Correct |
207 ms |
41752 KB |
Output is correct |
32 |
Correct |
207 ms |
41764 KB |
Output is correct |
33 |
Correct |
220 ms |
41828 KB |
Output is correct |
34 |
Correct |
163 ms |
41480 KB |
Output is correct |
35 |
Correct |
165 ms |
41468 KB |
Output is correct |
36 |
Correct |
157 ms |
41364 KB |
Output is correct |
37 |
Correct |
187 ms |
41384 KB |
Output is correct |
38 |
Correct |
160 ms |
41380 KB |
Output is correct |
39 |
Correct |
185 ms |
40424 KB |
Output is correct |
40 |
Correct |
150 ms |
30508 KB |
Output is correct |
41 |
Correct |
185 ms |
39972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
4 ms |
1324 KB |
Output is correct |
14 |
Correct |
5 ms |
1512 KB |
Output is correct |
15 |
Correct |
5 ms |
1476 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
4 ms |
1240 KB |
Output is correct |
18 |
Correct |
4 ms |
1364 KB |
Output is correct |
19 |
Correct |
1394 ms |
182204 KB |
Output is correct |
20 |
Correct |
1392 ms |
181408 KB |
Output is correct |
21 |
Correct |
1383 ms |
181788 KB |
Output is correct |
22 |
Correct |
1379 ms |
181372 KB |
Output is correct |
23 |
Correct |
1246 ms |
181044 KB |
Output is correct |
24 |
Correct |
96 ms |
17796 KB |
Output is correct |
25 |
Correct |
105 ms |
19760 KB |
Output is correct |
26 |
Correct |
83 ms |
19796 KB |
Output is correct |
27 |
Correct |
86 ms |
19804 KB |
Output is correct |
28 |
Correct |
84 ms |
19780 KB |
Output is correct |
29 |
Correct |
83 ms |
19612 KB |
Output is correct |
30 |
Correct |
77 ms |
19708 KB |
Output is correct |
31 |
Correct |
80 ms |
19452 KB |
Output is correct |
32 |
Correct |
41 ms |
5864 KB |
Output is correct |
33 |
Correct |
99 ms |
19472 KB |
Output is correct |
34 |
Correct |
230 ms |
41864 KB |
Output is correct |
35 |
Correct |
229 ms |
41764 KB |
Output is correct |
36 |
Correct |
188 ms |
41712 KB |
Output is correct |
37 |
Correct |
196 ms |
41580 KB |
Output is correct |
38 |
Correct |
187 ms |
41704 KB |
Output is correct |
39 |
Correct |
164 ms |
41516 KB |
Output is correct |
40 |
Correct |
172 ms |
41576 KB |
Output is correct |
41 |
Correct |
187 ms |
41764 KB |
Output is correct |
42 |
Correct |
201 ms |
41772 KB |
Output is correct |
43 |
Correct |
201 ms |
41848 KB |
Output is correct |
44 |
Correct |
212 ms |
41820 KB |
Output is correct |
45 |
Correct |
223 ms |
41836 KB |
Output is correct |
46 |
Correct |
207 ms |
41752 KB |
Output is correct |
47 |
Correct |
207 ms |
41764 KB |
Output is correct |
48 |
Correct |
220 ms |
41828 KB |
Output is correct |
49 |
Correct |
163 ms |
41480 KB |
Output is correct |
50 |
Correct |
165 ms |
41468 KB |
Output is correct |
51 |
Correct |
157 ms |
41364 KB |
Output is correct |
52 |
Correct |
187 ms |
41384 KB |
Output is correct |
53 |
Correct |
160 ms |
41380 KB |
Output is correct |
54 |
Correct |
185 ms |
40424 KB |
Output is correct |
55 |
Correct |
150 ms |
30508 KB |
Output is correct |
56 |
Correct |
185 ms |
39972 KB |
Output is correct |
57 |
Correct |
1457 ms |
208100 KB |
Output is correct |
58 |
Correct |
1394 ms |
208160 KB |
Output is correct |
59 |
Correct |
1334 ms |
207996 KB |
Output is correct |
60 |
Correct |
1335 ms |
208136 KB |
Output is correct |
61 |
Correct |
1350 ms |
207992 KB |
Output is correct |
62 |
Correct |
1297 ms |
208004 KB |
Output is correct |
63 |
Correct |
887 ms |
206276 KB |
Output is correct |
64 |
Correct |
886 ms |
206152 KB |
Output is correct |
65 |
Correct |
1231 ms |
207960 KB |
Output is correct |
66 |
Correct |
1218 ms |
207984 KB |
Output is correct |
67 |
Correct |
1211 ms |
208000 KB |
Output is correct |
68 |
Correct |
1304 ms |
208128 KB |
Output is correct |
69 |
Correct |
1309 ms |
208184 KB |
Output is correct |
70 |
Correct |
1310 ms |
208096 KB |
Output is correct |
71 |
Correct |
1304 ms |
208084 KB |
Output is correct |
72 |
Correct |
1330 ms |
208124 KB |
Output is correct |
73 |
Correct |
869 ms |
204640 KB |
Output is correct |
74 |
Correct |
871 ms |
205688 KB |
Output is correct |
75 |
Correct |
835 ms |
204728 KB |
Output is correct |
76 |
Correct |
856 ms |
204648 KB |
Output is correct |
77 |
Correct |
860 ms |
204648 KB |
Output is correct |
78 |
Correct |
1174 ms |
202012 KB |
Output is correct |
79 |
Correct |
836 ms |
133924 KB |
Output is correct |
80 |
Correct |
1132 ms |
199152 KB |
Output is correct |