제출 #1359197

#제출 시각아이디문제언어결과실행 시간메모리
1359197srividya_06새 집 (APIO18_new_home)C++20
100 / 100
2352 ms143416 KiB
#include <bits/stdc++.h>
#define int long long
#define REP(i,a,b) for(int i = a; i<b; i++)
#define RREP(i,a,b) for(int i = a; i>b; i--)
#define pb push_back
using namespace std;
int INF = 1e18;
struct node{
    int a, id, pos, type;
};
vector<node> qry;
vector<int> comp;
vector<int> tree;
vector<priority_queue<int,vector<int>, greater<int>>> cur, del;
vector<multiset<int>> s;
vector<int> res;
void update(int pos, int l, int r, int ind){
    if(r<pos || l>pos) return;
    if(l == r){
        while(!del[pos].empty()&&cur[pos].top() == del[pos].top()){
            cur[pos].pop();
            del[pos].pop();
        }
        tree[ind] = (cur[pos].empty())? INF:cur[pos].top();
        return;
    }
    int mid = (l+r)/2;
    update(pos,l,mid,2*ind);
    update(pos,mid+1,r,2*ind+1);
    tree[ind] = min(tree[2*ind], tree[2*ind+1]);
}
int query(int a, int b, int l, int r, int ind){
    if(r<a || l>b) return INF;
    if(l >= a && r <=b ) return tree[ind];
    int mid = (l+r)/2;
    return min(query(a, b, l, mid, 2*ind), query(a, b, mid+1, r, 2*ind+1));
}
int get(int val){
    return lower_bound(comp.begin(), comp.end(), val) - comp.begin();
}
bool check(int pos, int r){
    return pos-r<=query(get(pos+r+1),comp.size(),0, comp.size(),1);
}
int search(int pos){
    int lo = 0, hi = 1e8, ans = -1;
    while(lo <= hi){
        int mid = (lo + hi) / 2;
        if(check(pos, mid)){
            ans = mid;
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }
    }
    return ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,k,q;
    cin>>n>>k>>q;
    res.resize(q,-2);
    s.resize(k);
    int x,t,a,b;
    REP(i,0,n){
        cin>>x>>t>>a>>b; t--;
        qry.push_back({a, -2, x, t});
        qry.push_back({b+1, -1, x, t});
        comp.push_back(x);
    }
    comp.push_back(INF);
    sort(comp.begin(), comp.end());
    comp.erase(unique(comp.begin(),comp.end()),comp.end());
    int sz = comp.size(); 
    tree.resize(4*sz+1, INF);
    cur.resize(sz+1);
    del.resize(sz+1);
    REP(i,0,q){
        cin>>x>>a;
        qry.push_back({a, i, x, 0});
    }
    sort(qry.begin(), qry.end(), [](node a, node b){
        return (a.a == b.a)? a.id< b.id:a.a<b.a;
    });
    int ind = get(INF);
    REP(i,0,k){
        s[i].insert(-INF);
        s[i].insert(INF);
        cur[ind].push(-INF);
    }
    update(ind,0,sz,1);
    for(node tmp:qry){
        if(tmp.id == -2){
            s[tmp.type].insert(tmp.pos);
            auto it = s[tmp.type].find(tmp.pos);
            int l = *prev(it), r = *next(it);
            int ppos = get(*it), rpos = get(r);
            cur[ppos].push(l);
            update(ppos,0,sz,1);
            del[rpos].push(l);
            cur[rpos].push(*it);
            update(rpos,0,sz,1);
        }
        else if(tmp.id == -1){
            auto it = s[tmp.type].find(tmp.pos);
            int l = *prev(it), r = *next(it);
            int ppos = get(tmp.pos), rpos = get(r);
            del[ppos].push(l);
            update(ppos, 0, sz,1);
            del[rpos].push(tmp.pos);
            cur[rpos].push(l);
            update(rpos,0,sz, 1);
            s[tmp.type].erase(it);
        }
        else{
            int ans = search(tmp.pos);
            res[tmp.id] = ans;
        }
    }
    REP(i,0,q) if(res[i]!=-2) cout<<res[i]<<"\n";
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…