Submission #1228483

#TimeUsernameProblemLanguageResultExecution timeMemory
1228483mactoddloverNew Home (APIO18_new_home)C++17
0 / 100
1986 ms148884 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;

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> coord[N];// coordinate in type t
multiset<int> ms[N];// current max in position x

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

    void build(int l = 1, int r = m, int id = 1){
        if(l == r) return idNode[l] = id, void();
        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]);
        }
    }

    int get(int pos, int l = 1, 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(1);
    for(int i = 1; i <= n; i++){
        int x, t, l, r; cin >> x >> t >> l >> r;
        evt.push_back(node(0, x, l, t));
        evt.push_back(node(1, x, r+1, t));
        compress.push_back(x+1);
    }

    for(int i = 1; i <= q; i++){
        int x, y; cin >> x >> y;
        evt.push_back(node(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);

    //key idea to check if all k type present in this timeline:
    // since we update pos[l] = max all r
    // -> if type t is empty -> update pos[1] = limit
    for(int i = 1; i <= k; i++){
        ms[1].insert(limit);
    }

    SMT::build();
    SMT::update(1);

    auto add = [&](int l, int r) -> void{
        l = upper_bound(all(compress), l) - compress.begin(); //index + 1
        ms[l].insert(r);
        SMT::update(l);
    };

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

    auto update = [&](int act, int type, int x) -> void{
        //break coordinates of type t to segments:
        // (1 -> x1-1), (x1+1, x2-1), ...
        //cout << (act ? "open" : "close") << " " << type << "  " << x << endl;
        if(act){
            auto id = coord[type].lower_bound(x);
            int l = 1, r = limit;
            if(id != coord[type].begin()) l = *prev(id) + 1;
            if(id != coord[type].end()) r = *id - 1;
            coord[type].insert(x);
            //cout << l << " " << r << endl;
            del(l, r);
            add(l, x-1);
            add(x+1, r);
        }
        else{
            auto id = coord[type].lower_bound(x);
            int l = 1, r = limit;
            if(id != coord[type].begin()) l = *prev(id) + 1;
            if(next(id) != coord[type].end()) r = *next(id) - 1;
            coord[type].erase(id);
            del(l, x-1);
            add(x+1, r);
            add(l, r);
        }
    };

    auto query = [&](int x) -> int{
        int l = 0, r = (x, limit - x), 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();
            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){
            update(type ^ 1, id, x);
        }
        else{
            ans[id] = query(x);
        }
    }

    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:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  175 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |         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...