This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 1e6 + 5, lg = 22, lng = 26, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int seg[maxn<<2], n;
int upd(int i, int k, int x = 1, int lx = 0, int rx = n){
if(lx + 1 == rx) return seg[x] = max(seg[x], k);
int mid = (lx + rx)>>1;
if(i < mid) upd(i, k, x<<1, lx, mid);
else upd(i, k, x<<1|1, mid, rx);
return seg[x] = max(seg[x<<1], seg[x<<1|1]);
}
int get(int l, int r, int x = 1, int lx = 0, int rx = n){
if(l <= lx && r >= rx) return seg[x];
if(l >= rx || r <= lx) return -1;
int mid = (lx + rx)>>1;
return max(get(l, r, x<<1, lx, mid), get(l, r, x<<1|1, mid, rx));
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
int q; cin >> n >> q;
vector<int> w(n);
rep(i,0,n){
cin >> w[i];
}
struct tr{
int r, id, k;
};
vector<vector<tr>> query(n);
rep(i,0,q){
int l, r, k; cin >> l >> r >> k; l--;
query[l].pb({r, i, k});
}
fill(seg, seg+(n<<2), -1);
vector<int> st;
vector<bool> ans(q);
per(i,n-1,0){
while(sz(st) && w[st.back()] < w[i]){
upd(st.back(), w[st.back()] + w[i]);
st.pop_back();
}
st.pb(i);
for(auto[r, id, k]: query[i]){
ans[id] = get(i, r) <= k;
}
}
for(auto c: ans) cout << c << '\n';
return 0;
}
/*
5 2
3 5 1 8 2
1 3 6
2 5 3
*/
Compilation message (stderr)
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:59:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for(auto[r, id, k]: query[i]){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |