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>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*n)
#define rc (2*n+1)
using namespace std;
ll fp(ll x , ll y)
{
    if(y==0)
        return 1;
    ll z = fp(x,y/2);
    if(y&1)
        return z*z%mod*x%mod;
    else
        return z*z%mod;
}
int sqrot(ll x)
{
    int l = 0 , r = INT_MAX;
    while(l<=r)
    {
        if(mid*mid>=x)
            r=mid-1;
        else
            l=mid+1;
    }
    return r+1;
}
ll cel(ll x , ll y)
{
    return x/y + bool(x%y);
}
pair<pll,pll> a[60009];
pair<pll,ll> w[60009];
ll l[60009];
ll y[60009];
multiset<pll> s1[409] , s2[409];
ll Q[60009];
int main()
{
    d3
    ll n , k , q;
    cin >> n >> k >> q;
    for(int i = 0 ; n>i ; i++)
        cin >> a[i].S.F >> a[i].S.S >> a[i].F.F >> a[i].F.S;
    sort(a,a+n);
    for(int i = 0 ; q>i ; i++)
    {
        cin >> w[i].F.S >> w[i].F.F;
        w[i].S=i;
    }
    sort(w,w+q);
    for(int i = 0 ; q>i ; i++)
    {
        l[i]=w[i].F.S;
        y[i]=w[i].F.F;
    }
    ll idx = 0;
    for(int i = 0 ; q>i ; i++)
    {
        ll t = y[i];
        ll c = l[i];
        for(;n>idx;idx++)
        {
            if(a[idx].F.F>t)
                break;
            s1[a[idx].S.S].in({a[idx].S.F,a[idx].F.S});
            s2[a[idx].S.S].in({a[idx].F.S,a[idx].S.F});
        }
        ll ans = 0;
        for(int j = 1 ; k>=j ; j++)
        {
            while(!s2[j].empty())
            {
                auto it = s2[j].begin();
                if(it->F>=t)
                    break;
                s1[j].erase(s1[j].find({it->S,it->F}));
                s2[j].erase(it);
            }
            if(s1[j].empty())
                ans=-1;
            if(ans==-1)
                continue;
            auto it = s1[j].upper_bound({c,0});
            ll ans1 = 1e10;
            if(it!=s1[j].end())
                ans1=min(ans1,it->F-c);
            if(it!=s1[j].begin())
            {
                it--;
                ans1=min(ans1,c-it->F);
            }
            ans=max(ans1,ans);
        }
        Q[w[i].S]=ans;
    }
    for(int i = 0 ; q>i ; i++)
        cout << Q[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... |