// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i=0;i<(n);++i)
#define FOO(i,a,b) for(int i=(a);i<=int(b);++i)
#define OOF(i,a,b) for(int i=(a);i>=int(b);--i)
#define AI(x) (x).begin(),(x).end()
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
template<class V>void lisan(V&v){sort(AI(v));v.erase(unique(AI(v)),v.end());}
template<class V,class T>int lspos(const V&v,T x){return lower_bound(AI(v),x)-v.begin();}
template<class...T>void RI(T&...t){(...,(cin>>t));}
template<class...T>void PL(T...t){int c=sizeof...(T);if(!c){cout<<'\n';return;}(...,(cout<<t<<(--c?' ':'\n')));}
constexpr inline ll cdiv(ll x,ll m){return x/m+(x%m?(x<0)^(m>0):0);}
constexpr inline ll mpow(ll x,ll e,ll m){ll r=1;for(x%=m;e;e/=2,x=x*x%m)if(e&1)r=r*x%m;return r;}
#ifdef OWO
#define safe cerr<<"\033[1;32m"<<__PRETTY_FUNCTION__<<" - "<<__LINE__<<" JIZZ\033[0m\n"
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}
template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
#else
#pragma GCC optimize("Ofast")
#define safe ((void)0)
#define debug(...) ((void)0)
#endif
constexpr ll inf = 1e9, INF = 4e18;
const int N = 1e6 + 225, M = 1e9 + 7;
int n, m, a[N];
struct sufMax{
int v[N];
void mod(int p, int val){
for(; p > 0; p -= p & -p) chmax(v[p], val);
}
int query(int p){
int r = 0;
for(; p < N; p += p & -p) chmax(r, v[p]);
return r;
}
} bit;
struct query{
int l, r, k, id;
query(){}
query(int l, int r, int k, int i): l(l), r(r), k(k), id(i){}
bool operator<(const query &q){ return r < q.r;}
};
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m; FOO(i,1,n) cin >> a[i];
vector<query> qs;
vector<bool> ans(m);
for(int l, r, k, i = 0; i < m; ++i){
cin >> l >> r >> k;
qs.pb(l, r, k, i);
}
sort(AI(qs));
stack<int> stk;
int i = 0;
for(auto q: qs){
auto [l, r, k, id] = q;
debug(l, r, k, id);
while(i < r){
++i;
while(!stk.empty() and a[stk.top()] <= a[i]) stk.pop();
if(!stk.empty()) bit.mod(stk.top(), a[stk.top()] + a[i]);
stk.push(i);
}
ans[id] = (bit.query(l) <= k);
}
for(auto i: ans) cout << i << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
2 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 |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 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 |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
4 ms |
748 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
4 ms |
748 KB |
Output is correct |
18 |
Correct |
4 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
765 ms |
26112 KB |
Output is correct |
2 |
Correct |
783 ms |
59192 KB |
Output is correct |
3 |
Correct |
785 ms |
59068 KB |
Output is correct |
4 |
Correct |
787 ms |
59316 KB |
Output is correct |
5 |
Correct |
752 ms |
55348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
3172 KB |
Output is correct |
2 |
Correct |
68 ms |
4964 KB |
Output is correct |
3 |
Correct |
61 ms |
4708 KB |
Output is correct |
4 |
Correct |
61 ms |
4708 KB |
Output is correct |
5 |
Correct |
67 ms |
4756 KB |
Output is correct |
6 |
Correct |
60 ms |
4580 KB |
Output is correct |
7 |
Correct |
58 ms |
4580 KB |
Output is correct |
8 |
Correct |
60 ms |
4328 KB |
Output is correct |
9 |
Correct |
47 ms |
3560 KB |
Output is correct |
10 |
Correct |
59 ms |
4328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 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 |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
4 ms |
748 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
4 ms |
748 KB |
Output is correct |
18 |
Correct |
4 ms |
748 KB |
Output is correct |
19 |
Correct |
154 ms |
12252 KB |
Output is correct |
20 |
Correct |
159 ms |
12380 KB |
Output is correct |
21 |
Correct |
152 ms |
12128 KB |
Output is correct |
22 |
Correct |
150 ms |
12124 KB |
Output is correct |
23 |
Correct |
151 ms |
12124 KB |
Output is correct |
24 |
Correct |
143 ms |
11240 KB |
Output is correct |
25 |
Correct |
147 ms |
11100 KB |
Output is correct |
26 |
Correct |
145 ms |
11356 KB |
Output is correct |
27 |
Correct |
155 ms |
11356 KB |
Output is correct |
28 |
Correct |
146 ms |
11356 KB |
Output is correct |
29 |
Correct |
145 ms |
11484 KB |
Output is correct |
30 |
Correct |
147 ms |
11604 KB |
Output is correct |
31 |
Correct |
148 ms |
11496 KB |
Output is correct |
32 |
Correct |
147 ms |
11484 KB |
Output is correct |
33 |
Correct |
145 ms |
11484 KB |
Output is correct |
34 |
Correct |
145 ms |
11228 KB |
Output is correct |
35 |
Correct |
140 ms |
11100 KB |
Output is correct |
36 |
Correct |
134 ms |
10844 KB |
Output is correct |
37 |
Correct |
134 ms |
10844 KB |
Output is correct |
38 |
Correct |
139 ms |
11120 KB |
Output is correct |
39 |
Correct |
143 ms |
10464 KB |
Output is correct |
40 |
Correct |
123 ms |
9180 KB |
Output is correct |
41 |
Correct |
132 ms |
9564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 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 |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
4 ms |
748 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
4 ms |
748 KB |
Output is correct |
18 |
Correct |
4 ms |
748 KB |
Output is correct |
19 |
Correct |
765 ms |
26112 KB |
Output is correct |
20 |
Correct |
783 ms |
59192 KB |
Output is correct |
21 |
Correct |
785 ms |
59068 KB |
Output is correct |
22 |
Correct |
787 ms |
59316 KB |
Output is correct |
23 |
Correct |
752 ms |
55348 KB |
Output is correct |
24 |
Correct |
71 ms |
3172 KB |
Output is correct |
25 |
Correct |
68 ms |
4964 KB |
Output is correct |
26 |
Correct |
61 ms |
4708 KB |
Output is correct |
27 |
Correct |
61 ms |
4708 KB |
Output is correct |
28 |
Correct |
67 ms |
4756 KB |
Output is correct |
29 |
Correct |
60 ms |
4580 KB |
Output is correct |
30 |
Correct |
58 ms |
4580 KB |
Output is correct |
31 |
Correct |
60 ms |
4328 KB |
Output is correct |
32 |
Correct |
47 ms |
3560 KB |
Output is correct |
33 |
Correct |
59 ms |
4328 KB |
Output is correct |
34 |
Correct |
154 ms |
12252 KB |
Output is correct |
35 |
Correct |
159 ms |
12380 KB |
Output is correct |
36 |
Correct |
152 ms |
12128 KB |
Output is correct |
37 |
Correct |
150 ms |
12124 KB |
Output is correct |
38 |
Correct |
151 ms |
12124 KB |
Output is correct |
39 |
Correct |
143 ms |
11240 KB |
Output is correct |
40 |
Correct |
147 ms |
11100 KB |
Output is correct |
41 |
Correct |
145 ms |
11356 KB |
Output is correct |
42 |
Correct |
155 ms |
11356 KB |
Output is correct |
43 |
Correct |
146 ms |
11356 KB |
Output is correct |
44 |
Correct |
145 ms |
11484 KB |
Output is correct |
45 |
Correct |
147 ms |
11604 KB |
Output is correct |
46 |
Correct |
148 ms |
11496 KB |
Output is correct |
47 |
Correct |
147 ms |
11484 KB |
Output is correct |
48 |
Correct |
145 ms |
11484 KB |
Output is correct |
49 |
Correct |
145 ms |
11228 KB |
Output is correct |
50 |
Correct |
140 ms |
11100 KB |
Output is correct |
51 |
Correct |
134 ms |
10844 KB |
Output is correct |
52 |
Correct |
134 ms |
10844 KB |
Output is correct |
53 |
Correct |
139 ms |
11120 KB |
Output is correct |
54 |
Correct |
143 ms |
10464 KB |
Output is correct |
55 |
Correct |
123 ms |
9180 KB |
Output is correct |
56 |
Correct |
132 ms |
9564 KB |
Output is correct |
57 |
Correct |
791 ms |
59892 KB |
Output is correct |
58 |
Correct |
788 ms |
59700 KB |
Output is correct |
59 |
Correct |
783 ms |
59616 KB |
Output is correct |
60 |
Correct |
783 ms |
59828 KB |
Output is correct |
61 |
Correct |
783 ms |
59700 KB |
Output is correct |
62 |
Correct |
778 ms |
59700 KB |
Output is correct |
63 |
Correct |
702 ms |
53888 KB |
Output is correct |
64 |
Correct |
703 ms |
53940 KB |
Output is correct |
65 |
Correct |
724 ms |
55792 KB |
Output is correct |
66 |
Correct |
727 ms |
55756 KB |
Output is correct |
67 |
Correct |
730 ms |
55732 KB |
Output is correct |
68 |
Correct |
730 ms |
55732 KB |
Output is correct |
69 |
Correct |
735 ms |
56040 KB |
Output is correct |
70 |
Correct |
731 ms |
55904 KB |
Output is correct |
71 |
Correct |
741 ms |
55932 KB |
Output is correct |
72 |
Correct |
728 ms |
55880 KB |
Output is correct |
73 |
Correct |
686 ms |
52480 KB |
Output is correct |
74 |
Correct |
707 ms |
53508 KB |
Output is correct |
75 |
Correct |
689 ms |
52448 KB |
Output is correct |
76 |
Correct |
683 ms |
52432 KB |
Output is correct |
77 |
Correct |
686 ms |
52620 KB |
Output is correct |
78 |
Correct |
694 ms |
51044 KB |
Output is correct |
79 |
Correct |
598 ms |
43180 KB |
Output is correct |
80 |
Correct |
673 ms |
46900 KB |
Output is correct |