This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int inf = 1e8+5;
const int maxn = 3e5+5;
vector<int> posq;
int n, K, q;
int X[maxn], T[maxn], A[maxn], B[maxn], L[maxn], Y[maxn], ans[maxn], td[maxn][2];
int pc[inf];
int lb(int x){return pc[x-1];}
int ub(int x){return pc[x];}
int etype[maxn];
int tot;
set<pair<int, int>> lmao[maxn];
priority_queue<pair<int, int>> st[4*maxn][2];
void _add(int l, int r, int ql, int qr, pair<int, int> k, int ty, int id){
//cout << ql << " " << qr << " " << ty << endl;
if(ql > qr)return;
if(l > qr || ql > r)return;
else if(ql <= l && r <= qr){
//cout << "a " << id << " " << ty << endl;
if(!ty)st[id][ty].push({-k.f, k.s});
else st[id][ty].push({k.f, k.s});
}
else {
int mid = (l+r)>>1;
_add(l, mid, ql, qr, k, ty, id<<1);
_add(mid+1, r, ql, qr, k, ty, id<<1|1);
}
}
int _query(int l, int r, int k, int id){
//cout << l << " " << r << " " << id << endl;
if(tot != K)return -1;
int re = 0;
while(!st[id][0].empty()){
auto x = st[id][0].top();
if(td[x.s][0] == -1)st[id][0].pop();
else break;
}
while(!st[id][1].empty()){
auto x = st[id][1].top();
if(td[x.s][1] != -1)st[id][1].pop();
else break;
}
if(!st[id][0].empty())re = max(re, posq[k] + st[id][0].top().f);
if(!st[id][1].empty())re = max(re, st[id][1].top().f - posq[k]);
if(l == r)return re;
int mid = (l+r)>>1;
if(k <= mid)re = max(re, _query(l, mid, k, id<<1));
else re = max(re, _query(mid+1, r, k, id<<1|1));
return re;
}
int query(int id){
return _query(0, posq.size()-1, lb(L[id]), 1);
}
void add(int ql, int qr, pair<int, int> k, int ty){
_add(0, posq.size()-1, lb(ql), ub(qr)-1, k, ty, 1);
}
void s_add(set<pair<int, int>>::iterator it, int ty){
int id = it->s;
if(ty == 0){
if(next(it) == lmao[T[id]].end())add(X[id]+1, inf-1, {X[id], id}, 0);
else add(X[id]+1, (next(it)->f+X[id])/2, {X[id], id}, 0);
}
else{
if(it == lmao[T[id]].begin())add(1, X[id], {X[id], id}, 1);
else add((prev(it)->f + X[id])/2+1, X[id], {X[id], id}, 1);
}
}
void s_rmv(set<pair<int, int>>::iterator it, int ty){
int id = it->s;
td[id][ty] = -1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> K >> q;
vector<pair<int, pair<int, int>>> ev;
for(int i = 0; i < n; i++){
cin >> X[i] >> T[i] >> A[i] >> B[i];
ev.push_back({A[i], {0, i}});
ev.push_back({B[i]+1, {1, i}});
}
for(int i = 0; i < q; i++){
cin >> L[i] >> Y[i];
ev.push_back({Y[i], {2, i}});
posq.push_back(L[i]);
}
sort(posq.begin(), posq.end());
posq.erase(unique(posq.begin(), posq.end()), posq.end());
for(auto v: posq)pc[v]++;
for(int i = 1; i < inf; i++)pc[i]+=pc[i-1];
sort(ev.begin(), ev.end());
for(auto v: ev){
//cout << v.s.f << " " << v.s.s << endl;
int id = v.s.s;
if(v.s.f == 0){
auto it = lmao[T[id]].lower_bound({X[id], id});
if(it != lmao[T[id]].end())s_rmv(it, 1);
if(it != lmao[T[id]].begin())s_rmv(prev(it), 0);
it = lmao[T[id]].insert({X[id], id}).f;
s_add(it, 0);
s_add(it, 1);
if(it != lmao[T[id]].begin())s_add(prev(it), 0);
if(next(it) != lmao[T[id]].end())s_add(next(it), 1);
tot += (etype[T[id]]++)==0;
}
else if(v.s.f == 1){
auto it = lmao[T[id]].find({X[id], id});
s_rmv(it, 0);
s_rmv(it, 1);
if(it != lmao[T[id]].begin())s_rmv(prev(it), 0);
if(next(it) != lmao[T[id]].end())s_rmv(next(it), 1);
lmao[T[id]].erase(it);
it = lmao[T[id]].lower_bound({X[id], id});
if(it != lmao[T[id]].end())s_add(it, 1);
if(it != lmao[T[id]].begin())s_add(prev(it), 0);
tot -= (--etype[T[id]])==0;
}
else ans[id] = query(id);
}
for(int i = 0; i < q; i++)cout << ans[i] << "\n";
}
# | 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... |