#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 1e6 + 5, lg = 22, lng = 26, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int seg[maxn<<2], n;
int upd(int i, int k, int x = 1, int lx = 0, int rx = n){
if(lx + 1 == rx) return seg[x] = max(seg[x], k);
int mid = (lx + rx)>>1;
if(i < mid) upd(i, k, x<<1, lx, mid);
else upd(i, k, x<<1|1, mid, rx);
return seg[x] = max(seg[x<<1], seg[x<<1|1]);
}
int get(int l, int r, int x = 1, int lx = 0, int rx = n){
if(l <= lx && r >= rx) return seg[x];
if(l >= rx || r <= lx) return -1;
int mid = (lx + rx)>>1;
return max(get(l, r, x<<1, lx, mid), get(l, r, x<<1|1, mid, rx));
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
int q; cin >> n >> q;
vector<int> w(n);
rep(i,0,n){
cin >> w[i];
}
struct tr{
int r, id, k;
};
vector<vector<tr>> query(n);
rep(i,0,q){
int l, r, k; cin >> l >> r >> k; l--;
query[l].pb({r, i, k});
}
fill(seg, seg+(n<<2), -1);
vector<int> st;
vector<bool> ans(q);
per(i,n-1,0){
while(sz(st) && w[st.back()] < w[i]){
upd(st.back(), w[st.back()] + w[i]);
st.pop_back();
}
st.pb(i);
for(auto[r, id, k]: query[i]){
ans[id] = get(i, r) <= k;
}
}
for(auto c: ans) cout << c << '\n';
return 0;
}
/*
5 2
3 5 1 8 2
1 3 6
2 5 3
*/
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:59:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for(auto[r, id, k]: query[i]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
732 KB |
Output is correct |
15 |
Correct |
5 ms |
816 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1186 ms |
98952 KB |
Output is correct |
2 |
Correct |
1201 ms |
83072 KB |
Output is correct |
3 |
Correct |
1197 ms |
82812 KB |
Output is correct |
4 |
Correct |
1197 ms |
81596 KB |
Output is correct |
5 |
Correct |
1098 ms |
104100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
9028 KB |
Output is correct |
2 |
Correct |
72 ms |
8780 KB |
Output is correct |
3 |
Correct |
71 ms |
9380 KB |
Output is correct |
4 |
Correct |
77 ms |
9540 KB |
Output is correct |
5 |
Correct |
73 ms |
9552 KB |
Output is correct |
6 |
Correct |
56 ms |
8784 KB |
Output is correct |
7 |
Correct |
61 ms |
8824 KB |
Output is correct |
8 |
Correct |
63 ms |
8704 KB |
Output is correct |
9 |
Correct |
32 ms |
3396 KB |
Output is correct |
10 |
Correct |
66 ms |
8764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
732 KB |
Output is correct |
15 |
Correct |
5 ms |
816 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
684 KB |
Output is correct |
19 |
Correct |
208 ms |
20424 KB |
Output is correct |
20 |
Correct |
211 ms |
20448 KB |
Output is correct |
21 |
Correct |
164 ms |
19712 KB |
Output is correct |
22 |
Correct |
164 ms |
19792 KB |
Output is correct |
23 |
Correct |
170 ms |
19816 KB |
Output is correct |
24 |
Correct |
162 ms |
20548 KB |
Output is correct |
25 |
Correct |
148 ms |
20552 KB |
Output is correct |
26 |
Correct |
196 ms |
20736 KB |
Output is correct |
27 |
Correct |
177 ms |
20812 KB |
Output is correct |
28 |
Correct |
182 ms |
20760 KB |
Output is correct |
29 |
Correct |
198 ms |
21312 KB |
Output is correct |
30 |
Correct |
212 ms |
21288 KB |
Output is correct |
31 |
Correct |
208 ms |
21272 KB |
Output is correct |
32 |
Correct |
204 ms |
21316 KB |
Output is correct |
33 |
Correct |
195 ms |
21196 KB |
Output is correct |
34 |
Correct |
152 ms |
19784 KB |
Output is correct |
35 |
Correct |
148 ms |
19832 KB |
Output is correct |
36 |
Correct |
148 ms |
19644 KB |
Output is correct |
37 |
Correct |
162 ms |
19640 KB |
Output is correct |
38 |
Correct |
134 ms |
19840 KB |
Output is correct |
39 |
Correct |
190 ms |
19412 KB |
Output is correct |
40 |
Correct |
132 ms |
15084 KB |
Output is correct |
41 |
Correct |
188 ms |
18540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
732 KB |
Output is correct |
15 |
Correct |
5 ms |
816 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
684 KB |
Output is correct |
19 |
Correct |
1186 ms |
98952 KB |
Output is correct |
20 |
Correct |
1201 ms |
83072 KB |
Output is correct |
21 |
Correct |
1197 ms |
82812 KB |
Output is correct |
22 |
Correct |
1197 ms |
81596 KB |
Output is correct |
23 |
Correct |
1098 ms |
104100 KB |
Output is correct |
24 |
Correct |
82 ms |
9028 KB |
Output is correct |
25 |
Correct |
72 ms |
8780 KB |
Output is correct |
26 |
Correct |
71 ms |
9380 KB |
Output is correct |
27 |
Correct |
77 ms |
9540 KB |
Output is correct |
28 |
Correct |
73 ms |
9552 KB |
Output is correct |
29 |
Correct |
56 ms |
8784 KB |
Output is correct |
30 |
Correct |
61 ms |
8824 KB |
Output is correct |
31 |
Correct |
63 ms |
8704 KB |
Output is correct |
32 |
Correct |
32 ms |
3396 KB |
Output is correct |
33 |
Correct |
66 ms |
8764 KB |
Output is correct |
34 |
Correct |
208 ms |
20424 KB |
Output is correct |
35 |
Correct |
211 ms |
20448 KB |
Output is correct |
36 |
Correct |
164 ms |
19712 KB |
Output is correct |
37 |
Correct |
164 ms |
19792 KB |
Output is correct |
38 |
Correct |
170 ms |
19816 KB |
Output is correct |
39 |
Correct |
162 ms |
20548 KB |
Output is correct |
40 |
Correct |
148 ms |
20552 KB |
Output is correct |
41 |
Correct |
196 ms |
20736 KB |
Output is correct |
42 |
Correct |
177 ms |
20812 KB |
Output is correct |
43 |
Correct |
182 ms |
20760 KB |
Output is correct |
44 |
Correct |
198 ms |
21312 KB |
Output is correct |
45 |
Correct |
212 ms |
21288 KB |
Output is correct |
46 |
Correct |
208 ms |
21272 KB |
Output is correct |
47 |
Correct |
204 ms |
21316 KB |
Output is correct |
48 |
Correct |
195 ms |
21196 KB |
Output is correct |
49 |
Correct |
152 ms |
19784 KB |
Output is correct |
50 |
Correct |
148 ms |
19832 KB |
Output is correct |
51 |
Correct |
148 ms |
19644 KB |
Output is correct |
52 |
Correct |
162 ms |
19640 KB |
Output is correct |
53 |
Correct |
134 ms |
19840 KB |
Output is correct |
54 |
Correct |
190 ms |
19412 KB |
Output is correct |
55 |
Correct |
132 ms |
15084 KB |
Output is correct |
56 |
Correct |
188 ms |
18540 KB |
Output is correct |
57 |
Correct |
1388 ms |
101384 KB |
Output is correct |
58 |
Correct |
1219 ms |
101640 KB |
Output is correct |
59 |
Correct |
1145 ms |
99340 KB |
Output is correct |
60 |
Correct |
1146 ms |
99432 KB |
Output is correct |
61 |
Correct |
1136 ms |
99432 KB |
Output is correct |
62 |
Correct |
1169 ms |
99284 KB |
Output is correct |
63 |
Correct |
756 ms |
97768 KB |
Output is correct |
64 |
Correct |
702 ms |
97892 KB |
Output is correct |
65 |
Correct |
1043 ms |
103360 KB |
Output is correct |
66 |
Correct |
1031 ms |
103344 KB |
Output is correct |
67 |
Correct |
1093 ms |
103392 KB |
Output is correct |
68 |
Correct |
1140 ms |
105544 KB |
Output is correct |
69 |
Correct |
1150 ms |
105432 KB |
Output is correct |
70 |
Correct |
1146 ms |
105016 KB |
Output is correct |
71 |
Correct |
1188 ms |
105136 KB |
Output is correct |
72 |
Correct |
1136 ms |
105088 KB |
Output is correct |
73 |
Correct |
651 ms |
95332 KB |
Output is correct |
74 |
Correct |
669 ms |
95316 KB |
Output is correct |
75 |
Correct |
624 ms |
95280 KB |
Output is correct |
76 |
Correct |
620 ms |
95244 KB |
Output is correct |
77 |
Correct |
634 ms |
95496 KB |
Output is correct |
78 |
Correct |
898 ms |
96092 KB |
Output is correct |
79 |
Correct |
665 ms |
67604 KB |
Output is correct |
80 |
Correct |
899 ms |
92152 KB |
Output is correct |