#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
vector<pii> v[1000009];
int tab[1000009];
struct SegTree {
const static int T = 1<<20;
vector<pii> t[2*T];
void build(int n) {
for(int i=0;i<n;i++) {
if(sz(v[i])==0) continue;
t[i+T].pb(v[i][0]);
for(int j=1;j<sz(v[i]);j++) {
if(v[i][j].nd>t[i+T].back().nd)
t[i+T].pb({v[i][j].st, v[i][j].nd});
}
//cerr << "teraz: " << i+T << "\n";
for(pii x:t[i+T]) {
//cerr << x.st << " " << x.nd << "\n";
}
}
for(int i=T-1;i>=1;i--) {
int l = 2*i;
int r = 2*i+1;
int a = 0;
int b = 0;
while(a!=sz(t[l])||b!=sz(t[r])) {
if(a==sz(t[l])) {
if(t[r][b].nd>(sz(t[i])?t[i].back().nd:0))
t[i].pb({t[r][b].st, t[r][b].nd});
b++;
} else if(b==sz(t[r])) {
if(t[l][a].nd>(sz(t[i])?t[i].back().nd:0));
t[i].pb({t[l][a].st, t[l][a].nd});
a++;
} else if(t[l][a].st<=t[r][b].st) {
if(t[l][a].nd>(sz(t[i])?t[i].back().nd:0));
t[i].pb({t[l][a].st, t[l][a].nd});
a++;
} else {
if(t[r][b].nd>(sz(t[i])?t[i].back().nd:0))
t[i].pb({t[r][b].st, t[r][b].nd});
b++;
}
}
//cerr << "teraz: " << i << "\n";
for(pii x:t[i]) {
//cerr << x.st << " " << x.nd << "\n";
}
}
}
int query(int val, int l, int r, int tl=0, int tr=T-1, int v=1) {
//cerr << "query: " << val << " "<< l << " " << r << " " << tl << " " << tr << " " << v << "\n";
if(l > r) return 0;
if(tl==l&&tr==r) {
int s = -1;
int e = sz(t[v]);
while(e-s>1) {
int m = (e+s)/2;
if(t[v][m].st<=val) s = m;
else e = m;
}
if(s!=-1) return t[v][s].nd;
return 0;
}
int tm = (tl+tr)/2;
return max(query(val, l, min(r, tm), tl, tm, 2*v), query(val, max(l, tm+1), r, tm+1, tr, 2*v+1));
}
};
SegTree seg;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
for(int i=0;i<n;i++) {
cin >> tab[i];
}
vector<pii> s;
for(int i=0;i<n;i++) {
while(sz(s)&&s.back().nd<=tab[i]) {
s.pop_back();
}
if(sz(s)) {
//cerr << "dodaje: " << tab[i]+tab[s.back().st] << " do: " << s.back().st << "\n";
v[s.back().st].pb({i, tab[i]+tab[s.back().st]});
}
s.pb({i, tab[i]});
}
//cerr << "done\n";
seg.build(n);
for(int i=0;i<q;i++) {
int l, r, k;
cin >> l >> r >> k;
l--;
r--;
cout << (seg.query(r, l, r)<=k) << "\n";
}
}
Compilation message
sortbooks.cpp: In member function 'void SegTree::build(int64_t)':
sortbooks.cpp:27:12: warning: variable 'x' set but not used [-Wunused-but-set-variable]
27 | for(pii x:t[i+T]) {
| ^
sortbooks.cpp:42:6: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
42 | if(t[l][a].nd>(sz(t[i])?t[i].back().nd:0));
| ^~
sortbooks.cpp:43:7: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
43 | t[i].pb({t[l][a].st, t[l][a].nd});
| ^
sortbooks.cpp:46:6: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
46 | if(t[l][a].nd>(sz(t[i])?t[i].back().nd:0));
| ^~
sortbooks.cpp:47:7: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
47 | t[i].pb({t[l][a].st, t[l][a].nd});
| ^
sortbooks.cpp:56:12: warning: variable 'x' set but not used [-Wunused-but-set-variable]
56 | for(pii x:t[i]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
73016 KB |
Output is correct |
2 |
Correct |
44 ms |
72972 KB |
Output is correct |
3 |
Correct |
39 ms |
73060 KB |
Output is correct |
4 |
Correct |
39 ms |
73052 KB |
Output is correct |
5 |
Correct |
40 ms |
73076 KB |
Output is correct |
6 |
Correct |
38 ms |
73072 KB |
Output is correct |
7 |
Correct |
37 ms |
73092 KB |
Output is correct |
8 |
Correct |
41 ms |
72996 KB |
Output is correct |
9 |
Correct |
38 ms |
73128 KB |
Output is correct |
10 |
Correct |
38 ms |
73008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
73016 KB |
Output is correct |
2 |
Correct |
44 ms |
72972 KB |
Output is correct |
3 |
Correct |
39 ms |
73060 KB |
Output is correct |
4 |
Correct |
39 ms |
73052 KB |
Output is correct |
5 |
Correct |
40 ms |
73076 KB |
Output is correct |
6 |
Correct |
38 ms |
73072 KB |
Output is correct |
7 |
Correct |
37 ms |
73092 KB |
Output is correct |
8 |
Correct |
41 ms |
72996 KB |
Output is correct |
9 |
Correct |
38 ms |
73128 KB |
Output is correct |
10 |
Correct |
38 ms |
73008 KB |
Output is correct |
11 |
Correct |
44 ms |
73204 KB |
Output is correct |
12 |
Correct |
42 ms |
73592 KB |
Output is correct |
13 |
Correct |
42 ms |
73584 KB |
Output is correct |
14 |
Correct |
43 ms |
73704 KB |
Output is correct |
15 |
Correct |
44 ms |
73716 KB |
Output is correct |
16 |
Correct |
42 ms |
73284 KB |
Output is correct |
17 |
Correct |
42 ms |
73524 KB |
Output is correct |
18 |
Correct |
46 ms |
73168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2711 ms |
206100 KB |
Output is correct |
2 |
Correct |
2783 ms |
238164 KB |
Output is correct |
3 |
Correct |
2710 ms |
237936 KB |
Output is correct |
4 |
Correct |
2830 ms |
238088 KB |
Output is correct |
5 |
Correct |
1296 ms |
115660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
86280 KB |
Output is correct |
2 |
Correct |
196 ms |
86116 KB |
Output is correct |
3 |
Correct |
117 ms |
74004 KB |
Output is correct |
4 |
Correct |
144 ms |
74092 KB |
Output is correct |
5 |
Correct |
122 ms |
74312 KB |
Output is correct |
6 |
Correct |
128 ms |
74744 KB |
Output is correct |
7 |
Correct |
118 ms |
74864 KB |
Output is correct |
8 |
Correct |
141 ms |
76232 KB |
Output is correct |
9 |
Correct |
90 ms |
73860 KB |
Output is correct |
10 |
Correct |
112 ms |
75660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
73016 KB |
Output is correct |
2 |
Correct |
44 ms |
72972 KB |
Output is correct |
3 |
Correct |
39 ms |
73060 KB |
Output is correct |
4 |
Correct |
39 ms |
73052 KB |
Output is correct |
5 |
Correct |
40 ms |
73076 KB |
Output is correct |
6 |
Correct |
38 ms |
73072 KB |
Output is correct |
7 |
Correct |
37 ms |
73092 KB |
Output is correct |
8 |
Correct |
41 ms |
72996 KB |
Output is correct |
9 |
Correct |
38 ms |
73128 KB |
Output is correct |
10 |
Correct |
38 ms |
73008 KB |
Output is correct |
11 |
Correct |
44 ms |
73204 KB |
Output is correct |
12 |
Correct |
42 ms |
73592 KB |
Output is correct |
13 |
Correct |
42 ms |
73584 KB |
Output is correct |
14 |
Correct |
43 ms |
73704 KB |
Output is correct |
15 |
Correct |
44 ms |
73716 KB |
Output is correct |
16 |
Correct |
42 ms |
73284 KB |
Output is correct |
17 |
Correct |
42 ms |
73524 KB |
Output is correct |
18 |
Correct |
46 ms |
73168 KB |
Output is correct |
19 |
Correct |
476 ms |
100024 KB |
Output is correct |
20 |
Correct |
540 ms |
100168 KB |
Output is correct |
21 |
Correct |
371 ms |
100060 KB |
Output is correct |
22 |
Correct |
409 ms |
99976 KB |
Output is correct |
23 |
Correct |
411 ms |
99960 KB |
Output is correct |
24 |
Correct |
214 ms |
75464 KB |
Output is correct |
25 |
Correct |
195 ms |
75564 KB |
Output is correct |
26 |
Correct |
242 ms |
75584 KB |
Output is correct |
27 |
Correct |
231 ms |
75616 KB |
Output is correct |
28 |
Correct |
232 ms |
75572 KB |
Output is correct |
29 |
Correct |
241 ms |
75576 KB |
Output is correct |
30 |
Correct |
252 ms |
75480 KB |
Output is correct |
31 |
Correct |
233 ms |
75704 KB |
Output is correct |
32 |
Correct |
249 ms |
75680 KB |
Output is correct |
33 |
Correct |
232 ms |
75592 KB |
Output is correct |
34 |
Correct |
189 ms |
75472 KB |
Output is correct |
35 |
Correct |
188 ms |
75564 KB |
Output is correct |
36 |
Correct |
191 ms |
75600 KB |
Output is correct |
37 |
Correct |
185 ms |
75592 KB |
Output is correct |
38 |
Correct |
196 ms |
75596 KB |
Output is correct |
39 |
Correct |
246 ms |
83744 KB |
Output is correct |
40 |
Correct |
207 ms |
86536 KB |
Output is correct |
41 |
Correct |
205 ms |
78592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
73016 KB |
Output is correct |
2 |
Correct |
44 ms |
72972 KB |
Output is correct |
3 |
Correct |
39 ms |
73060 KB |
Output is correct |
4 |
Correct |
39 ms |
73052 KB |
Output is correct |
5 |
Correct |
40 ms |
73076 KB |
Output is correct |
6 |
Correct |
38 ms |
73072 KB |
Output is correct |
7 |
Correct |
37 ms |
73092 KB |
Output is correct |
8 |
Correct |
41 ms |
72996 KB |
Output is correct |
9 |
Correct |
38 ms |
73128 KB |
Output is correct |
10 |
Correct |
38 ms |
73008 KB |
Output is correct |
11 |
Correct |
44 ms |
73204 KB |
Output is correct |
12 |
Correct |
42 ms |
73592 KB |
Output is correct |
13 |
Correct |
42 ms |
73584 KB |
Output is correct |
14 |
Correct |
43 ms |
73704 KB |
Output is correct |
15 |
Correct |
44 ms |
73716 KB |
Output is correct |
16 |
Correct |
42 ms |
73284 KB |
Output is correct |
17 |
Correct |
42 ms |
73524 KB |
Output is correct |
18 |
Correct |
46 ms |
73168 KB |
Output is correct |
19 |
Correct |
2711 ms |
206100 KB |
Output is correct |
20 |
Correct |
2783 ms |
238164 KB |
Output is correct |
21 |
Correct |
2710 ms |
237936 KB |
Output is correct |
22 |
Correct |
2830 ms |
238088 KB |
Output is correct |
23 |
Correct |
1296 ms |
115660 KB |
Output is correct |
24 |
Correct |
214 ms |
86280 KB |
Output is correct |
25 |
Correct |
196 ms |
86116 KB |
Output is correct |
26 |
Correct |
117 ms |
74004 KB |
Output is correct |
27 |
Correct |
144 ms |
74092 KB |
Output is correct |
28 |
Correct |
122 ms |
74312 KB |
Output is correct |
29 |
Correct |
128 ms |
74744 KB |
Output is correct |
30 |
Correct |
118 ms |
74864 KB |
Output is correct |
31 |
Correct |
141 ms |
76232 KB |
Output is correct |
32 |
Correct |
90 ms |
73860 KB |
Output is correct |
33 |
Correct |
112 ms |
75660 KB |
Output is correct |
34 |
Correct |
476 ms |
100024 KB |
Output is correct |
35 |
Correct |
540 ms |
100168 KB |
Output is correct |
36 |
Correct |
371 ms |
100060 KB |
Output is correct |
37 |
Correct |
409 ms |
99976 KB |
Output is correct |
38 |
Correct |
411 ms |
99960 KB |
Output is correct |
39 |
Correct |
214 ms |
75464 KB |
Output is correct |
40 |
Correct |
195 ms |
75564 KB |
Output is correct |
41 |
Correct |
242 ms |
75584 KB |
Output is correct |
42 |
Correct |
231 ms |
75616 KB |
Output is correct |
43 |
Correct |
232 ms |
75572 KB |
Output is correct |
44 |
Correct |
241 ms |
75576 KB |
Output is correct |
45 |
Correct |
252 ms |
75480 KB |
Output is correct |
46 |
Correct |
233 ms |
75704 KB |
Output is correct |
47 |
Correct |
249 ms |
75680 KB |
Output is correct |
48 |
Correct |
232 ms |
75592 KB |
Output is correct |
49 |
Correct |
189 ms |
75472 KB |
Output is correct |
50 |
Correct |
188 ms |
75564 KB |
Output is correct |
51 |
Correct |
191 ms |
75600 KB |
Output is correct |
52 |
Correct |
185 ms |
75592 KB |
Output is correct |
53 |
Correct |
196 ms |
75596 KB |
Output is correct |
54 |
Correct |
246 ms |
83744 KB |
Output is correct |
55 |
Correct |
207 ms |
86536 KB |
Output is correct |
56 |
Correct |
205 ms |
78592 KB |
Output is correct |
57 |
Correct |
2788 ms |
238684 KB |
Output is correct |
58 |
Correct |
2690 ms |
238644 KB |
Output is correct |
59 |
Correct |
2702 ms |
238656 KB |
Output is correct |
60 |
Correct |
2840 ms |
238652 KB |
Output is correct |
61 |
Correct |
2730 ms |
238620 KB |
Output is correct |
62 |
Correct |
2648 ms |
237572 KB |
Output is correct |
63 |
Correct |
866 ms |
114672 KB |
Output is correct |
64 |
Correct |
894 ms |
114528 KB |
Output is correct |
65 |
Correct |
1127 ms |
116412 KB |
Output is correct |
66 |
Correct |
1165 ms |
115716 KB |
Output is correct |
67 |
Correct |
1106 ms |
115680 KB |
Output is correct |
68 |
Correct |
1264 ms |
115960 KB |
Output is correct |
69 |
Correct |
1235 ms |
115852 KB |
Output is correct |
70 |
Correct |
1363 ms |
115844 KB |
Output is correct |
71 |
Correct |
1323 ms |
115856 KB |
Output is correct |
72 |
Correct |
1261 ms |
115852 KB |
Output is correct |
73 |
Correct |
962 ms |
112608 KB |
Output is correct |
74 |
Correct |
963 ms |
113480 KB |
Output is correct |
75 |
Correct |
943 ms |
112496 KB |
Output is correct |
76 |
Correct |
907 ms |
112680 KB |
Output is correct |
77 |
Correct |
878 ms |
112880 KB |
Output is correct |
78 |
Correct |
1296 ms |
150960 KB |
Output is correct |
79 |
Correct |
1025 ms |
147172 KB |
Output is correct |
80 |
Correct |
1024 ms |
122808 KB |
Output is correct |