#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
class ST {
private:
vl st;
int n;
public:
ST(int n_) {
n = n_;
st.assign(2*n,0);
}
void up(ll l, ll r, ll val) {
l += n;
r += n;
for(;l<r;l>>=1,r>>=1) {
if(l&1) {
st[l] = max(st[l],val);
l++;
}
if(r&1) {
--r;
st[r] = max(st[r],val);
}
}
}
ll get(int p) {
p += n;
ll res = 0;
while(p > 0) {
res = max(res,st[p]);
p >>= 1;
}
return res;
}
};
vl w;
struct Q {
ll idx,lf,rt,lim;
bool operator<(const Q& other) {
return lf < other.lf;
}
};
vector<vector<pl>> up;
int main() {
ios::sync_with_stdio(0);cin.tie(0);
ll n,q;
cin >> n >> q;
for(int i=0;i<n;i++) {
ll t;
cin >> t;
w.push_back(t);
}
up.assign(n,vector<pl>());
stack<pl> su;
for(int i=n-1;i>=0;i--) {
while(!su.empty() && su.top().second < w[i]) {
up[i].push_back(su.top());
su.pop();
}
su.emplace(i,w[i]);
}
vector<Q> qv;
for(int i=0;i<q;i++) {
ll lf,rt,lim;
cin >> lf >> rt >> lim;
lf--;rt--;
qv.push_back({i,lf,rt,lim});
}
sort(qv.begin(),qv.end());
vl ans(q,0);
int xv = n-1;
ST st(n);
for(int i=q-1;i>=0;i--) {
while(xv >= qv[i].lf) {
for(const auto& it: up[xv]) {
st.up(it.first,n,it.second+w[xv]);
}
xv--;
}
ans[qv[i].idx] = qv[i].lim >= st.get(qv[i].rt);
}
for(int i=0;i<q;i++) {
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
920 KB |
Output is correct |
13 |
Correct |
4 ms |
1004 KB |
Output is correct |
14 |
Correct |
5 ms |
1136 KB |
Output is correct |
15 |
Correct |
5 ms |
1132 KB |
Output is correct |
16 |
Correct |
4 ms |
1132 KB |
Output is correct |
17 |
Correct |
3 ms |
1008 KB |
Output is correct |
18 |
Correct |
4 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
986 ms |
119384 KB |
Output is correct |
2 |
Correct |
983 ms |
119564 KB |
Output is correct |
3 |
Correct |
991 ms |
119440 KB |
Output is correct |
4 |
Correct |
995 ms |
119440 KB |
Output is correct |
5 |
Correct |
830 ms |
110224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
13664 KB |
Output is correct |
2 |
Correct |
77 ms |
13668 KB |
Output is correct |
3 |
Correct |
66 ms |
12764 KB |
Output is correct |
4 |
Correct |
67 ms |
12764 KB |
Output is correct |
5 |
Correct |
65 ms |
12764 KB |
Output is correct |
6 |
Correct |
62 ms |
12636 KB |
Output is correct |
7 |
Correct |
62 ms |
12636 KB |
Output is correct |
8 |
Correct |
69 ms |
12892 KB |
Output is correct |
9 |
Correct |
43 ms |
5852 KB |
Output is correct |
10 |
Correct |
65 ms |
12572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
920 KB |
Output is correct |
13 |
Correct |
4 ms |
1004 KB |
Output is correct |
14 |
Correct |
5 ms |
1136 KB |
Output is correct |
15 |
Correct |
5 ms |
1132 KB |
Output is correct |
16 |
Correct |
4 ms |
1132 KB |
Output is correct |
17 |
Correct |
3 ms |
1008 KB |
Output is correct |
18 |
Correct |
4 ms |
1132 KB |
Output is correct |
19 |
Correct |
192 ms |
29796 KB |
Output is correct |
20 |
Correct |
201 ms |
29652 KB |
Output is correct |
21 |
Correct |
177 ms |
29520 KB |
Output is correct |
22 |
Correct |
175 ms |
29524 KB |
Output is correct |
23 |
Correct |
174 ms |
29524 KB |
Output is correct |
24 |
Correct |
157 ms |
27600 KB |
Output is correct |
25 |
Correct |
144 ms |
27588 KB |
Output is correct |
26 |
Correct |
150 ms |
27856 KB |
Output is correct |
27 |
Correct |
153 ms |
27728 KB |
Output is correct |
28 |
Correct |
152 ms |
27728 KB |
Output is correct |
29 |
Correct |
153 ms |
27856 KB |
Output is correct |
30 |
Correct |
154 ms |
27856 KB |
Output is correct |
31 |
Correct |
157 ms |
27900 KB |
Output is correct |
32 |
Correct |
155 ms |
27908 KB |
Output is correct |
33 |
Correct |
153 ms |
28032 KB |
Output is correct |
34 |
Correct |
147 ms |
27472 KB |
Output is correct |
35 |
Correct |
145 ms |
27476 KB |
Output is correct |
36 |
Correct |
144 ms |
27272 KB |
Output is correct |
37 |
Correct |
143 ms |
27476 KB |
Output is correct |
38 |
Correct |
146 ms |
27496 KB |
Output is correct |
39 |
Correct |
153 ms |
27104 KB |
Output is correct |
40 |
Correct |
132 ms |
21312 KB |
Output is correct |
41 |
Correct |
149 ms |
26224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
920 KB |
Output is correct |
13 |
Correct |
4 ms |
1004 KB |
Output is correct |
14 |
Correct |
5 ms |
1136 KB |
Output is correct |
15 |
Correct |
5 ms |
1132 KB |
Output is correct |
16 |
Correct |
4 ms |
1132 KB |
Output is correct |
17 |
Correct |
3 ms |
1008 KB |
Output is correct |
18 |
Correct |
4 ms |
1132 KB |
Output is correct |
19 |
Correct |
986 ms |
119384 KB |
Output is correct |
20 |
Correct |
983 ms |
119564 KB |
Output is correct |
21 |
Correct |
991 ms |
119440 KB |
Output is correct |
22 |
Correct |
995 ms |
119440 KB |
Output is correct |
23 |
Correct |
830 ms |
110224 KB |
Output is correct |
24 |
Correct |
79 ms |
13664 KB |
Output is correct |
25 |
Correct |
77 ms |
13668 KB |
Output is correct |
26 |
Correct |
66 ms |
12764 KB |
Output is correct |
27 |
Correct |
67 ms |
12764 KB |
Output is correct |
28 |
Correct |
65 ms |
12764 KB |
Output is correct |
29 |
Correct |
62 ms |
12636 KB |
Output is correct |
30 |
Correct |
62 ms |
12636 KB |
Output is correct |
31 |
Correct |
69 ms |
12892 KB |
Output is correct |
32 |
Correct |
43 ms |
5852 KB |
Output is correct |
33 |
Correct |
65 ms |
12572 KB |
Output is correct |
34 |
Correct |
192 ms |
29796 KB |
Output is correct |
35 |
Correct |
201 ms |
29652 KB |
Output is correct |
36 |
Correct |
177 ms |
29520 KB |
Output is correct |
37 |
Correct |
175 ms |
29524 KB |
Output is correct |
38 |
Correct |
174 ms |
29524 KB |
Output is correct |
39 |
Correct |
157 ms |
27600 KB |
Output is correct |
40 |
Correct |
144 ms |
27588 KB |
Output is correct |
41 |
Correct |
150 ms |
27856 KB |
Output is correct |
42 |
Correct |
153 ms |
27728 KB |
Output is correct |
43 |
Correct |
152 ms |
27728 KB |
Output is correct |
44 |
Correct |
153 ms |
27856 KB |
Output is correct |
45 |
Correct |
154 ms |
27856 KB |
Output is correct |
46 |
Correct |
157 ms |
27900 KB |
Output is correct |
47 |
Correct |
155 ms |
27908 KB |
Output is correct |
48 |
Correct |
153 ms |
28032 KB |
Output is correct |
49 |
Correct |
147 ms |
27472 KB |
Output is correct |
50 |
Correct |
145 ms |
27476 KB |
Output is correct |
51 |
Correct |
144 ms |
27272 KB |
Output is correct |
52 |
Correct |
143 ms |
27476 KB |
Output is correct |
53 |
Correct |
146 ms |
27496 KB |
Output is correct |
54 |
Correct |
153 ms |
27104 KB |
Output is correct |
55 |
Correct |
132 ms |
21312 KB |
Output is correct |
56 |
Correct |
149 ms |
26224 KB |
Output is correct |
57 |
Correct |
1008 ms |
119860 KB |
Output is correct |
58 |
Correct |
987 ms |
119844 KB |
Output is correct |
59 |
Correct |
949 ms |
119768 KB |
Output is correct |
60 |
Correct |
950 ms |
119844 KB |
Output is correct |
61 |
Correct |
944 ms |
119972 KB |
Output is correct |
62 |
Correct |
944 ms |
119744 KB |
Output is correct |
63 |
Correct |
719 ms |
110352 KB |
Output is correct |
64 |
Correct |
731 ms |
110240 KB |
Output is correct |
65 |
Correct |
786 ms |
110396 KB |
Output is correct |
66 |
Correct |
799 ms |
110652 KB |
Output is correct |
67 |
Correct |
796 ms |
110520 KB |
Output is correct |
68 |
Correct |
828 ms |
110496 KB |
Output is correct |
69 |
Correct |
834 ms |
110524 KB |
Output is correct |
70 |
Correct |
823 ms |
110696 KB |
Output is correct |
71 |
Correct |
814 ms |
110480 KB |
Output is correct |
72 |
Correct |
832 ms |
110528 KB |
Output is correct |
73 |
Correct |
696 ms |
110088 KB |
Output is correct |
74 |
Correct |
727 ms |
110344 KB |
Output is correct |
75 |
Correct |
695 ms |
110160 KB |
Output is correct |
76 |
Correct |
700 ms |
109996 KB |
Output is correct |
77 |
Correct |
705 ms |
110004 KB |
Output is correct |
78 |
Correct |
814 ms |
113176 KB |
Output is correct |
79 |
Correct |
630 ms |
79408 KB |
Output is correct |
80 |
Correct |
807 ms |
112988 KB |
Output is correct |