Submission #916031

# Submission time Handle Problem Language Result Execution time Memory
916031 2024-01-25T07:16:23 Z GrindMachine New Home (APIO18_new_home) C++17
100 / 100
4354 ms 240776 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

/*

sweep through time, answer queries offline
for a given query, do b.s
when mid is fixed, we get a range [l,r]
find if all types [1..k] are present in [l,r]

key idea:
at a given time, lets say for a given type of shop t, the active positions are p[1],p[2],...,p[m] 
then if [l,r] lies in any of the segs [1,p[1]-1],[p[1]+1,p[2]-1],[p[2]+1,p[3]-1],...,[p[m]+1,MAX(=1e8)], then [l,r] is bad
otherwise, [l,r] is good

checking this efficiently can be done with a range max segtree 
all ranges can be maintained efficiently with a multiset

*/

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 {
        int 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)
{
    int n,k,q; cin >> n >> k >> q;
    vector<array<int,4>> a(n+5);
    rep1(i,n) rep(j,4) cin >> a[i][j];
    vector<int> b;

    map<int,vector<array<int,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){
        int x,y; cin >> x >> y;
        mp[y].pb({3,id,x});
        b.pb(x);        
    }

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

    multiset<int> pos[k+5];
    multiset<int> ms[siz];
    segtree<int> 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 = [&](int op, int l, int 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 = [&](int t, int x){
        x = lower_bound(all(b),x)-b.begin();
        pos[t].insert(x);
        auto it = pos[t].find(x);
        int 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 = [&](int t, int x){
        x = lower_bound(all(b),x)-b.begin();
        auto it = pos[t].find(x);
        int 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<int> 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{
                int id = typ;
                int lo = 0, hi = 1e8;
                while(lo <= hi){
                    int mid = (lo+hi) >> 1;
                    int ind = lower_bound(all(b),x-mid)-b.begin();
                    int 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 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 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 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 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 600 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 2 ms 600 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 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 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 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 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 600 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 2 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 628 ms 36916 KB Output is correct
32 Correct 121 ms 7924 KB Output is correct
33 Correct 539 ms 33276 KB Output is correct
34 Correct 496 ms 33340 KB Output is correct
35 Correct 602 ms 36844 KB Output is correct
36 Correct 567 ms 36836 KB Output is correct
37 Correct 410 ms 31616 KB Output is correct
38 Correct 439 ms 31616 KB Output is correct
39 Correct 401 ms 31236 KB Output is correct
40 Correct 384 ms 31184 KB Output is correct
41 Correct 342 ms 31540 KB Output is correct
42 Correct 308 ms 31324 KB Output is correct
43 Correct 81 ms 13444 KB Output is correct
44 Correct 323 ms 31436 KB Output is correct
45 Correct 383 ms 31472 KB Output is correct
46 Correct 351 ms 31448 KB Output is correct
47 Correct 248 ms 30168 KB Output is correct
48 Correct 254 ms 29968 KB Output is correct
49 Correct 319 ms 30396 KB Output is correct
50 Correct 293 ms 31188 KB Output is correct
51 Correct 328 ms 30284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2296 ms 139812 KB Output is correct
2 Correct 3016 ms 129236 KB Output is correct
3 Correct 2464 ms 184720 KB Output is correct
4 Correct 2470 ms 150304 KB Output is correct
5 Correct 3062 ms 125288 KB Output is correct
6 Correct 3218 ms 125704 KB Output is correct
7 Correct 2486 ms 185508 KB Output is correct
8 Correct 1908 ms 147188 KB Output is correct
9 Correct 1797 ms 137084 KB Output is correct
10 Correct 2548 ms 127024 KB Output is correct
11 Correct 1680 ms 127820 KB Output is correct
12 Correct 1771 ms 129140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3748 ms 155932 KB Output is correct
2 Correct 608 ms 58796 KB Output is correct
3 Correct 4003 ms 152852 KB Output is correct
4 Correct 3466 ms 210656 KB Output is correct
5 Correct 3254 ms 164620 KB Output is correct
6 Correct 3389 ms 172872 KB Output is correct
7 Correct 3998 ms 153344 KB Output is correct
8 Correct 4108 ms 152956 KB Output is correct
9 Correct 3342 ms 211120 KB Output is correct
10 Correct 2887 ms 169620 KB Output is correct
11 Correct 2966 ms 156844 KB Output is correct
12 Correct 3607 ms 153408 KB Output is correct
13 Correct 1727 ms 148512 KB Output is correct
14 Correct 1692 ms 148396 KB Output is correct
15 Correct 1954 ms 152212 KB Output is correct
16 Correct 2213 ms 154360 KB Output is correct
17 Correct 2088 ms 150768 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 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 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 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 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 600 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 2 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 628 ms 36916 KB Output is correct
32 Correct 121 ms 7924 KB Output is correct
33 Correct 539 ms 33276 KB Output is correct
34 Correct 496 ms 33340 KB Output is correct
35 Correct 602 ms 36844 KB Output is correct
36 Correct 567 ms 36836 KB Output is correct
37 Correct 410 ms 31616 KB Output is correct
38 Correct 439 ms 31616 KB Output is correct
39 Correct 401 ms 31236 KB Output is correct
40 Correct 384 ms 31184 KB Output is correct
41 Correct 342 ms 31540 KB Output is correct
42 Correct 308 ms 31324 KB Output is correct
43 Correct 81 ms 13444 KB Output is correct
44 Correct 323 ms 31436 KB Output is correct
45 Correct 383 ms 31472 KB Output is correct
46 Correct 351 ms 31448 KB Output is correct
47 Correct 248 ms 30168 KB Output is correct
48 Correct 254 ms 29968 KB Output is correct
49 Correct 319 ms 30396 KB Output is correct
50 Correct 293 ms 31188 KB Output is correct
51 Correct 328 ms 30284 KB Output is correct
52 Correct 547 ms 47796 KB Output is correct
53 Correct 537 ms 44452 KB Output is correct
54 Correct 461 ms 40472 KB Output is correct
55 Correct 378 ms 37072 KB Output is correct
56 Correct 388 ms 39776 KB Output is correct
57 Correct 378 ms 33168 KB Output is correct
58 Correct 387 ms 36960 KB Output is correct
59 Correct 428 ms 39748 KB Output is correct
60 Correct 361 ms 33008 KB Output is correct
61 Correct 124 ms 24656 KB Output is correct
62 Correct 498 ms 48212 KB Output is correct
63 Correct 414 ms 41164 KB Output is correct
64 Correct 493 ms 39176 KB Output is correct
65 Correct 386 ms 34164 KB Output is correct
66 Correct 325 ms 32060 KB Output is correct
67 Correct 116 ms 8792 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 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 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 600 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 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 600 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 600 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 600 KB Output is correct
29 Correct 2 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 628 ms 36916 KB Output is correct
32 Correct 121 ms 7924 KB Output is correct
33 Correct 539 ms 33276 KB Output is correct
34 Correct 496 ms 33340 KB Output is correct
35 Correct 602 ms 36844 KB Output is correct
36 Correct 567 ms 36836 KB Output is correct
37 Correct 410 ms 31616 KB Output is correct
38 Correct 439 ms 31616 KB Output is correct
39 Correct 401 ms 31236 KB Output is correct
40 Correct 384 ms 31184 KB Output is correct
41 Correct 342 ms 31540 KB Output is correct
42 Correct 308 ms 31324 KB Output is correct
43 Correct 81 ms 13444 KB Output is correct
44 Correct 323 ms 31436 KB Output is correct
45 Correct 383 ms 31472 KB Output is correct
46 Correct 351 ms 31448 KB Output is correct
47 Correct 248 ms 30168 KB Output is correct
48 Correct 254 ms 29968 KB Output is correct
49 Correct 319 ms 30396 KB Output is correct
50 Correct 293 ms 31188 KB Output is correct
51 Correct 328 ms 30284 KB Output is correct
52 Correct 2296 ms 139812 KB Output is correct
53 Correct 3016 ms 129236 KB Output is correct
54 Correct 2464 ms 184720 KB Output is correct
55 Correct 2470 ms 150304 KB Output is correct
56 Correct 3062 ms 125288 KB Output is correct
57 Correct 3218 ms 125704 KB Output is correct
58 Correct 2486 ms 185508 KB Output is correct
59 Correct 1908 ms 147188 KB Output is correct
60 Correct 1797 ms 137084 KB Output is correct
61 Correct 2548 ms 127024 KB Output is correct
62 Correct 1680 ms 127820 KB Output is correct
63 Correct 1771 ms 129140 KB Output is correct
64 Correct 3748 ms 155932 KB Output is correct
65 Correct 608 ms 58796 KB Output is correct
66 Correct 4003 ms 152852 KB Output is correct
67 Correct 3466 ms 210656 KB Output is correct
68 Correct 3254 ms 164620 KB Output is correct
69 Correct 3389 ms 172872 KB Output is correct
70 Correct 3998 ms 153344 KB Output is correct
71 Correct 4108 ms 152956 KB Output is correct
72 Correct 3342 ms 211120 KB Output is correct
73 Correct 2887 ms 169620 KB Output is correct
74 Correct 2966 ms 156844 KB Output is correct
75 Correct 3607 ms 153408 KB Output is correct
76 Correct 1727 ms 148512 KB Output is correct
77 Correct 1692 ms 148396 KB Output is correct
78 Correct 1954 ms 152212 KB Output is correct
79 Correct 2213 ms 154360 KB Output is correct
80 Correct 2088 ms 150768 KB Output is correct
81 Correct 547 ms 47796 KB Output is correct
82 Correct 537 ms 44452 KB Output is correct
83 Correct 461 ms 40472 KB Output is correct
84 Correct 378 ms 37072 KB Output is correct
85 Correct 388 ms 39776 KB Output is correct
86 Correct 378 ms 33168 KB Output is correct
87 Correct 387 ms 36960 KB Output is correct
88 Correct 428 ms 39748 KB Output is correct
89 Correct 361 ms 33008 KB Output is correct
90 Correct 124 ms 24656 KB Output is correct
91 Correct 498 ms 48212 KB Output is correct
92 Correct 414 ms 41164 KB Output is correct
93 Correct 493 ms 39176 KB Output is correct
94 Correct 386 ms 34164 KB Output is correct
95 Correct 325 ms 32060 KB Output is correct
96 Correct 116 ms 8792 KB Output is correct
97 Correct 3708 ms 239820 KB Output is correct
98 Correct 623 ms 39140 KB Output is correct
99 Correct 4354 ms 164532 KB Output is correct
100 Correct 3609 ms 221808 KB Output is correct
101 Correct 3578 ms 202496 KB Output is correct
102 Correct 4336 ms 182456 KB Output is correct
103 Correct 2752 ms 157344 KB Output is correct
104 Correct 2715 ms 157432 KB Output is correct
105 Correct 2146 ms 154768 KB Output is correct
106 Correct 2240 ms 155600 KB Output is correct
107 Correct 2984 ms 181716 KB Output is correct
108 Correct 2960 ms 190904 KB Output is correct
109 Correct 2741 ms 164392 KB Output is correct
110 Correct 3106 ms 185036 KB Output is correct
111 Correct 2912 ms 199156 KB Output is correct
112 Correct 2470 ms 165392 KB Output is correct
113 Correct 629 ms 122696 KB Output is correct
114 Correct 3665 ms 240776 KB Output is correct
115 Correct 3574 ms 207552 KB Output is correct
116 Correct 3509 ms 192668 KB Output is correct
117 Correct 2993 ms 168480 KB Output is correct
118 Correct 2452 ms 156964 KB Output is correct
119 Correct 754 ms 42172 KB Output is correct
120 Correct 1267 ms 145336 KB Output is correct
121 Correct 1529 ms 150368 KB Output is correct
122 Correct 1570 ms 150024 KB Output is correct
123 Correct 1655 ms 152364 KB Output is correct
124 Correct 1711 ms 155280 KB Output is correct
125 Correct 1881 ms 152740 KB Output is correct
126 Correct 1667 ms 154692 KB Output is correct