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<ll, ll> pp;
#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) begin(x), end(x)
#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...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
#ifndef ONLINE_JUDGEs
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#else
#define er(args ...) 0
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 6e5 + 5, lg = 22, inf = ll(2e8) + 5, p = 9973;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll,md);return k*k%md*(b&1ll?a:1)%md;}
multiset<int> seg[maxn<<2];
vector<pp> query[maxn];
multiset<int> pos[maxn];
vector<int> cmp;
int ans[maxn];
int less_id(int x){
return upper_bound(all(cmp), x) - begin(cmp) - 1;
}
void add(int l, int r, int t, int x = 1, int lx = 0, int rx = maxn){
if(l <= lx && r >= rx){
seg[x].insert(t);
return;
}
int mid = (lx + rx)>>1;
if(l < mid) add(l, r, t, x<<1, lx, mid);
if(mid < r) add(l, r, t, x<<1|1, mid, rx);
}
void rem(int l, int r, int t, int x = 1, int lx = 0, int rx = maxn){
if(l <= lx && r >= rx){
seg[x].erase(seg[x].find(t));
return;
}
int mid = (lx + rx)>>1;
if(l < mid) rem(l, r, t, x<<1, lx, mid);
if(mid < r) rem(l, r, t, x<<1|1, mid, rx);
}
pp get(int i, int x = 1, int lx = 0, int rx = maxn){
pp res = {sz(cmp)-1, 0};
if(sz(seg[x])) res.ff = *begin(seg[x]), res.ss = *prev(end(seg[x]));
if(lx + 1 == rx){
return res;
}
int mid = (lx + rx)>>1;
pp ret;
if(i < mid) ret = get(i, x<<1, lx, mid);
else ret = get(i, x<<1|1, mid, rx);
res.ff = min(res.ff, ret.ff), res.ss = max(res.ss, ret.ss);
return res;
}
void add_t(int a, int b){
int mid = less_id((cmp[a] + cmp[b])/2);
// er(a, b, mid);
if(mid >= a) add(a, mid + 1, a);
if(mid < b) add(mid + 1, b + 1, b);
}
void rem_t(int a, int b){
int mid = less_id((cmp[a] + cmp[b])/2);
// er(a, b, mid, (cmp[a]+cmp[b])/2);
if(mid >= a) rem(a, mid + 1, a);
if(mid < b) rem(mid + 1, b + 1, b);
}
void add_t(pp t){
auto[x, tp] = t; x = less_id(x);
auto it = pos[tp].upper_bound(x);
// er(x, tp, *it, *prev(it));
rem_t(*prev(it), *it);
add_t(*prev(it), x);
add_t(x, *it);
pos[tp].insert(x);
}
void rem_t(pp t){
auto[x, tp] = t; x = less_id(x);
auto it = pos[tp].lower_bound(x);
rem_t(x, *next(it));
rem_t(*prev(it), x);
add_t(*prev(it), *next(it));
pos[tp].erase(pos[tp].find(x));
}
int main(){ IOS();
int n, k, q; cin >> n >> k >> q;
map<int, pair<vector<pair<pp, bool>>, vector<pp>>> mp;
cmp = {-inf, inf};
rep(i,0,n){
int x, t, l, r; cin >> x >> t >> l >> r;
mp[l].ff.pb({{x, t}, true});
mp[r + 1].ff.pb({{x, t}, false});
cmp.pb(x);
}
rep(i,0,q){
int l, y; cin >> l >> y;
cmp.pb(l);
mp[y].ss.pb({l, i});
}
sort(all(cmp)), cmp.erase(unique(all(cmp)), end(cmp));
rep(i,1,k+1) pos[i].insert(0), pos[i].insert(sz(cmp)-1), add_t(0, sz(cmp)-1);
// for(int c: cmp) er(c);
for(auto[_, tmp]: mp){
auto[upd, query] = tmp;
// er(_);
for(auto[t, is]: upd){
if(is) add_t(t);
else rem_t(t);
}
// er(_);
for(auto[l, id]: query){
auto[mn, mx] = get(less_id(l));
// er(l, id, mn, mx);
if(!mn || mx == sz(cmp)-1) ans[id] = -1;
else ans[id] = max(cmp[mx] - l, l - cmp[mn]);
}
// er(_);
}
rep(i,0,q) cout << ans[i] << '\n';
return 0;
}
# | 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... |