Submission #705585

# Submission time Handle Problem Language Result Execution time Memory
705585 2023-03-04T17:20:11 Z noedit New Home (APIO18_new_home) C++17
100 / 100
3953 ms 100728 KB
#include <bits/stdc++.h>
//#include <quadmath.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#define sz(x) (int)x.size()
//#define sqr(x) x*x
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("no-stack-protector")
//#pragma GCC optimize("fast-math")
using namespace std;
//using namespace __gnu_pbds;

//#define int long long
//#define ld long double
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//typedef long long ll;

const int INF = 2e9;
const int MAX_ANS = 1e8;

int nn;

vector<int> t;

void build(int v, int tl, int tr, vector<int>& a)
{
    if (tl == tr)
    {
        t[v] = a[tl];
    }
    else
    {
        int tm = (tl + tr) >> 1;
        build(v * 2, tl, tm, a);
        build(v * 2 + 1, tm + 1, tr, a);
        t[v] = max(t[v * 2], t[v * 2 + 1]);
    }
}

void update(int v, int tl, int tr, int p, int x)
{
    if (tl == tr)
    {
        t[v] = x;
    }
    else
    {
        int tm = (tl + tr) >> 1;
        if (p <= tm)
            update(v * 2, tl, tm, p, x);
        else
            update(v * 2 + 1, tm + 1, tr, p, x);
        t[v] = max(t[v * 2], t[v * 2 + 1]);
    }
}

int get(int v, int tl, int tr, int l, int r)
{
    if (l > r || l < tl || tr < r)
        return -INF;
    if (tl == l && tr == r)
    {
        return t[v];
    }
    int tm = (tl + tr) >> 1;
    int ql = get(v * 2, tl, tm, l, min(r, tm));
    int qr = get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
    return max(ql, qr);
}

//0 - add
//1 - query
//2 - rem
struct ev
{
    int x, type, ind, pos;
    ev() {}
    ev(int x, int type, int ind, int pos) : x(x), type(type), ind(ind), pos(pos) {}
    bool operator<(const ev& o)
    {
        if (x != o.x)
            return x < o.x;
        if (type != o.type)
            return type < o.type;
        return ind < o.ind;
    }
};

struct store
{
    int x, type, a, b;
    store() {}
    store(int x, int type, int a, int b) : x(x), type(type), a(a), b(b) {}
};

vector<set<pair<int, int> > > type_list;

vector<int> R;
vector<store> sts;
vector<int> compx;
vector<int> rev_compx;
vector<pair<int, int> > comp1;
vector<ev> evs;
vector<int> ans;
vector<pair<int, int> > qs;
vector<int> cnt;

int k;

int query(int b)
{
    int ind = upper_bound(rev_compx.begin(), rev_compx.end(), b) - rev_compx.begin() - 1;
    int cur = get(1, 0, nn - 1, 0, ind);
    if (cur == INF)
        return INF;
    return rev_compx[cur];
}

int query_easy(int b)
{
    int mx = 0;
    for (int i = 0; i < (int)R.size(); i++)
    {
        if (rev_compx[i] <= b)
        {
            mx = max(mx, R[i]);
        }
    }
    if (mx == INF)
        return INF;
    return rev_compx[mx];
}

