Submission #986447

#TimeUsernameProblemLanguageResultExecution timeMemory
986447huutuanNew Home (APIO18_new_home)C++14
80 / 100
5007 ms452960 KiB
#include<bits/stdc++.h> using namespace std; const int N=1e6+10, inf=1e9; vector<pair<pair<int, int>, pair<int, int>>> vq[N]; int ans[N]; vector<pair<int, int>> vpos; vector<int> vtime; struct SegmentTree1{ int n; pair<int, int> t[N*2]; void init(int _n){ n=_n; for (int i=0; i<=n*2; ++i) t[i]={-inf, -inf}; } void update(int pos, pair<int, int> val){ --pos; for (t[pos+=n]=val; pos>1; pos>>=1) t[pos>>1].first=max(t[pos].first, t[pos^1].first), t[pos>>1].second=max(t[pos].second, t[pos^1].second); } int getl(int l, int r){ --l; int res=-inf; for (l+=n, r+=n; l<r; l>>=1, r>>=1){ if (l&1) res=max(res, t[l++].first); if (r&1) res=max(res, t[--r].first); } return res; } int getr(int l, int r){ --l; int res=-inf; for (l+=n, r+=n; l<r; l>>=1, r>>=1){ if (l&1) res=max(res, t[l++].second); if (r&1) res=max(res, t[--r].second); } return res; } } st1; struct SegmentTree2{ int n; vector<pair<int, pair<int, int>>> t[N*4]; void init(int _n){ n=_n; } void update(int k, int l, int r, int L, int R, pair<int, pair<int, int>> val){ if (r<L || R<l) return; if (L<=l && r<=R){ t[k].push_back(val); return; } int mid=(l+r)>>1; update(k<<1, l, mid, L, R, val); update(k<<1|1, mid+1, r, L, R, val); } void dfs(int k, int l, int r){ for (auto &i:t[k]){ #ifdef sus cout << "update l " << i.first << ' ' << i.second.first << ' ' << i.second.second << endl; cout << "update r " << i.first << ' ' << i.second.first << ' ' << i.second.second << endl; #endif st1.update(i.first, {i.second.first+i.second.second, i.second.second-i.second.first}); } if (l==r){ for (auto &i:vq[l]){ ans[i.second.first]=max(st1.getl(1, i.first.first)-i.second.second, st1.getr(i.first.second, st1.n)+i.second.second)/2; } }else{ int mid=(l+r)>>1; dfs(k<<1, l, mid); dfs(k<<1|1, mid+1, r); } for (auto &i:t[k]){ st1.update(i.first, {-inf, -inf}); } } } st2; int n, q, k; vector<pair<int, int>> v[N]; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k >> q; vtime.push_back(-inf*2); vpos.emplace_back(-inf*2, -1); vector<pair<int, pair<int, int>>> check; { map<pair<int, int>, vector<pair<int, int>>> mp; for (int i=1; i<=n; ++i){ int x, t, l, r; cin >> x >> t >> l >> r; x*=2; mp[{x, t}].emplace_back(l, r); } for (auto &i:mp){ sort(i.second.begin(), i.second.end()); vector<pair<int, int>> vv; for (auto &j:i.second){ if (vv.size() && vv.back().second+1>=j.first) vv.back().second=max(vv.back().second, j.second); else vv.push_back(j); } i.second.swap(vv); } for (auto &i:mp){ int x=i.first.first, t=i.first.second; for (auto &j:i.second){ int l=j.first, r=j.second; v[t].emplace_back(l, x); v[t].emplace_back(r+1, -x); check.push_back({l, {1, t}}); check.push_back({r+1, {-1, t}}); } } } vector<vector<int>> events; for (int i=1; i<=k; ++i){ set<int> st; map<int, int> mp; sort(v[i].begin(), v[i].end()); for (auto &j:v[i]){ int pos=abs(j.second), t=j.first; auto it=st.lower_bound(pos); if (j.second>0){ if (st.size()){ int x=0, y=0; if (it==st.begin()) x=-*it, y=*it*2; else if (it==st.end()) x=inf+*prev(it), y=inf; else x=(*it+*prev(it))>>1, y=(*it-*prev(it))>>1; events.push_back({mp[x], t-1, x, y}); mp.erase(x); } { int x=0; if (it==st.begin()) x=-pos; else x=(pos+*prev(it))>>1; mp[x]=t; } { int x=0; if (it==st.end()) x=inf+pos; else x=(*it+pos)>>1; mp[x]=t; } st.emplace_hint(it, pos); }else{ { int x=0, y=0; if (it==st.begin()) x=-pos, y=pos*2; else x=(pos+*prev(it))>>1, y=(pos-*prev(it))>>1; events.push_back({mp[x], t-1, x, y}); mp.erase(x); } { int x=0, y=0; if (next(it)==st.end()) x=inf+pos, y=inf; else x=(*next(it)+pos)>>1, y=(*next(it)-pos)>>1; events.push_back({mp[x], t-1, x, y}); mp.erase(x); } if ((int)st.size()>1){ int x=0; if (it==st.begin()) x=-*next(it); else if (next(it)==st.end()) x=inf+*prev(it); else x=(*next(it)+*prev(it))>>1; mp[x]=t; } st.erase(it); } } } for (int i=0; i<(int)events.size(); ++i){ vpos.emplace_back(events[i][2], i); } sort(vpos.begin(), vpos.end()); vector<pair<int, int>> query(q); for (auto &i:query) cin >> i.first >> i.second, vtime.push_back(i.second), i.first*=2; sort(vtime.begin(), vtime.end()); vtime.resize(unique(vtime.begin(), vtime.end())-vtime.begin()); for (int i=1; i<=q; ++i){ int pl=lower_bound(vpos.begin(), vpos.end(), make_pair(query[i-1].first, -inf))-vpos.begin(); int pr=lower_bound(vpos.begin(), vpos.end(), make_pair(query[i-1].first, inf))-vpos.begin()-1; vq[lower_bound(vtime.begin(), vtime.end(), query[i-1].second)-vtime.begin()].push_back({{pr, pl}, {i, query[i-1].first}}); check.push_back({query[i-1].second, {2, i}}); } st1.init((int)vpos.size()-1); st2.init((int)vtime.size()-1); for (int i=0; i<(int)events.size(); ++i){ events[i][0]=lower_bound(vtime.begin(), vtime.end(), events[i][0])-vtime.begin(); events[i][1]=upper_bound(vtime.begin(), vtime.end(), events[i][1])-vtime.begin()-1; st2.update(1, 1, st2.n, events[i][0], events[i][1], {lower_bound(vpos.begin(), vpos.end(), make_pair(events[i][2], i))-vpos.begin(), {events[i][2], events[i][3]}}); } st2.dfs(1, 1, st2.n); sort(check.begin(), check.end()); vector<int> cnt(k+1); int cnt0=k; for (auto &i:check){ if (abs(i.second.first)==1){ cnt0-=!cnt[i.second.second]; cnt[i.second.second]+=i.second.first; cnt0+=!cnt[i.second.second]; }else{ if (cnt0) ans[i.second.second]=-1; } } for (int i=1; i<=q; ++i) cout << ans[i] << '\n'; return 0; }
#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...