#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int N=1e6+50;
const int OFF=(1<<20);
int n, q;
int p[N], sol[N];
int T[2*OFF], levi[N];
vector <ppi> que[N];
vector <int> st;
void update(int pos, int val) {
pos+=OFF; T[pos]=max(T[pos], val);
pos/=2;
while (pos) {
T[pos]=max(T[pos*2], T[pos*2+1]);
pos/=2;
}
}
int query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return 0;
int mid=(lo+hi)/2;
return max(query(a, b, pos*2, lo, mid), query(a, b, pos*2+1, mid, hi));
}
void solve() {
for (int i=0; i<n; ++i) {
while (st.size() && p[st.back()]<=p[i]) st.pop_back();
levi[i]=st.empty() ? -1 : st.back();
st.pb(i);
}
for (int i=0; i<n; ++i) {
if (levi[i]!=-1) update(levi[i], p[i]+p[levi[i]]);
for (ppi pp:que[i]) sol[pp.Y]=query(pp.X.X, i+1)<=pp.X.Y;
}
for (int i=0; i<q; ++i) printf("%d\n", sol[i]);
}
void load() {
scanf("%d %d", &n, &q);
for (int i=0; i<n; ++i) scanf("%d", &p[i]);
for (int i=0; i<q; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c); a--; b--;
que[b].pb(mp(mp(a, c), i));
}
}
int main() {
load();
solve();
return 0;
}
Compilation message
sortbooks.cpp: In function 'void load()':
sortbooks.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
sortbooks.cpp:56:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
56 | for (int i=0; i<n; ++i) scanf("%d", &p[i]);
| ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
59 | scanf("%d %d %d", &a, &b, &c); a--; b--;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
18 ms |
23916 KB |
Output is correct |
5 |
Correct |
15 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23936 KB |
Output is correct |
7 |
Correct |
16 ms |
24044 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
18 ms |
23916 KB |
Output is correct |
5 |
Correct |
15 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23936 KB |
Output is correct |
7 |
Correct |
16 ms |
24044 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
19 ms |
24044 KB |
Output is correct |
12 |
Correct |
21 ms |
24172 KB |
Output is correct |
13 |
Correct |
20 ms |
24172 KB |
Output is correct |
14 |
Correct |
21 ms |
24300 KB |
Output is correct |
15 |
Correct |
21 ms |
24300 KB |
Output is correct |
16 |
Correct |
20 ms |
24192 KB |
Output is correct |
17 |
Correct |
19 ms |
24172 KB |
Output is correct |
18 |
Correct |
20 ms |
24172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1707 ms |
67292 KB |
Output is correct |
2 |
Correct |
1717 ms |
67452 KB |
Output is correct |
3 |
Correct |
1698 ms |
67528 KB |
Output is correct |
4 |
Correct |
1692 ms |
67308 KB |
Output is correct |
5 |
Correct |
1485 ms |
59492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
28140 KB |
Output is correct |
2 |
Correct |
128 ms |
28908 KB |
Output is correct |
3 |
Correct |
124 ms |
28012 KB |
Output is correct |
4 |
Correct |
127 ms |
28140 KB |
Output is correct |
5 |
Correct |
127 ms |
28116 KB |
Output is correct |
6 |
Correct |
106 ms |
27756 KB |
Output is correct |
7 |
Correct |
107 ms |
27756 KB |
Output is correct |
8 |
Correct |
117 ms |
28068 KB |
Output is correct |
9 |
Correct |
75 ms |
26788 KB |
Output is correct |
10 |
Correct |
117 ms |
28068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
18 ms |
23916 KB |
Output is correct |
5 |
Correct |
15 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23936 KB |
Output is correct |
7 |
Correct |
16 ms |
24044 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
19 ms |
24044 KB |
Output is correct |
12 |
Correct |
21 ms |
24172 KB |
Output is correct |
13 |
Correct |
20 ms |
24172 KB |
Output is correct |
14 |
Correct |
21 ms |
24300 KB |
Output is correct |
15 |
Correct |
21 ms |
24300 KB |
Output is correct |
16 |
Correct |
20 ms |
24192 KB |
Output is correct |
17 |
Correct |
19 ms |
24172 KB |
Output is correct |
18 |
Correct |
20 ms |
24172 KB |
Output is correct |
19 |
Correct |
296 ms |
33260 KB |
Output is correct |
20 |
Correct |
290 ms |
33260 KB |
Output is correct |
21 |
Correct |
265 ms |
32876 KB |
Output is correct |
22 |
Correct |
267 ms |
32720 KB |
Output is correct |
23 |
Correct |
267 ms |
32876 KB |
Output is correct |
24 |
Correct |
238 ms |
31084 KB |
Output is correct |
25 |
Correct |
240 ms |
31212 KB |
Output is correct |
26 |
Correct |
256 ms |
31212 KB |
Output is correct |
27 |
Correct |
257 ms |
31468 KB |
Output is correct |
28 |
Correct |
263 ms |
31340 KB |
Output is correct |
29 |
Correct |
268 ms |
31596 KB |
Output is correct |
30 |
Correct |
273 ms |
31724 KB |
Output is correct |
31 |
Correct |
273 ms |
31724 KB |
Output is correct |
32 |
Correct |
279 ms |
31596 KB |
Output is correct |
33 |
Correct |
268 ms |
31596 KB |
Output is correct |
34 |
Correct |
220 ms |
30828 KB |
Output is correct |
35 |
Correct |
217 ms |
30828 KB |
Output is correct |
36 |
Correct |
221 ms |
30828 KB |
Output is correct |
37 |
Correct |
223 ms |
30828 KB |
Output is correct |
38 |
Correct |
221 ms |
30828 KB |
Output is correct |
39 |
Correct |
242 ms |
31908 KB |
Output is correct |
40 |
Correct |
191 ms |
30496 KB |
Output is correct |
41 |
Correct |
240 ms |
31392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23916 KB |
Output is correct |
2 |
Correct |
17 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23916 KB |
Output is correct |
4 |
Correct |
18 ms |
23916 KB |
Output is correct |
5 |
Correct |
15 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23936 KB |
Output is correct |
7 |
Correct |
16 ms |
24044 KB |
Output is correct |
8 |
Correct |
15 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
16 ms |
23916 KB |
Output is correct |
11 |
Correct |
19 ms |
24044 KB |
Output is correct |
12 |
Correct |
21 ms |
24172 KB |
Output is correct |
13 |
Correct |
20 ms |
24172 KB |
Output is correct |
14 |
Correct |
21 ms |
24300 KB |
Output is correct |
15 |
Correct |
21 ms |
24300 KB |
Output is correct |
16 |
Correct |
20 ms |
24192 KB |
Output is correct |
17 |
Correct |
19 ms |
24172 KB |
Output is correct |
18 |
Correct |
20 ms |
24172 KB |
Output is correct |
19 |
Correct |
1707 ms |
67292 KB |
Output is correct |
20 |
Correct |
1717 ms |
67452 KB |
Output is correct |
21 |
Correct |
1698 ms |
67528 KB |
Output is correct |
22 |
Correct |
1692 ms |
67308 KB |
Output is correct |
23 |
Correct |
1485 ms |
59492 KB |
Output is correct |
24 |
Correct |
136 ms |
28140 KB |
Output is correct |
25 |
Correct |
128 ms |
28908 KB |
Output is correct |
26 |
Correct |
124 ms |
28012 KB |
Output is correct |
27 |
Correct |
127 ms |
28140 KB |
Output is correct |
28 |
Correct |
127 ms |
28116 KB |
Output is correct |
29 |
Correct |
106 ms |
27756 KB |
Output is correct |
30 |
Correct |
107 ms |
27756 KB |
Output is correct |
31 |
Correct |
117 ms |
28068 KB |
Output is correct |
32 |
Correct |
75 ms |
26788 KB |
Output is correct |
33 |
Correct |
117 ms |
28068 KB |
Output is correct |
34 |
Correct |
296 ms |
33260 KB |
Output is correct |
35 |
Correct |
290 ms |
33260 KB |
Output is correct |
36 |
Correct |
265 ms |
32876 KB |
Output is correct |
37 |
Correct |
267 ms |
32720 KB |
Output is correct |
38 |
Correct |
267 ms |
32876 KB |
Output is correct |
39 |
Correct |
238 ms |
31084 KB |
Output is correct |
40 |
Correct |
240 ms |
31212 KB |
Output is correct |
41 |
Correct |
256 ms |
31212 KB |
Output is correct |
42 |
Correct |
257 ms |
31468 KB |
Output is correct |
43 |
Correct |
263 ms |
31340 KB |
Output is correct |
44 |
Correct |
268 ms |
31596 KB |
Output is correct |
45 |
Correct |
273 ms |
31724 KB |
Output is correct |
46 |
Correct |
273 ms |
31724 KB |
Output is correct |
47 |
Correct |
279 ms |
31596 KB |
Output is correct |
48 |
Correct |
268 ms |
31596 KB |
Output is correct |
49 |
Correct |
220 ms |
30828 KB |
Output is correct |
50 |
Correct |
217 ms |
30828 KB |
Output is correct |
51 |
Correct |
221 ms |
30828 KB |
Output is correct |
52 |
Correct |
223 ms |
30828 KB |
Output is correct |
53 |
Correct |
221 ms |
30828 KB |
Output is correct |
54 |
Correct |
242 ms |
31908 KB |
Output is correct |
55 |
Correct |
191 ms |
30496 KB |
Output is correct |
56 |
Correct |
240 ms |
31392 KB |
Output is correct |
57 |
Correct |
1683 ms |
66668 KB |
Output is correct |
58 |
Correct |
1684 ms |
66948 KB |
Output is correct |
59 |
Correct |
1597 ms |
65388 KB |
Output is correct |
60 |
Correct |
1613 ms |
65516 KB |
Output is correct |
61 |
Correct |
1638 ms |
65444 KB |
Output is correct |
62 |
Correct |
1625 ms |
65260 KB |
Output is correct |
63 |
Correct |
1174 ms |
56044 KB |
Output is correct |
64 |
Correct |
1192 ms |
55932 KB |
Output is correct |
65 |
Correct |
1458 ms |
57372 KB |
Output is correct |
66 |
Correct |
1444 ms |
57324 KB |
Output is correct |
67 |
Correct |
1463 ms |
57376 KB |
Output is correct |
68 |
Correct |
1492 ms |
59116 KB |
Output is correct |
69 |
Correct |
1497 ms |
58900 KB |
Output is correct |
70 |
Correct |
1484 ms |
58732 KB |
Output is correct |
71 |
Correct |
1475 ms |
58732 KB |
Output is correct |
72 |
Correct |
1498 ms |
58604 KB |
Output is correct |
73 |
Correct |
1081 ms |
53868 KB |
Output is correct |
74 |
Correct |
1081 ms |
53592 KB |
Output is correct |
75 |
Correct |
1074 ms |
53740 KB |
Output is correct |
76 |
Correct |
1070 ms |
53612 KB |
Output is correct |
77 |
Correct |
1078 ms |
53612 KB |
Output is correct |
78 |
Correct |
1311 ms |
59812 KB |
Output is correct |
79 |
Correct |
963 ms |
51192 KB |
Output is correct |
80 |
Correct |
1277 ms |
57300 KB |
Output is correct |