void solve()
{
    int n, q;
    cin >> n >> k >> q;
    cnt.resize(k);
    type_list.resize(k);
    sts.resize(n);
    comp1.resize(n);
    compx.resize(n);
    rev_compx.resize(n + k);
    qs.resize(q);
    ans.resize(q);
    R.resize(n + k);
    nn = n + k;
    for (int i = 0; i < n; i++)
    {
        int x, t, a, b;
        cin >> x >> t >> a >> b;
        t--;
        compx[i] = x;
        sts[i] = {x, t, a, b};
        evs.push_back({a, 0, i, x});
        evs.push_back({b, 2, i, x});
        comp1[i] = {x, i};
    }
    for (int i = 0; i < q; i++)
    {
        int t, x;
        cin >> x >> t;
        qs[i] = {x, t};
        evs.push_back({t, 1, i, x});
    }
    sort(evs.begin(), evs.end());
    sort(comp1.begin(), comp1.end());
    for (int i = 0; i < k; i++)
    {
        rev_compx[i] = -INF;
    }
    for (int i = 0; i < n; i++)
    {
        compx[comp1[i].second] = i + k;
        rev_compx[i + k] = comp1[i].first;
    }
    for (int i = 0; i < k; i++)
    {
        type_list[i].insert({i, i});
    }
    for (int i = 0; i < n + k; i++)
    {
        R[i] = -INF;
    }
    t.resize(4 * nn);
    build(1, 0, nn - 1, R);
    int cnt_zero = k;
    for (auto&[x, type, ind, pos] : evs)
    {
        if (type == 1)
        {
            //if (ind == 0)
            //cout << "hui";
            if (cnt_zero)
            {
                ans[ind] = -1;
            }
            else
            {
                int lt = 0, rt = MAX_ANS;
                while (rt - lt > 1)
                {
                    int mid = (lt + rt) >> 1;
                    int beg = pos - mid;
                    int ed = pos + mid;
                    int mx = query(beg);
                    if (mx < ed)
                    {
                        rt = mid;
                    }
                    else
                    {
                        lt = mid;
                    }
                }
                ans[ind] = lt;
            }
        }
        else
        {
            if (type == 0)
            {
                if (cnt[sts[ind].type] == 0) cnt_zero--;
                cnt[sts[ind].type]++;
                int t = sts[ind].type;
                auto it = type_list[t].lower_bound({compx[ind], -1});
                int rt = INF, lt = -INF;
                if (it != type_list[t].end()) rt = (*it).first;
                if (it != type_list[t].begin()) lt = (*(--it)).first;
                R[compx[ind]] = rt;
                update(1, 0, nn - 1, compx[ind], rt);
                type_list[t].insert({compx[ind], ind});
                if (lt != -INF)
                {
                    R[lt] = compx[ind];
                    update(1, 0, nn - 1, lt, compx[ind]);
                }
            }
            else
            {
                if (cnt[sts[ind].type] == 1) cnt_zero++;
                cnt[sts[ind].type]--;
                int t = sts[ind].type;
                auto it = type_list[t].upper_bound({compx[ind], ind});
                int rt = INF, lt = -INF;
                if (it != type_list[t].end()) rt = (*it).first;
                it = type_list[t].lower_bound({compx[ind], ind});
                if (it != type_list[t].begin()) lt = (*(--it)).first;
                if (lt != -INF)
                {
                    R[lt] = rt;
                    update(1, 0, nn - 1, lt, rt);
                }
                R[compx[ind]] = -INF;
                update(1, 0, nn - 1, compx[ind], -INF);
                type_list[t].erase({compx[ind], ind});
            }
        }
    }
    for (int i = 0; i < q; i++)
    {
        cout << ans[i] << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    t = 1;
    while (t--)
    {
        solve();
    }
    return 0;
}
/*
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10
*/

/*
2 1 3
1 1 1 4
1 1 2 6
1 3
1 5
1 7
*/

/*
1 1 1
100000000 1 1 1
1 1
*/

/*
4 4
1 2 1 3
1 1
1 2
1 3
1 4
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 400 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 328 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 3 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 396 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 400 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 328 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 3 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 396 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 519 ms 13156 KB Output is correct
32 Correct 364 ms 9044 KB Output is correct
33 Correct 517 ms 11192 KB Output is correct
34 Correct 480 ms 11408 KB Output is correct
35 Correct 503 ms 13128 KB Output is correct
36 Correct 528 ms 12972 KB Output is correct
37 Correct 483 ms 10416 KB Output is correct
38 Correct 483 ms 10336 KB Output is correct
39 Correct 441 ms 10256 KB Output is correct
40 Correct 529 ms 10084 KB Output is correct
41 Correct 378 ms 10300 KB Output is correct
42 Correct 266 ms 10184 KB Output is correct
43 Correct 334 ms 12556 KB Output is correct
44 Correct 377 ms 10432 KB Output is correct
45 Correct 406 ms 10288 KB Output is correct
46 Correct 419 ms 10348 KB Output is correct
47 Correct 309 ms 9736 KB Output is correct
48 Correct 331 ms 9680 KB Output is correct
49 Correct 361 ms 9884 KB Output is correct
50 Correct 275 ms 10132 KB Output is correct
51 Correct 379 ms 9900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2427 ms 57408 KB Output is correct
2 Correct 3224 ms 61396 KB Output is correct
3 Correct 1801 ms 100184 KB Output is correct
4 Correct 2023 ms 75796 KB Output is correct
5 Correct 3366 ms 60940 KB Output is correct
6 Correct 3360 ms 61332 KB Output is correct
7 Correct 1582 ms 100332 KB Output is correct
8 Correct 2207 ms 75876 KB Output is correct
9 Correct 2316 ms 67168 KB Output is correct
10 Correct 2892 ms 62312 KB Output is correct
11 Correct 2391 ms 60620 KB Output is correct
12 Correct 2443 ms 62176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2670 ms 50660 KB Output is correct
2 Correct 1861 ms 52116 KB Output is correct
3 Correct 3408 ms 61216 KB Output is correct
4 Correct 1221 ms 98012 KB Output is correct
5 Correct 1216 ms 68704 KB Output is correct
6 Correct 1216 ms 73820 KB Output is correct
7 Correct 3456 ms 60776 KB Output is correct
8 Correct 3421 ms 61216 KB Output is correct
9 Correct 1563 ms 99336 KB Output is correct
10 Correct 2061 ms 72108 KB Output is correct
11 Correct 2446 ms 64812 KB Output is correct
12 Correct 3363 ms 61452 KB Output is correct
13 Correct 2428 ms 59200 KB Output is correct
14 Correct 2827 ms 58572 KB Output is correct
15 Correct 2823 ms 60392 KB Output is correct
16 Correct 2777 ms 61720 KB Output is correct
17 Correct 2742 ms 59952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 400 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 328 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 3 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 396 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 519 ms 13156 KB Output is correct
32 Correct 364 ms 9044 KB Output is correct
33 Correct 517 ms 11192 KB Output is correct
34 Correct 480 ms 11408 KB Output is correct
35 Correct 503 ms 13128 KB Output is correct
36 Correct 528 ms 12972 KB Output is correct
37 Correct 483 ms 10416 KB Output is correct
38 Correct 483 ms 10336 KB Output is correct
39 Correct 441 ms 10256 KB Output is correct
40 Correct 529 ms 10084 KB Output is correct
41 Correct 378 ms 10300 KB Output is correct
42 Correct 266 ms 10184 KB Output is correct
43 Correct 334 ms 12556 KB Output is correct
44 Correct 377 ms 10432 KB Output is correct
45 Correct 406 ms 10288 KB Output is correct
46 Correct 419 ms 10348 KB Output is correct
47 Correct 309 ms 9736 KB Output is correct
48 Correct 331 ms 9680 KB Output is correct
49 Correct 361 ms 9884 KB Output is correct
50 Correct 275 ms 10132 KB Output is correct
51 Correct 379 ms 9900 KB Output is correct
52 Correct 226 ms 20216 KB Output is correct
53 Correct 224 ms 18508 KB Output is correct
54 Correct 225 ms 15372 KB Output is correct
55 Correct 443 ms 13964 KB Output is correct
56 Correct 545 ms 15460 KB Output is correct
57 Correct 426 ms 11652 KB Output is correct
58 Correct 313 ms 13800 KB Output is correct
59 Correct 327 ms 15444 KB Output is correct
60 Correct 350 ms 11316 KB Output is correct
61 Correct 296 ms 19924 KB Output is correct
62 Correct 341 ms 20360 KB Output is correct
63 Correct 304 ms 16176 KB Output is correct
64 Correct 441 ms 14560 KB Output is correct
65 Correct 398 ms 11752 KB Output is correct
66 Correct 444 ms 10472 KB Output is correct
67 Correct 373 ms 9732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 400 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 3 ms 328 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 3 ms 340 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 396 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 328 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 519 ms 13156 KB Output is correct
32 Correct 364 ms 9044 KB Output is correct
33 Correct 517 ms 11192 KB Output is correct
34 Correct 480 ms 11408 KB Output is correct
35 Correct 503 ms 13128 KB Output is correct
36 Correct 528 ms 12972 KB Output is correct
37 Correct 483 ms 10416 KB Output is correct
38 Correct 483 ms 10336 KB Output is correct
39 Correct 441 ms 10256 KB Output is correct
40 Correct 529 ms 10084 KB Output is correct
41 Correct 378 ms 10300 KB Output is correct
42 Correct 266 ms 10184 KB Output is correct
43 Correct 334 ms 12556 KB Output is correct
44 Correct 377 ms 10432 KB Output is correct
45 Correct 406 ms 10288 KB Output is correct
46 Correct 419 ms 10348 KB Output is correct
47 Correct 309 ms 9736 KB Output is correct
48 Correct 331 ms 9680 KB Output is correct
49 Correct 361 ms 9884 KB Output is correct
50 Correct 275 ms 10132 KB Output is correct
51 Correct 379 ms 9900 KB Output is correct
52 Correct 2427 ms 57408 KB Output is correct
53 Correct 3224 ms 61396 KB Output is correct
54 Correct 1801 ms 100184 KB Output is correct
55 Correct 2023 ms 75796 KB Output is correct
56 Correct 3366 ms 60940 KB Output is correct
57 Correct 3360 ms 61332 KB Output is correct
58 Correct 1582 ms 100332 KB Output is correct
59 Correct 2207 ms 75876 KB Output is correct
60 Correct 2316 ms 67168 KB Output is correct
61 Correct 2892 ms 62312 KB Output is correct
62 Correct 2391 ms 60620 KB Output is correct
63 Correct 2443 ms 62176 KB Output is correct
64 Correct 2670 ms 50660 KB Output is correct
65 Correct 1861 ms 52116 KB Output is correct
66 Correct 3408 ms 61216 KB Output is correct
67 Correct 1221 ms 98012 KB Output is correct
68 Correct 1216 ms 68704 KB Output is correct
69 Correct 1216 ms 73820 KB Output is correct
70 Correct 3456 ms 60776 KB Output is correct
71 Correct 3421 ms 61216 KB Output is correct
72 Correct 1563 ms 99336 KB Output is correct
73 Correct 2061 ms 72108 KB Output is correct
74 Correct 2446 ms 64812 KB Output is correct
75 Correct 3363 ms 61452 KB Output is correct
76 Correct 2428 ms 59200 KB Output is correct
77 Correct 2827 ms 58572 KB Output is correct
78 Correct 2823 ms 60392 KB Output is correct
79 Correct 2777 ms 61720 KB Output is correct
80 Correct 2742 ms 59952 KB Output is correct
81 Correct 226 ms 20216 KB Output is correct
82 Correct 224 ms 18508 KB Output is correct
83 Correct 225 ms 15372 KB Output is correct
84 Correct 443 ms 13964 KB Output is correct
85 Correct 545 ms 15460 KB Output is correct
86 Correct 426 ms 11652 KB Output is correct
87 Correct 313 ms 13800 KB Output is correct
88 Correct 327 ms 15444 KB Output is correct
89 Correct 350 ms 11316 KB Output is correct
90 Correct 296 ms 19924 KB Output is correct
91 Correct 341 ms 20360 KB Output is correct
92 Correct 304 ms 16176 KB Output is correct
93 Correct 441 ms 14560 KB Output is correct
94 Correct 398 ms 11752 KB Output is correct
95 Correct 444 ms 10472 KB Output is correct
96 Correct 373 ms 9732 KB Output is correct
97 Correct 1373 ms 100184 KB Output is correct
98 Correct 2315 ms 44464 KB Output is correct
99 Correct 3869 ms 54428 KB Output is correct
100 Correct 1308 ms 91304 KB Output is correct
101 Correct 1360 ms 75724 KB Output is correct
102 Correct 3953 ms 63240 KB Output is correct
103 Correct 3024 ms 50412 KB Output is correct
104 Correct 3108 ms 50032 KB Output is correct
105 Correct 2802 ms 49076 KB Output is correct
106 Correct 2959 ms 49024 KB Output is correct
107 Correct 2915 ms 68180 KB Output is correct
108 Correct 2551 ms 76464 KB Output is correct
109 Correct 2508 ms 56388 KB Output is correct
110 Correct 1834 ms 67532 KB Output is correct
111 Correct 1914 ms 76072 KB Output is correct
112 Correct 1835 ms 55680 KB Output is correct
113 Correct 1117 ms 98880 KB Output is correct
114 Correct 1553 ms 100728 KB Output is correct
115 Correct 1791 ms 80076 KB Output is correct
116 Correct 2000 ms 71916 KB Output is correct
117 Correct 2153 ms 57904 KB Output is correct
118 Correct 2417 ms 51424 KB Output is correct
119 Correct 1729 ms 47284 KB Output is correct
120 Correct 1803 ms 45740 KB Output is correct
121 Correct 2138 ms 47668 KB Output is correct
122 Correct 2268 ms 47440 KB Output is correct
123 Correct 2114 ms 48432 KB Output is correct
124 Correct 1892 ms 49372 KB Output is correct
125 Correct 2352 ms 48772 KB Output is correct
126 Correct 1668 ms 49576 KB Output is correct