Submission #321531

#TimeUsernameProblemLanguageResultExecution timeMemory
321531ryanseeHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
17 / 100
25 ms1388 KiB
#include "bits/stdc++.h" using namespace std; #define FAST ios_base::sync_with_stdio(false); cin.tie(0); #define pb push_back #define eb emplace_back #define ins insert #define f first #define s second #define cbr cerr<<"hi\n" #define mmst(x, v) memset((x), v, sizeof ((x))) #define siz(x) ll(x.size()) #define all(x) (x).begin(), (x).end() #define lbd(x,y) (lower_bound(all(x),y)-x.begin()) #define ubd(x,y) (upper_bound(all(x),y)-x.begin()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng) inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); } using ll=long long; using ld=long double; #define FOR(i,s,e) for(ll i=s;i<=ll(e);++i) #define DEC(i,s,e) for(ll i=s;i>=ll(e);--i) using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; #define LLINF ((long long)1e18) #define INF int(1e9+1e6) #define MAXN (10006) struct node { // int s,e,m; // ll v,add,base,mx_base; // node*l,*r; // node(int S,int E){ // s=S,e=E,m=(s+e)>>1; // v=-LLINF, add=-LLINF, base=0, mx_base=0; // if(s^e)l=new node(s,m),r=new node(m+1,e); // } // void update(int x,int y,ll nval) { // value(); // if(s==x&&e==y) { // add = max(add, nval); // return; // } // if(x>m) r->update(x,y,nval); // else if(y<=m) l->update(x,y,nval); // else l->update(x,m,nval),r->update(m+1,y,nval); // v = max(l->value()+base, r->value()+base); // mx_base = max(l->mx_base, r->mx_base); // } // ll rmq(int x,int y) { // if(x>y) return -LLINF; // value(); // if(s==x&&e==y) return v + base; // if(x>m) return r->rmq(x,y); // else if(y<=m) return l->rmq(x,y); // else return max(l->rmq(x,m), r->rmq(m+1,y)); // } // void set(int x,ll nval) { // value(); // if(s==e) { // v = nval; // return; // } // if(x>m) r->set(x,nval); // else l->set(x,nval); // v=max(l->value()+base,r->value()+base); // mx_base = max(l->mx_base, r->mx_base); // } // ll value() { // v = max(v, add + mx_base * (s != e)); // if(s^e) { // l->add=max(l->add, add); // r->add=max(r->add, add); // } // add=-LLINF; // return v; // } // void set_base(int x, ll nval) { // value(); // if(s==e) { // mx_base = base = nval; // return; // } // if(x>m) r->set_base(x, nval); // else l->set_base(x, nval); // v = max(l->value()+base, r->value()+base); // mx_base = max(l->mx_base, r->mx_base); // } ll v[MAXN], base[MAXN]; node(int S,int E) { FOR(i,S,E) v[i]=-LLINF, base[i]=0; } void update(int x,int y,ll nval) { FOR(i,x,y) v[i]=max(v[i], nval); } ll rmq(int x,int y) { ll ans = -LLINF; FOR(i,x,y) ans=max(ans,v[i]+base[i]); return ans; } void set(int x,ll nval) { v[x]=nval; } void set_base(int x,ll nval) { base[x]=nval; } } *seg, *ans; ll n,m,A[MAXN]; bool can[MAXN]; vector<ll> v; vector<tuple<ll,ll,ll>> Q[MAXN]; int main() { FAST cin>>n>>m; FOR(i,1,n)cin>>A[i]; seg=new node(1, n), ans = new node(1, n); FOR(i,1,m) { ll l,r,k; cin>>l>>r>>k; Q[r].pb({l, k, i}); } auto add=[&](ll x){ while(v.size() && A[v.back()] <= A[x]) { ans->set(v.back(), seg->rmq(siz(v),siz(v))); seg->set(siz(v), -LLINF); v.pop_back(); } v.eb(x); seg->set_base(siz(v), A[x]); }; auto upd=[&](ll x){ ll st=-1, en=siz(v); while(en-st>1){// highest ind that is still > x ll mid=(st+en)>>1; if(A[v[mid]] > x) st=mid; else en=mid; } if(st<0) return; seg->update(1, st+1, x); }; FOR(r,1,n) { upd(A[r]); add(r); for(auto q:Q[r]) { ll l=get<0>(q); ll ind = lbd(v, l) + 1; ll mx = max(ans->rmq(l, r), seg->rmq(ind, siz(v))); if(mx <= get<1>(q)) can[get<2>(q)]=1; } } FOR(i,1,m) cout<<can[i]<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...