#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct SegTree{
vector<int> tr;
int n;
int join(int a, int b){
return max(a,b);
}
int neutral(){
return 0;
}
SegTree (int _n){
n = _n;
tr.assign(4*n, neutral());
}
SegTree() {}
void init(int _n){
n = _n;
tr.assign(4 * n, neutral());
}
void init(vector<int> &a){
build(a);
}
void build(vector<int> &a, int x, int l, int r){
if(l == r){
tr[x] = a[l]; return;
}
int m = (l + r) >> 1;
build(a, x * 2, l, m); build(a, x * 2 + 1, m + 1, r);
tr[x] = join(tr[x*2], tr[x*2 + 1]);
}
void upd(int x, int l, int r, int &ind, int &val){
if(l == r){
tr[x] = val; return;
}
int m = (l+r) >> 1;
if(ind <= m) upd(x * 2, l, m, ind, val);
else upd(x * 2 + 1, m + 1, r, ind, val);
tr[x] = join(tr[x*2], tr[x*2 + 1]);
}
int get(int x, int l, int r, int &kl, int &kr){
if(l > kr || r < kl) return neutral();
if(kl <= l && r <= kr) return tr[x];
int m = (l + r) >> 1;
return join(get(x * 2, l, m, kl, kr), get(x * 2 + 1, m + 1, r, kl, kr));
}
int findkth(int x, int l, int r, int k){
if(k > tr[x]){
return -1;
}
if(l == r){
return l;
}
int tm = (l + r) >> 1;
if(tr[x << 1] >= k){
return findkth(x << 1, l, tm, k);
}else{
return findkth((x << 1) + 1, tm + 1, r, k - tr[x << 1]);
}
}
// lessen
void build(vector<int> a){
n = a.size();
tr.resize(4 * n);
build(a, 1, 0, n - 1);
}
void upd(int ind, int val){
upd(1, 0, n - 1, ind, val);
}
int get(int l, int r){
return get(1, 0, n - 1, l, r);
}
int findkth(int k){
return findkth(1, 0, n - 1, k);
}
};
void solve(){
int n, q; cin >> n >> q;
vector<int> a(n);
for(auto &u : a){
cin >> u;
}
SegTree t(n);
vector<vector<tuple<int,int,int>>> qu(n);
for(int i = 0; i < q; i ++){
int l, r, k; cin >> l >> r >> k;
l --; r --;
qu[r].emplace_back(l, i, k);
}
stack<int> st;
vector<int> ans(q);
for(int i = 0; i < n; i ++){
while(!st.empty() && a[st.top()] <= a[i]){
st.pop();
}
if(!st.empty()) t.upd(st.top(), a[st.top()] + a[i]);
st.push(i);
for(auto [l, id, k] : qu[i]){
ans[id] = t.get(l, i) <= k;
}
}
for(auto u : ans) cout << u << '\n';
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
cout << '\n';
#ifdef LOCAL
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:159:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
159 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
sortbooks.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
864 KB |
Output is correct |
14 |
Correct |
3 ms |
1112 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
896 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
905 ms |
71236 KB |
Output is correct |
2 |
Correct |
927 ms |
96056 KB |
Output is correct |
3 |
Correct |
918 ms |
95828 KB |
Output is correct |
4 |
Correct |
920 ms |
96248 KB |
Output is correct |
5 |
Correct |
831 ms |
96336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
7248 KB |
Output is correct |
2 |
Correct |
56 ms |
8836 KB |
Output is correct |
3 |
Correct |
53 ms |
9012 KB |
Output is correct |
4 |
Correct |
56 ms |
9060 KB |
Output is correct |
5 |
Correct |
54 ms |
9064 KB |
Output is correct |
6 |
Correct |
45 ms |
8760 KB |
Output is correct |
7 |
Correct |
47 ms |
8780 KB |
Output is correct |
8 |
Correct |
51 ms |
8716 KB |
Output is correct |
9 |
Correct |
25 ms |
3844 KB |
Output is correct |
10 |
Correct |
45 ms |
8676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
864 KB |
Output is correct |
14 |
Correct |
3 ms |
1112 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
896 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
146 ms |
19860 KB |
Output is correct |
20 |
Correct |
143 ms |
19668 KB |
Output is correct |
21 |
Correct |
125 ms |
19280 KB |
Output is correct |
22 |
Correct |
124 ms |
19284 KB |
Output is correct |
23 |
Correct |
130 ms |
19516 KB |
Output is correct |
24 |
Correct |
105 ms |
19004 KB |
Output is correct |
25 |
Correct |
109 ms |
18932 KB |
Output is correct |
26 |
Correct |
138 ms |
19796 KB |
Output is correct |
27 |
Correct |
118 ms |
19284 KB |
Output is correct |
28 |
Correct |
119 ms |
19540 KB |
Output is correct |
29 |
Correct |
124 ms |
19788 KB |
Output is correct |
30 |
Correct |
139 ms |
20052 KB |
Output is correct |
31 |
Correct |
126 ms |
19796 KB |
Output is correct |
32 |
Correct |
126 ms |
19796 KB |
Output is correct |
33 |
Correct |
135 ms |
19992 KB |
Output is correct |
34 |
Correct |
98 ms |
18564 KB |
Output is correct |
35 |
Correct |
98 ms |
18720 KB |
Output is correct |
36 |
Correct |
103 ms |
18768 KB |
Output is correct |
37 |
Correct |
98 ms |
18256 KB |
Output is correct |
38 |
Correct |
98 ms |
18604 KB |
Output is correct |
39 |
Correct |
113 ms |
18260 KB |
Output is correct |
40 |
Correct |
92 ms |
14540 KB |
Output is correct |
41 |
Correct |
104 ms |
17888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
864 KB |
Output is correct |
14 |
Correct |
3 ms |
1112 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
896 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
905 ms |
71236 KB |
Output is correct |
20 |
Correct |
927 ms |
96056 KB |
Output is correct |
21 |
Correct |
918 ms |
95828 KB |
Output is correct |
22 |
Correct |
920 ms |
96248 KB |
Output is correct |
23 |
Correct |
831 ms |
96336 KB |
Output is correct |
24 |
Correct |
60 ms |
7248 KB |
Output is correct |
25 |
Correct |
56 ms |
8836 KB |
Output is correct |
26 |
Correct |
53 ms |
9012 KB |
Output is correct |
27 |
Correct |
56 ms |
9060 KB |
Output is correct |
28 |
Correct |
54 ms |
9064 KB |
Output is correct |
29 |
Correct |
45 ms |
8760 KB |
Output is correct |
30 |
Correct |
47 ms |
8780 KB |
Output is correct |
31 |
Correct |
51 ms |
8716 KB |
Output is correct |
32 |
Correct |
25 ms |
3844 KB |
Output is correct |
33 |
Correct |
45 ms |
8676 KB |
Output is correct |
34 |
Correct |
146 ms |
19860 KB |
Output is correct |
35 |
Correct |
143 ms |
19668 KB |
Output is correct |
36 |
Correct |
125 ms |
19280 KB |
Output is correct |
37 |
Correct |
124 ms |
19284 KB |
Output is correct |
38 |
Correct |
130 ms |
19516 KB |
Output is correct |
39 |
Correct |
105 ms |
19004 KB |
Output is correct |
40 |
Correct |
109 ms |
18932 KB |
Output is correct |
41 |
Correct |
138 ms |
19796 KB |
Output is correct |
42 |
Correct |
118 ms |
19284 KB |
Output is correct |
43 |
Correct |
119 ms |
19540 KB |
Output is correct |
44 |
Correct |
124 ms |
19788 KB |
Output is correct |
45 |
Correct |
139 ms |
20052 KB |
Output is correct |
46 |
Correct |
126 ms |
19796 KB |
Output is correct |
47 |
Correct |
126 ms |
19796 KB |
Output is correct |
48 |
Correct |
135 ms |
19992 KB |
Output is correct |
49 |
Correct |
98 ms |
18564 KB |
Output is correct |
50 |
Correct |
98 ms |
18720 KB |
Output is correct |
51 |
Correct |
103 ms |
18768 KB |
Output is correct |
52 |
Correct |
98 ms |
18256 KB |
Output is correct |
53 |
Correct |
98 ms |
18604 KB |
Output is correct |
54 |
Correct |
113 ms |
18260 KB |
Output is correct |
55 |
Correct |
92 ms |
14540 KB |
Output is correct |
56 |
Correct |
104 ms |
17888 KB |
Output is correct |
57 |
Correct |
930 ms |
95976 KB |
Output is correct |
58 |
Correct |
917 ms |
95996 KB |
Output is correct |
59 |
Correct |
811 ms |
94316 KB |
Output is correct |
60 |
Correct |
858 ms |
94284 KB |
Output is correct |
61 |
Correct |
823 ms |
94464 KB |
Output is correct |
62 |
Correct |
822 ms |
94544 KB |
Output is correct |
63 |
Correct |
576 ms |
91728 KB |
Output is correct |
64 |
Correct |
572 ms |
91732 KB |
Output is correct |
65 |
Correct |
727 ms |
94288 KB |
Output is correct |
66 |
Correct |
734 ms |
94176 KB |
Output is correct |
67 |
Correct |
749 ms |
94292 KB |
Output is correct |
68 |
Correct |
840 ms |
87636 KB |
Output is correct |
69 |
Correct |
830 ms |
87572 KB |
Output is correct |
70 |
Correct |
811 ms |
87092 KB |
Output is correct |
71 |
Correct |
816 ms |
87272 KB |
Output is correct |
72 |
Correct |
818 ms |
87124 KB |
Output is correct |
73 |
Correct |
510 ms |
80392 KB |
Output is correct |
74 |
Correct |
526 ms |
80980 KB |
Output is correct |
75 |
Correct |
530 ms |
80368 KB |
Output is correct |
76 |
Correct |
507 ms |
80724 KB |
Output is correct |
77 |
Correct |
516 ms |
80368 KB |
Output is correct |
78 |
Correct |
732 ms |
82624 KB |
Output is correct |
79 |
Correct |
509 ms |
57596 KB |
Output is correct |
80 |
Correct |
702 ms |
83352 KB |
Output is correct |