//
// A.cpp
// torelax
//
// Created by Rakhman on 1/5/21.
//
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer {cout << "NO"; exit(0);}
#define FOR(i, start, finish, k) for(llong i = start; i <= finish; i += k)
const long long mxn = 1e6 + 110;
const long long mnn = 1e3 + 2;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long OO = 1e9 + 100;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
int n, m, b[mxn], su[mxn], mx[mxn], pos[mxn];
int Q;
vector<int> rightq[mxn];
struct query{
int l, r, x;
}q[mxn];
void divideandconquer(int L, int R){
if(L == R) return ;
int m = (L + R) / 2;
vector<int> v;
su[m + 1] = mx[m + 1] = 0;
for(int i = m; i >= L; i--) {
su[i] = max(su[i + 1], b[i]);
mx[i] = mx[i + 1];
while(v.size() > 0 && v.back() <= b[i]){
if(b[i] > v.back()) mx[i] = max(mx[i], b[i] + v.back());
v.pop_back();
}
v.push_back(b[i]);
}
int batkya = 0, mow = 0;
vector<int> t;
for(int i = m + 1; i <= R; i++){
if(t.size() == 0 || t.back() < b[i]){
t.push_back(b[i]);
}
if(batkya > b[i]) mow = max(mow, batkya + b[i]);
batkya = max(batkya, b[i]);
for(auto id : rightq[i]){
int left = q[id].l, k = q[id].x;
if(!(L <= left && left <= m)){
continue;
}
if(pos[id] == 0 || max(mow, mx[left]) > k){
pos[id] = 0;
continue;
}
int lol = lower_bound(t.begin(), t.end(), su[left]) - t.begin() - 1;
if(0 <= lol && lol < t.size()){
if(t[lol] + su[left] > k){
pos[id] = 0;
continue;
}
}
}
}
divideandconquer(L, m);
divideandconquer(m + 1, R);
}
int main() {
ios;
cin >> n >> Q;
for(int i = 1; i <= n; i++){
cin >> b[i];
}
for(int i = 1; i <= Q; i++){
cin >> q[i].l >> q[i].r >> q[i].x;
rightq[q[i].r].push_back(i);
pos[i] = 1;
}
divideandconquer(1, n);
for(int i = 1; i <= Q; i++){
cout << pos[i] << nl;
}
}
Compilation message
sortbooks.cpp: In function 'void divideandconquer(int, int)':
sortbooks.cpp:94:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | if(0 <= lol && lol < t.size()){
| ~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24064 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
22 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23916 KB |
Output is correct |
7 |
Correct |
19 ms |
23944 KB |
Output is correct |
8 |
Correct |
18 ms |
23888 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24064 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
22 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23916 KB |
Output is correct |
7 |
Correct |
19 ms |
23944 KB |
Output is correct |
8 |
Correct |
18 ms |
23888 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23912 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
20 ms |
24044 KB |
Output is correct |
13 |
Correct |
21 ms |
24044 KB |
Output is correct |
14 |
Correct |
24 ms |
24044 KB |
Output is correct |
15 |
Correct |
21 ms |
24044 KB |
Output is correct |
16 |
Correct |
20 ms |
24172 KB |
Output is correct |
17 |
Correct |
21 ms |
24172 KB |
Output is correct |
18 |
Correct |
20 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1623 ms |
71148 KB |
Output is correct |
2 |
Correct |
1622 ms |
71220 KB |
Output is correct |
3 |
Correct |
1580 ms |
71280 KB |
Output is correct |
4 |
Correct |
1593 ms |
71232 KB |
Output is correct |
5 |
Correct |
1811 ms |
77096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
28396 KB |
Output is correct |
2 |
Correct |
100 ms |
27756 KB |
Output is correct |
3 |
Correct |
134 ms |
28184 KB |
Output is correct |
4 |
Correct |
117 ms |
28396 KB |
Output is correct |
5 |
Correct |
133 ms |
28412 KB |
Output is correct |
6 |
Correct |
84 ms |
27372 KB |
Output is correct |
7 |
Correct |
95 ms |
27372 KB |
Output is correct |
8 |
Correct |
137 ms |
28272 KB |
Output is correct |
9 |
Correct |
69 ms |
26088 KB |
Output is correct |
10 |
Correct |
100 ms |
28264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24064 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
22 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23916 KB |
Output is correct |
7 |
Correct |
19 ms |
23944 KB |
Output is correct |
8 |
Correct |
18 ms |
23888 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23912 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
20 ms |
24044 KB |
Output is correct |
13 |
Correct |
21 ms |
24044 KB |
Output is correct |
14 |
Correct |
24 ms |
24044 KB |
Output is correct |
15 |
Correct |
21 ms |
24044 KB |
Output is correct |
16 |
Correct |
20 ms |
24172 KB |
Output is correct |
17 |
Correct |
21 ms |
24172 KB |
Output is correct |
18 |
Correct |
20 ms |
24044 KB |
Output is correct |
19 |
Correct |
276 ms |
32984 KB |
Output is correct |
20 |
Correct |
279 ms |
32944 KB |
Output is correct |
21 |
Correct |
218 ms |
31292 KB |
Output is correct |
22 |
Correct |
251 ms |
31292 KB |
Output is correct |
23 |
Correct |
210 ms |
31260 KB |
Output is correct |
24 |
Correct |
241 ms |
32360 KB |
Output is correct |
25 |
Correct |
218 ms |
32448 KB |
Output is correct |
26 |
Correct |
264 ms |
33124 KB |
Output is correct |
27 |
Correct |
256 ms |
33124 KB |
Output is correct |
28 |
Correct |
295 ms |
33392 KB |
Output is correct |
29 |
Correct |
353 ms |
34264 KB |
Output is correct |
30 |
Correct |
320 ms |
34280 KB |
Output is correct |
31 |
Correct |
297 ms |
34160 KB |
Output is correct |
32 |
Correct |
309 ms |
34160 KB |
Output is correct |
33 |
Correct |
337 ms |
34220 KB |
Output is correct |
34 |
Correct |
202 ms |
32100 KB |
Output is correct |
35 |
Correct |
253 ms |
32104 KB |
Output is correct |
36 |
Correct |
234 ms |
32204 KB |
Output is correct |
37 |
Correct |
198 ms |
32164 KB |
Output is correct |
38 |
Correct |
206 ms |
32156 KB |
Output is correct |
39 |
Correct |
326 ms |
33628 KB |
Output is correct |
40 |
Correct |
209 ms |
32612 KB |
Output is correct |
41 |
Correct |
223 ms |
32808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24064 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
22 ms |
23916 KB |
Output is correct |
4 |
Correct |
15 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
23916 KB |
Output is correct |
6 |
Correct |
15 ms |
23916 KB |
Output is correct |
7 |
Correct |
19 ms |
23944 KB |
Output is correct |
8 |
Correct |
18 ms |
23888 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23912 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
20 ms |
24044 KB |
Output is correct |
13 |
Correct |
21 ms |
24044 KB |
Output is correct |
14 |
Correct |
24 ms |
24044 KB |
Output is correct |
15 |
Correct |
21 ms |
24044 KB |
Output is correct |
16 |
Correct |
20 ms |
24172 KB |
Output is correct |
17 |
Correct |
21 ms |
24172 KB |
Output is correct |
18 |
Correct |
20 ms |
24044 KB |
Output is correct |
19 |
Correct |
1623 ms |
71148 KB |
Output is correct |
20 |
Correct |
1622 ms |
71220 KB |
Output is correct |
21 |
Correct |
1580 ms |
71280 KB |
Output is correct |
22 |
Correct |
1593 ms |
71232 KB |
Output is correct |
23 |
Correct |
1811 ms |
77096 KB |
Output is correct |
24 |
Correct |
112 ms |
28396 KB |
Output is correct |
25 |
Correct |
100 ms |
27756 KB |
Output is correct |
26 |
Correct |
134 ms |
28184 KB |
Output is correct |
27 |
Correct |
117 ms |
28396 KB |
Output is correct |
28 |
Correct |
133 ms |
28412 KB |
Output is correct |
29 |
Correct |
84 ms |
27372 KB |
Output is correct |
30 |
Correct |
95 ms |
27372 KB |
Output is correct |
31 |
Correct |
137 ms |
28272 KB |
Output is correct |
32 |
Correct |
69 ms |
26088 KB |
Output is correct |
33 |
Correct |
100 ms |
28264 KB |
Output is correct |
34 |
Correct |
276 ms |
32984 KB |
Output is correct |
35 |
Correct |
279 ms |
32944 KB |
Output is correct |
36 |
Correct |
218 ms |
31292 KB |
Output is correct |
37 |
Correct |
251 ms |
31292 KB |
Output is correct |
38 |
Correct |
210 ms |
31260 KB |
Output is correct |
39 |
Correct |
241 ms |
32360 KB |
Output is correct |
40 |
Correct |
218 ms |
32448 KB |
Output is correct |
41 |
Correct |
264 ms |
33124 KB |
Output is correct |
42 |
Correct |
256 ms |
33124 KB |
Output is correct |
43 |
Correct |
295 ms |
33392 KB |
Output is correct |
44 |
Correct |
353 ms |
34264 KB |
Output is correct |
45 |
Correct |
320 ms |
34280 KB |
Output is correct |
46 |
Correct |
297 ms |
34160 KB |
Output is correct |
47 |
Correct |
309 ms |
34160 KB |
Output is correct |
48 |
Correct |
337 ms |
34220 KB |
Output is correct |
49 |
Correct |
202 ms |
32100 KB |
Output is correct |
50 |
Correct |
253 ms |
32104 KB |
Output is correct |
51 |
Correct |
234 ms |
32204 KB |
Output is correct |
52 |
Correct |
198 ms |
32164 KB |
Output is correct |
53 |
Correct |
206 ms |
32156 KB |
Output is correct |
54 |
Correct |
326 ms |
33628 KB |
Output is correct |
55 |
Correct |
209 ms |
32612 KB |
Output is correct |
56 |
Correct |
223 ms |
32808 KB |
Output is correct |
57 |
Correct |
1790 ms |
103116 KB |
Output is correct |
58 |
Correct |
1622 ms |
103124 KB |
Output is correct |
59 |
Correct |
1507 ms |
98924 KB |
Output is correct |
60 |
Correct |
1558 ms |
98956 KB |
Output is correct |
61 |
Correct |
1468 ms |
98896 KB |
Output is correct |
62 |
Correct |
1490 ms |
98884 KB |
Output is correct |
63 |
Correct |
1244 ms |
97284 KB |
Output is correct |
64 |
Correct |
1188 ms |
97200 KB |
Output is correct |
65 |
Correct |
1632 ms |
104156 KB |
Output is correct |
66 |
Correct |
1757 ms |
104256 KB |
Output is correct |
67 |
Correct |
1725 ms |
104608 KB |
Output is correct |
68 |
Correct |
1791 ms |
109020 KB |
Output is correct |
69 |
Correct |
2011 ms |
108888 KB |
Output is correct |
70 |
Correct |
1697 ms |
108216 KB |
Output is correct |
71 |
Correct |
1703 ms |
108228 KB |
Output is correct |
72 |
Correct |
1679 ms |
108220 KB |
Output is correct |
73 |
Correct |
1068 ms |
95060 KB |
Output is correct |
74 |
Correct |
1074 ms |
95980 KB |
Output is correct |
75 |
Correct |
1080 ms |
95092 KB |
Output is correct |
76 |
Correct |
1068 ms |
95196 KB |
Output is correct |
77 |
Correct |
1049 ms |
94948 KB |
Output is correct |
78 |
Correct |
1377 ms |
99500 KB |
Output is correct |
79 |
Correct |
1053 ms |
86492 KB |
Output is correct |
80 |
Correct |
1277 ms |
92824 KB |
Output is correct |