Submission #471677

#TimeUsernameProblemLanguageResultExecution timeMemory
471677jeroenodbHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
100 / 100
2592 ms112556 KiB
#include "bits/stdc++.h" using namespace std; #define all(x) begin(x),end(x) template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #define debug(a) cerr << "(" << #a << ": " << a << ")\n"; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int mxN = 1e5+1, oo = 1e9; struct segtree { int ptwo; vector<ll> seg; segtree(){} ll& operator[](int i) { return seg[i+ptwo]; } segtree(int nn) { ptwo=1; while(ptwo<nn) ptwo*=2; seg.assign(ptwo*2,0); } auto query(int l, int r) { assert(l>=0 and l<ptwo and r >=0 and r<ptwo); l+=ptwo; r+=ptwo; ll res = -oo; while(l and l<=r) { if(l%2==1) res = max(res,seg[l++]); if(r%2==0) res = max(res,seg[r--]); l/=2; r/=2; } return res; } void update(int i, ll val) { assert(i>=0 and i<ptwo); i+=ptwo; seg[i] += val; for(i/=2;i>=1;i/=2) { seg[i] = max(seg[2*i],seg[2*i+1]); } } }; int main() { int n,m; cin >> n >> m; struct query { int l,k,id; }; vector<vector<query>> queries(n); vi w(n); for(auto& i : w) cin >> i; segtree seg(n); for(int i=0;i<m;++i) { int l,r,k; cin >> l >> r >> k,--l,--r; queries[r].push_back({l,k,i}); } vi prv(n,-1), from(n,0),ans(m,-1); int active=-1; for(int i=0;i<n;++i) { prv[i]=i-1; while(prv[i]!=-1 and w[prv[i]]<=w[i]) prv[i] = prv[prv[i]]; while(active<prv[i]) { ++active; seg.update(active,w[active]); } if(prv[i]!=-1) { int& before = from[prv[i]]; if(w[i]>before) { seg.update(prv[i], w[i]-before); before=w[i]; } } for(auto& q : queries[i]) { ans[q.id]=seg.query(q.l, i)<=q.k; } } for(auto i : ans) { cout << 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...