Submission #916010

# Submission time Handle Problem Language Result Execution time Memory
916010 2024-01-25T06:13:10 Z GrindMachine New Home (APIO18_new_home) C++17
100 / 100
4954 ms 255700 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct segtree {
    // https://codeforces.com/blog/entry/18051

    /*=======================================================*/

    struct data {
        ll a;
    };

    data neutral = {0};

    data merge(data &left, data &right) {
        data curr;
        curr.a = max(left.a,right.a);
        return curr;
    }

    void create(int i, T v) {

    }

    void modify(int i, T v) {
        tr[i].a = v;
    }

    /*=======================================================*/

    int n;
    vector<data> tr;

    segtree() {

    }

    segtree(int siz) {
        init(siz);
    }

    void init(int siz) {
        n = siz;
        tr.assign(2 * n, neutral);
    }

    void build(vector<T> &a, int siz) {
        rep(i, siz) create(i + n, a[i]);
        rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
    }

    void pupd(int i, T v) {
        modify(i + n, v);
        for (i = (i + n) >> 1; i; i >>= 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
    }

    data query(int l, int r) {
        data resl = neutral, resr = neutral;

        for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) resl = merge(resl, tr[l++]);
            if (!(r & 1)) resr = merge(tr[r--], resr);
        }

        return merge(resl, resr);
    }
};

