#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> 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;}
priority_queue<pp> segmx[maxn<<2];
priority_queue<pp, vector<pp>, greater<pp>> segmn[maxn<<2];
set<pp> pos[maxn];
vector<int> cmp;
int ans[maxn], z;
bool ers[maxn];
int less_id(int x){
return upper_bound(all(cmp), x) - begin(cmp) - 1;
}
void add(int l, int r, pp t, int x = 1, int lx = 0, int rx = maxn){
if(l <= lx && r >= rx){
segmn[x].push(t);
segmx[x].push(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};
while(sz(segmn[x]) && ers[segmn[x].top().ss]) segmn[x].pop();
while(sz(segmx[x]) && ers[segmx[x].top().ss]) segmx[x].pop();
if(sz(segmx[x])) res = {segmn[x].top().ff, segmx[x].top().ff};
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, z});
if(mid < b) add(mid + 1, b + 1, {b, z});
}
// 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, z);
// if(mid < b) rem(mid + 1, b + 1, b, z);
// }
void add_t(pp t){
auto[x, tp] = t; x = less_id(x);
auto it = pos[tp].upper_bound(pp(x, -1));
// er(x, tp, *it, *prev(it));
// rem_t(*prev(it), *it);
ers[it->ss] = true;
add_t(prev(it)->ff, x);
pp v = *it;
pos[tp].insert({x, z++});
add_t(x, v.ff);
pos[tp].erase(v);
v.ss = z++;
pos[tp].insert(v);
}
void rem_t(pp t){
auto[x, tp] = t; x = less_id(x);
auto it = pos[tp].lower_bound(pp(x, -1));
// rem_t(x, *next(it));
// rem_t(*prev(it), x);
ers[next(it)->ss] = ers[it->ss] = true;
add_t(prev(it)->ff, next(it)->ff);
pp v = *next(it);
pos[tp].erase(it);
pos[tp].erase(v);
v.ss = z++;
pos[tp].insert(v);
}
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));
z = 1;
rep(i,1,k+1) pos[i].insert({0, 0}), pos[i].insert({sz(cmp)-1, z}), add_t(0, sz(cmp)-1), z++;
// 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, sz(cmp));
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
178764 KB |
Output is correct |
2 |
Correct |
79 ms |
178816 KB |
Output is correct |
3 |
Correct |
79 ms |
178768 KB |
Output is correct |
4 |
Correct |
78 ms |
178724 KB |
Output is correct |
5 |
Incorrect |
79 ms |
178876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
178764 KB |
Output is correct |
2 |
Correct |
79 ms |
178816 KB |
Output is correct |
3 |
Correct |
79 ms |
178768 KB |
Output is correct |
4 |
Correct |
78 ms |
178724 KB |
Output is correct |
5 |
Incorrect |
79 ms |
178876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5106 ms |
642440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5119 ms |
639856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
178764 KB |
Output is correct |
2 |
Correct |
79 ms |
178816 KB |
Output is correct |
3 |
Correct |
79 ms |
178768 KB |
Output is correct |
4 |
Correct |
78 ms |
178724 KB |
Output is correct |
5 |
Incorrect |
79 ms |
178876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
178764 KB |
Output is correct |
2 |
Correct |
79 ms |
178816 KB |
Output is correct |
3 |
Correct |
79 ms |
178768 KB |
Output is correct |
4 |
Correct |
78 ms |
178724 KB |
Output is correct |
5 |
Incorrect |
79 ms |
178876 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |