Submission #553166

#TimeUsernameProblemLanguageResultExecution timeMemory
553166zaneyuNew Home (APIO18_new_home)C++14
100 / 100
1958 ms129776 KiB
/*input 2 1 3 1 1 1 4 1 1 2 6 1 3 1 5 1 7 */ #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; //order_of_key #of elements less than x // find_by_order kth element typedef long long int ll; #define ld long double #define pii pair<int,int> typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define f first #define s second #define pb push_back #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define FILL(n,x) memset(n,x,sizeof(n)) #define ALL(_a) _a.begin(),_a.end() #define sz(x) (int)x.size() const ll maxn=9e5+5; const ll maxlg=__lg(maxn)+2; const ll INF64=4e18; int MOD=2; const int INF=6e8; const ld PI=acos(-1); const ld eps=1e-9; #define lowb(x) x&(-x) #define MNTO(x,y) x=min(x,(__typeof__(x))y) #define MXTO(x,y) x=max(x,(__typeof__(x))y) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) template<typename T1,typename T2> ostream& operator<<(ostream& out,pair<T1,T2> P){ out<<P.f<<' '<<P.s; return out; } template<typename T> ostream& operator<<(ostream& out,vector<T> V){ REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?"\n":""); return out; } int mult(ll a,ll b){ if(a<0) a+=MOD; return (a*b)%MOD; } ll mypow(ll a,ll b){ if(b<=0) return 1; ll res=1LL; while(b){ if(b&1) res=mult(res,a); a=mult(a,a); b>>=1; } return res; } pair<pii,pii> arr[maxn]; #define get(x) lower_bound(ALL(ds),x)-ds.begin(); int cnt[maxn]; vector<pair<pii,int>> v[maxn]; int n,k,q; int seg[4*maxn]; int ans[maxn]; void upd(int idx,int l,int r,int ql,int qr,int x){ if(r<ql or l>qr) return; if(ql<=l and r<=qr){ MXTO(seg[idx],x); return; } int mid=(l+r)/2; upd(idx*2,l,mid,ql,qr,x),upd(idx*2+1,mid+1,r,ql,qr,x); } int query(int idx,int l,int r,int p){ if(r<p or l>p) return -INF; if(l==r) return seg[idx]; int mid=(l+r)/2; return max({query(idx*2,l,mid,p),query(idx*2+1,mid+1,r,p),seg[idx]}); } vector<int> ds; vector<pair<pii,int>> qq; void solve(){ REP(i,n) v[arr[i].f.s].pb({{arr[i].s.f,i},arr[i].f.f}),v[arr[i].f.s].pb({{arr[i].s.s,i},-arr[i].f.f}); vector<pair<pii,pii>> vv; REP1(i,k){ sort(ALL(v[i])); map<pii,int> s; s[{-INF,0}]=s[{INF,0}]=0; for(auto x:v[i]){ if(x.s>0){ auto b=s.lower_bound({x.s,x.f.s}); auto a=prev(b); vv.pb({{((*a).f.f+(*b).f.f)/2,(*b).f.f},{(*b).s,x.f.f}}); b->s=x.f.f; s[{x.s,x.f.s}]=x.f.f; } else{ x.s*=-1; auto it=s.lower_bound({x.s,x.f.s}); auto a=prev(it),b=next(it); vv.pb({{((*a).f.f+x.s)/2,x.s},{(*it).s,x.f.f}}); vv.pb({{(x.s+(*b).f.f)/2,(*b).f.f},{(*b).s,x.f.f}}); b->s=x.f.f; s.erase(it); } } vv.pb({{0,INF},{s[{INF,0}],sz(ds)}}); v[i].clear(); } REP(i,4*maxn) seg[i]=0; sort(ALL(vv)),sort(ALL(qq)); int p=0; for(auto x:qq){ while(p<sz(vv) and vv[p].f.f<=x.f.f){ if(vv[p].s.f<vv[p].s.s) upd(1,0,sz(ds)-1,vv[p].s.f,vv[p].s.s-1,vv[p].f.s); ++p; } MXTO(ans[x.s],query(1,0,sz(ds)-1,x.f.s)-x.f.f); } } int main(){ ios::sync_with_stdio(false),cin.tie(0); cin>>n>>k>>q; vector<pii> vv; REP(i,n){ int x,t,a,b; cin>>x>>t>>a>>b; x*=2; arr[i]={{x,t},{a,b}}; ds.pb(a),ds.pb(b+1); vv.pb({a,t}),vv.pb({b+1,-t}); } REP(i,q){ int p,y; cin>>p>>y; p*=2; qq.pb({{p,y},i}); vv.pb({y,i+n+k}); } sort(ALL(vv)); int asd=0; for(auto x:vv){ if(x.s>=n+k){ if(asd!=k) ans[x.s-n-k]=INT_MAX; } else{ if(x.s<0){ cnt[-x.s]--; if(!cnt[-x.s]) --asd; } else{ if(!cnt[x.s]) ++asd; cnt[x.s]++; } } } SORT_UNIQUE(ds); REP(i,n){ arr[i].s.f=get(arr[i].s.f); arr[i].s.s=get(arr[i].s.s+1); } for(auto &x:qq) x.f.s=upper_bound(ALL(ds),x.f.s)-ds.begin()-1; solve(); for(auto &x:qq) x.f.f=2e8+1-x.f.f; REP(i,n) arr[i].f.f=2e8+1-arr[i].f.f; solve(); REP(i,q){ if(ans[i]==INT_MAX) cout<<-1<<'\n'; else cout<<ans[i]/2<<'\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...