void solve(int test_case)
{
    ll n,k,q; cin >> n >> k >> q;
    vector<array<ll,4>> a(n+5);
    rep1(i,n) rep(j,4) cin >> a[i][j];
    vector<ll> b;

    map<ll,vector<array<ll,3>>> mp;

    rep1(i,n){
        auto [x,t,l,r] = a[i];
        mp[l].pb({1,t,x});
        mp[r+1].pb({2,t,x});
        b.pb(x);
    }

    rep1(id,q){
        ll x,y; cin >> x >> y;
        mp[y].pb({3,id,x});
        b.pb(x);        
    }

    b.pb(-inf2), b.pb(inf2);
    sort(all(b));
    b.resize(unique(all(b))-b.begin());
    ll siz = sz(b);

    multiset<ll> pos[k+5];
    multiset<ll> ms[siz];
    segtree<ll> st(siz);

    rep1(i,k){
        pos[i].insert(0), pos[i].insert(siz-1);
        ms[0].insert(siz-1);
    }

    st.pupd(0,siz-1);

    auto upd = [&](ll op, ll l, ll r){
        if(op == 1){
            ms[l].insert(r);
            st.pupd(l,*ms[l].rbegin());
        }
        else{
            ms[l].erase(ms[l].find(r));
            if(ms[l].empty()){
                st.pupd(l,0);
            }
            else{
                st.pupd(l,*ms[l].rbegin());
            }
        }
    };

    auto add = [&](ll t, ll x){
        x = lower_bound(all(b),x)-b.begin();
        pos[t].insert(x);
        auto it = pos[t].find(x);
        ll l = -1, r = -1;
        if(it != pos[t].begin()){
            l = *prev(it);
        }
        if(next(it) != pos[t].end()){
            r = *next(it);
        }

        assert(l != -1 and r != -1);

        upd(2,l,r);
        upd(1,l,x);
        upd(1,x,r);
    };

    auto rem = [&](ll t, ll x){
        x = lower_bound(all(b),x)-b.begin();
        auto it = pos[t].find(x);
        ll l = -1, r = -1;
        if(it != pos[t].begin()){
            l = *prev(it);
        }
        if(next(it) != pos[t].end()){
            r = *next(it);
        }

        assert(l != -1 and r != -1);
        pos[t].erase(it);

        upd(2,l,x);
        upd(2,x,r);
        upd(1,l,r);
    };

    vector<ll> ans(q+5,-1);

    for(auto [ti,v] : mp){
        for(auto [t,typ,x] : v){
            if(t == 1){
                add(typ,x);
            }
            else if(t == 2){
                rem(typ,x);
            }
            else{
                ll id = typ;
                ll lo = 0, hi = 1e8;
                while(lo <= hi){
                    ll mid = (lo+hi) >> 1;
                    ll ind = lower_bound(all(b),x-mid)-b.begin();
                    ll mxr = st.query(0,ind-1).a;
                    if(b[mxr]-x <= mid){
                        ans[id] = mid;
                        hi = mid-1;
                    }
                    else{
                        lo = mid+1;
                    }
                }
            }
        }
    }

    rep1(i,q) cout << ans[i] << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 560 ms 39872 KB Output is correct
32 Correct 99 ms 12484 KB Output is correct
33 Correct 603 ms 36272 KB Output is correct
34 Correct 543 ms 36320 KB Output is correct
35 Correct 544 ms 39816 KB Output is correct
36 Correct 619 ms 39712 KB Output is correct
37 Correct 412 ms 34628 KB Output is correct
38 Correct 434 ms 34424 KB Output is correct
39 Correct 347 ms 33988 KB Output is correct
40 Correct 357 ms 33992 KB Output is correct
41 Correct 321 ms 34412 KB Output is correct
42 Correct 324 ms 34248 KB Output is correct
43 Correct 78 ms 17768 KB Output is correct
44 Correct 332 ms 34500 KB Output is correct
45 Correct 335 ms 34300 KB Output is correct
46 Correct 351 ms 34216 KB Output is correct
47 Correct 243 ms 33328 KB Output is correct
48 Correct 264 ms 32712 KB Output is correct
49 Correct 280 ms 33464 KB Output is correct
50 Correct 274 ms 33988 KB Output is correct
51 Correct 303 ms 33276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2410 ms 161248 KB Output is correct
2 Correct 3168 ms 146132 KB Output is correct
3 Correct 2536 ms 205180 KB Output is correct
4 Correct 2445 ms 170656 KB Output is correct
5 Correct 3162 ms 147996 KB Output is correct
6 Correct 3094 ms 146212 KB Output is correct
7 Correct 2193 ms 206108 KB Output is correct
8 Correct 1806 ms 170732 KB Output is correct
9 Correct 1832 ms 155456 KB Output is correct
10 Correct 2423 ms 149944 KB Output is correct
11 Correct 1642 ms 140516 KB Output is correct
12 Correct 1710 ms 141776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3419 ms 165024 KB Output is correct
2 Correct 610 ms 80388 KB Output is correct
3 Correct 4118 ms 176172 KB Output is correct
4 Correct 3176 ms 233840 KB Output is correct
5 Correct 2828 ms 190144 KB Output is correct
6 Correct 2809 ms 197428 KB Output is correct
7 Correct 4062 ms 176564 KB Output is correct
8 Correct 4062 ms 175208 KB Output is correct
9 Correct 2871 ms 233976 KB Output is correct
10 Correct 2605 ms 193012 KB Output is correct
11 Correct 2797 ms 180376 KB Output is correct
12 Correct 3513 ms 178076 KB Output is correct
13 Correct 1664 ms 173484 KB Output is correct
14 Correct 1635 ms 169420 KB Output is correct
15 Correct 1885 ms 173328 KB Output is correct
16 Correct 2066 ms 175500 KB Output is correct
17 Correct 1998 ms 173100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 560 ms 39872 KB Output is correct
32 Correct 99 ms 12484 KB Output is correct
33 Correct 603 ms 36272 KB Output is correct
34 Correct 543 ms 36320 KB Output is correct
35 Correct 544 ms 39816 KB Output is correct
36 Correct 619 ms 39712 KB Output is correct
37 Correct 412 ms 34628 KB Output is correct
38 Correct 434 ms 34424 KB Output is correct
39 Correct 347 ms 33988 KB Output is correct
40 Correct 357 ms 33992 KB Output is correct
41 Correct 321 ms 34412 KB Output is correct
42 Correct 324 ms 34248 KB Output is correct
43 Correct 78 ms 17768 KB Output is correct
44 Correct 332 ms 34500 KB Output is correct
45 Correct 335 ms 34300 KB Output is correct
46 Correct 351 ms 34216 KB Output is correct
47 Correct 243 ms 33328 KB Output is correct
48 Correct 264 ms 32712 KB Output is correct
49 Correct 280 ms 33464 KB Output is correct
50 Correct 274 ms 33988 KB Output is correct
51 Correct 303 ms 33276 KB Output is correct
52 Correct 447 ms 51120 KB Output is correct
53 Correct 432 ms 47380 KB Output is correct
54 Correct 429 ms 43656 KB Output is correct
55 Correct 338 ms 40136 KB Output is correct
56 Correct 404 ms 42976 KB Output is correct
57 Correct 318 ms 36200 KB Output is correct
58 Correct 350 ms 40044 KB Output is correct
59 Correct 344 ms 42696 KB Output is correct
60 Correct 311 ms 36080 KB Output is correct
61 Correct 110 ms 29044 KB Output is correct
62 Correct 421 ms 51400 KB Output is correct
63 Correct 384 ms 44620 KB Output is correct
64 Correct 398 ms 41696 KB Output is correct
65 Correct 353 ms 36772 KB Output is correct
66 Correct 305 ms 34696 KB Output is correct
67 Correct 116 ms 12996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 608 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 560 ms 39872 KB Output is correct
32 Correct 99 ms 12484 KB Output is correct
33 Correct 603 ms 36272 KB Output is correct
34 Correct 543 ms 36320 KB Output is correct
35 Correct 544 ms 39816 KB Output is correct
36 Correct 619 ms 39712 KB Output is correct
37 Correct 412 ms 34628 KB Output is correct
38 Correct 434 ms 34424 KB Output is correct
39 Correct 347 ms 33988 KB Output is correct
40 Correct 357 ms 33992 KB Output is correct
41 Correct 321 ms 34412 KB Output is correct
42 Correct 324 ms 34248 KB Output is correct
43 Correct 78 ms 17768 KB Output is correct
44 Correct 332 ms 34500 KB Output is correct
45 Correct 335 ms 34300 KB Output is correct
46 Correct 351 ms 34216 KB Output is correct
47 Correct 243 ms 33328 KB Output is correct
48 Correct 264 ms 32712 KB Output is correct
49 Correct 280 ms 33464 KB Output is correct
50 Correct 274 ms 33988 KB Output is correct
51 Correct 303 ms 33276 KB Output is correct
52 Correct 2410 ms 161248 KB Output is correct
53 Correct 3168 ms 146132 KB Output is correct
54 Correct 2536 ms 205180 KB Output is correct
55 Correct 2445 ms 170656 KB Output is correct
56 Correct 3162 ms 147996 KB Output is correct
57 Correct 3094 ms 146212 KB Output is correct
58 Correct 2193 ms 206108 KB Output is correct
59 Correct 1806 ms 170732 KB Output is correct
60 Correct 1832 ms 155456 KB Output is correct
61 Correct 2423 ms 149944 KB Output is correct
62 Correct 1642 ms 140516 KB Output is correct
63 Correct 1710 ms 141776 KB Output is correct
64 Correct 3419 ms 165024 KB Output is correct
65 Correct 610 ms 80388 KB Output is correct
66 Correct 4118 ms 176172 KB Output is correct
67 Correct 3176 ms 233840 KB Output is correct
68 Correct 2828 ms 190144 KB Output is correct
69 Correct 2809 ms 197428 KB Output is correct
70 Correct 4062 ms 176564 KB Output is correct
71 Correct 4062 ms 175208 KB Output is correct
72 Correct 2871 ms 233976 KB Output is correct
73 Correct 2605 ms 193012 KB Output is correct
74 Correct 2797 ms 180376 KB Output is correct
75 Correct 3513 ms 178076 KB Output is correct
76 Correct 1664 ms 173484 KB Output is correct
77 Correct 1635 ms 169420 KB Output is correct
78 Correct 1885 ms 173328 KB Output is correct
79 Correct 2066 ms 175500 KB Output is correct
80 Correct 1998 ms 173100 KB Output is correct
81 Correct 447 ms 51120 KB Output is correct
82 Correct 432 ms 47380 KB Output is correct
83 Correct 429 ms 43656 KB Output is correct
84 Correct 338 ms 40136 KB Output is correct
85 Correct 404 ms 42976 KB Output is correct
86 Correct 318 ms 36200 KB Output is correct
87 Correct 350 ms 40044 KB Output is correct
88 Correct 344 ms 42696 KB Output is correct
89 Correct 311 ms 36080 KB Output is correct
90 Correct 110 ms 29044 KB Output is correct
91 Correct 421 ms 51400 KB Output is correct
92 Correct 384 ms 44620 KB Output is correct
93 Correct 398 ms 41696 KB Output is correct
94 Correct 353 ms 36772 KB Output is correct
95 Correct 305 ms 34696 KB Output is correct
96 Correct 116 ms 12996 KB Output is correct
97 Correct 3631 ms 253996 KB Output is correct
98 Correct 624 ms 62132 KB Output is correct
99 Correct 4254 ms 179896 KB Output is correct
100 Correct 3534 ms 237964 KB Output is correct
101 Correct 3514 ms 217404 KB Output is correct
102 Correct 4954 ms 198336 KB Output is correct
103 Correct 2705 ms 171588 KB Output is correct
104 Correct 2708 ms 172492 KB Output is correct
105 Correct 2154 ms 169216 KB Output is correct
106 Correct 2208 ms 170440 KB Output is correct
107 Correct 2758 ms 197284 KB Output is correct
108 Correct 2867 ms 208260 KB Output is correct
109 Correct 2582 ms 180452 KB Output is correct
110 Correct 2939 ms 200224 KB Output is correct
111 Correct 3085 ms 213672 KB Output is correct
112 Correct 2611 ms 179092 KB Output is correct
113 Correct 659 ms 145416 KB Output is correct
114 Correct 3876 ms 255700 KB Output is correct
115 Correct 3852 ms 221444 KB Output is correct
116 Correct 3693 ms 207532 KB Output is correct
117 Correct 3381 ms 182828 KB Output is correct
118 Correct 2539 ms 171500 KB Output is correct
119 Correct 902 ms 63416 KB Output is correct
120 Correct 1250 ms 158852 KB Output is correct
121 Correct 1508 ms 166116 KB Output is correct
122 Correct 1573 ms 164188 KB Output is correct
123 Correct 1659 ms 167020 KB Output is correct
124 Correct 1670 ms 168452 KB Output is correct
125 Correct 1840 ms 167408 KB Output is correct
126 Correct 1648 ms 170416 KB Output is correct