Submission #1228936

#TimeUsernameProblemLanguageResultExecution timeMemory
1228936mactoddloverNew Home (APIO18_new_home)C++17
100 / 100
2721 ms189140 KiB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e6 + 15;
const int limit = 1e8;
const int INF = 1e9;

struct node{
    int type, x, time, id;
    node() {}
    node(int type, int x, int time, int id) : type(type), x(x), time(time), id(id) {}
};

int n, k, q, m;
vector<node> evt;
vector<int> compress;
multiset<int> ms[N];//current max for position l
multiset<int> coord[N];//lists of coordinates of type t

namespace SMT{
    int st[N << 2];
    int idNode[N];

    void build(int l = 0, int r = m, int id = 1){

        if(l == r) return void(idNode[l] = id);
        int mid = (l+r) >> 1;
        build(l, mid, id<<1);
        build(mid+1, r, id<<1|1);
    }

    void update(int pos){
        int id = idNode[pos];
        st[id] = (ms[pos].empty()) ? 0 : *ms[pos].rbegin();
        while(id >>= 1){
            st[id] = max(st[id<<1], st[id<<1|1]);
        }
    }

    //max from 0 -> pos
    int get(int pos, int l = 0, int r = m, int id = 1){
        if(l == r) return st[id];
        int mid = (l+r) >> 1;
        if(pos <= mid) return get(pos, l, mid, id<<1);
        return max(st[id<<1], get(pos, mid+1, r, id<<1|1));
    }
}

int ans[N];

void solve(){
    cin >> n >> k >> q;

    compress.push_back(-INF); compress.push_back(INF);
    for(int i = 1; i <= n; i++){
        int x, t, l, r; cin >> x >> t >> l >> r;
        evt.emplace_back(0, x, l, t);
        evt.emplace_back(1, x, r+1, t);
        compress.push_back(x);
    }

    for(int i = 1; i <= q; i++){
        int x, y; cin >> x >> y;
        evt.emplace_back(2, x, y, i);
    }

    sort(all(compress)); compact(compress);
    sort(all(evt), [&](node a, node b){
        return a.time == b.time ? a.type < b.type : a.time < b.time;
    });

    m = sz(compress);

    for(int i = 1; i <= k; i++){
        coord[i].insert(-INF); coord[i].insert(INF);
        ms[0].insert(INF);
    }

    //for(int x : compress) cout << x << " ";
    //cout << endl;

    SMT::build();
    SMT::update(0);

    auto add = [&](int l, int r) -> void{
        //cout << "add: " << l << " " << r << "   ";
        l = lower_bound(all(compress), l) - compress.begin();
        ms[l].insert(r);
        //cout << l << " " << r << endl;
        SMT::update(l);
    };

    auto del = [&](int l, int r) -> void{
        //cout << "del: " << l << " " << r << "   ";
        l = lower_bound(all(compress), l) - compress.begin();
        //cout << l << " " << r << endl;
        ms[l].erase(ms[l].find(r));
        SMT::update(l);
    };

    auto update = [&](bool act, int x, int type) -> void{
        //cout << (act ? "open" : "close") << " " << type << "  " << x << endl;
        if(act){
            coord[type].insert(x);
            auto id = coord[type].lower_bound(x);
            int l = -INF, r = INF;
            if(id != coord[type].begin()) l = *prev(id);
            if(next(id) != coord[type].end()) r = *next(id);
            del(l, r);
            add(l, x);
            add(x, r);
        }
        else{
            auto id = coord[type].lower_bound(x);
            int l = -INF, r = INF;
            if(id != coord[type].begin()) l = *prev(id);
            if(next(id) != coord[type].end()) r = *next(id);
            coord[type].erase(id);
            del(l, x);
            del(x, r);
            add(l, r);
        }
    };

    auto query = [&](int x) -> int{
        int l = 0, r = limit, res = -1;
        while(l <= r){
            int mid = (l+r) >> 1;
            int boundL = max(1, x - mid), boundR = min(limit, x + mid);
            boundL = lower_bound(all(compress), boundL) - compress.begin() - 1;
            if(SMT::get(boundL) <= boundR){
                res = mid;
                r = mid -1;
            }
            else l = mid + 1;
        }
        return res;
    };

    for(auto [type, x, time, id] : evt){
        if(type < 2){
            //cout << "TIME: " << time << endl;
            update(type ^ 1, x, id);
        }
        else{
            //for(int i = 0; i < m; i++) cout << SMT::get(i) << " ";
            //cout << endl;
            ans[id] = query(x);
            //return;
        }
    }

    for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

new_home.cpp: In function 'int main()':
new_home.cpp:184:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  185 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...