답안 #568390

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568390 2022-05-25T11:07:15 Z Ooops_sorry 새 집 (APIO18_new_home) C++14
57 / 100
5000 ms 736980 KB
#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define ld double
#define ll __int128

mt19937 rnd(51);

const int INF = 1e9;

struct st{
    vector<int> t_min, t_max;
    vector<multiset<int>> t;
    void build(int n) {
        t_min.clear(), t_max.clear(), t.clear();
        t_min.resize(4 * n, INF);
        t_max.resize(4 * n, -INF);
        t.resize(4 * n);
    }
    void upd(int v) {
        if (t[v].size() == 0) {
            t_min[v] = INF;
            t_max[v] = -INF;
            return;
        }
        t_min[v] = *t[v].begin();
        t_max[v] = *t[v].rbegin();
    }
    void update(int v, int l, int r, int pos, int val, int add) {
        if (l == r) {
            if (add) {
                t[v].insert(val);
            } else {
                t[v].erase(t[v].find(val));
            }
            upd(v);
            return;
        }
        int m = (l + r) / 2;
        if (pos <= m) {
            update(2 * v, l, m, pos, val, add);
        } else {
            update(2 * v + 1, m + 1, r, pos, val, add);
        }
        t_min[v] = min(t_min[v * 2], t_min[v * 2 + 1]);
        t_max[v] = max(t_max[v * 2], t_max[v * 2 + 1]);
    }
    int get_min(int v, int tl, int tr, int l, int r) {
        if (l > r) return INF;
        if (tl == l && tr == r) {
            return t_min[v];
        }
        int tm = (tl + tr) / 2;
        return min(get_min(2 * v, tl, tm, l, min(r, tm)), get_min(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
    }
    int get_max(int v, int tl, int tr, int l, int r) {
        if (l > r) return -INF;
        if (tl == l && tr == r) {
            return t_max[v];
        }
        int tm = (tl + tr) / 2;
        return max(get_max(2 * v, tl, tm, l, min(r, tm)), get_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
    }
};

vector<multiset<int>> pos;
vector<int> u2;

st L, R;
int sz;

void add(int a, int b) {
    int need = (a + b) / 2;
    int pos = upper_bound(u2.begin(), u2.end(), need) - u2.begin() - 1;
    int I = lower_bound(u2.begin(), u2.end(), a) - u2.begin(), J = lower_bound(u2.begin(), u2.end(), b) - u2.begin();
    L.update(1, 0, sz - 1, pos, a, 1);
    R.update(1, 0, sz - 1, pos + 1, b, 1);
}

void del(int a, int b) {
    int need = (a + b) / 2;
    int pos = upper_bound(u2.begin(), u2.end(), need) - u2.begin() - 1;
    int I = lower_bound(u2.begin(), u2.end(), a) - u2.begin(), J = lower_bound(u2.begin(), u2.end(), b) - u2.begin();
    L.update(1, 0, sz - 1, pos, a, 0);
    R.update(1, 0, sz - 1, pos + 1, b, 0);
}

void func(int i, int j, int need) {
    if (need) {
        if (pos[i].find(j) != pos[i].end()) {
            pos[i].insert(j);
            return;
        }
        int pre = *prev(pos[i].lower_bound(j));
        int nxt = *pos[i].upper_bound(j);
        del(pre, nxt);
        add(pre, j);
        add(j, nxt);
        pos[i].insert(j);
    } else {
        pos[i].erase(pos[i].find(j));
        if (pos[i].find(j) != pos[i].end()) return;
        int pre = *prev(pos[i].lower_bound(j));
        int nxt = *pos[i].upper_bound(j);
        del(pre, j);
        del(j, nxt);
        add(pre, nxt);
    }
}

vector<int> solve(int n, int k, int q, vector<int> x, vector<int> t, vector<int> a, vector<int> b, vector<int> l, vector<int> y) {
    u2.clear();
    u2.pb(-INF);
    u2.pb(INF);
    vector<vector<int>> ind(k);
    pos.clear();
    pos.resize(k);
    vector<int> ans(q);
    for (int i = 0; i < n; i++) {
        ind[t[i]].pb(x[i]);
        u2.pb(x[i]);
    }
    for (int i = 0; i < k; i++) {
        ind[i].pb(-INF);
        ind[i].pb(INF);
        sort(ind[i].begin(), ind[i].end());
        for (int j = 0; j + 1 < ind[i].size(); j++) {
            u2.pb({(ind[i][j] + ind[i][j + 1]) / 2});
        }
    }
    for (int i = 0; i < q; i++) {
        u2.pb(l[i]);
    }
    sort(u2.begin(), u2.end());
    u2.erase(unique(u2.begin(), u2.end()), u2.end());
    vector<vector<int>> qu;
    for (int i = 0; i < n; i++) {
        qu.pb({a[i], -1, t[i], x[i]});
        qu.pb({b[i], 1, t[i], x[i]});
    }
    for (int i = 0; i < q; i++) {
        qu.pb({y[i], 0, l[i], i});
    }
    sz = u2.size();
    bool bad = 0;
    L.build(sz);
    R.build(sz);
    for (int i = 0; i < k; i++) {
        if (ind[i].size() == 2) bad = 1;
        pos[i].insert(-INF);
        pos[i].insert(INF);
        add(-INF, INF);
    }
    sort(qu.begin(), qu.end());
    set<int> st;
    for (int i = 0; i < k; i++) st.insert(i);
    for (int i = 0; i < qu.size(); i++) {
        if (qu[i][1] == -1) {
            if (pos[qu[i][2]].size() == 2) st.erase(qu[i][2]);
            func(qu[i][2], qu[i][3], 1);
        } else if (qu[i][1] == 0) {
            int j = lower_bound(u2.begin(), u2.end(), qu[i][2]) - u2.begin();
            int mx = L.get_min(1, 0, sz - 1, j, sz - 1), mn = R.get_max(1, 0, sz - 1, 0, j);
            if (st.size() > 0) {
                ans[qu[i][3]] = -1;
            } else {
                ans[qu[i][3]] = max(qu[i][2] - mx, mn - qu[i][2]);
            }
        } else {
            func(qu[i][2], qu[i][3], 0);
            if (pos[qu[i][2]].size() == 2) st.insert(qu[i][2]);
        }
    }
    return ans;
}

vector<int> solvee(int n, int k, int q, vector<int> x, vector<int> t, vector<int> a, vector<int> b, vector<int> l, vector<int> y) {
    vector<int> ans;
    for (int i = 0; i < q; i++) {
        vector<int> bst(k, INF);
        for (int j = 0; j < n; j++) {
            if (a[j] <= y[i] && y[i] <= b[j]) {
                bst[t[j]] = min(bst[t[j]], abs(x[j] - l[i]));
            }
        }
        if (*max_element(bst.begin(), bst.end()) == INF) ans.pb(-1);
        else ans.pb(*max_element(bst.begin(), bst.end()));
    }
    return ans;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (1) {
        int n, k, q;
        cin >> n >> k >> q;
        vector<int> x(n), t(n), a(n), b(n), l(q), y(q);
        for (int i = 0; i < n; i++) {
            cin >> x[i] >> t[i] >> a[i] >> b[i];
            t[i]--;
        }
        for (int i = 0; i < q; i++) {
            cin >> l[i] >> y[i];
        }
        vector<int> ans = solve(n, k, q, x, t, a, b, l, y);
        for (auto to : ans) {
            cout << to << '\n';
        }
        return 0;
    }
    while (1) {
        int n = rnd() % 5 + 1, k = rnd() % 5 + 1, q = rnd() % 1 + 1;
        vector<int> x(n), t(n), a(n), b(n), l(q), y(q);
        for (int i = 0; i < n; i++) {
            x[i] = rnd() % 10 + 1;
            t[i] = rnd() % k;
            a[i] = rnd() % 10 + 1;
            b[i] = rnd() % 10 + 1;
            if (a[i] > b[i]) swap(a[i], b[i]);
        }
        for (int i = 0; i < q; i++) {
            l[i] = rnd() % 10 + 1;
            y[i] = rnd() % 10 + 1;
        }
        auto res = solve(n, k, q, x, t, a, b, l, y), res2 = solvee(n, k, q, x, t, a, b, l, y);
        if (res != res2) {
            cout << n << ' ' << k << ' ' << q << endl;
            for (int i = 0; i < n; i++) {
                cout << x[i] << ' ' <<  t[i] + 1 << ' ' << a[i] << ' ' << b[i] << endl;
            }
            for (int i = 0; i < q; i++) {
                cout << l[i] << ' ' << y[i] << endl;
            }
            cout << "Bad" << endl;
            for (int i = 0; i < q; i++) {
                cout << res[i] << ' ' << res2[i] << endl;
            }
            return 0;
        }
        cout << "ok" << endl;
    }
    return 0;
}

Compilation message

new_home.cpp: In function 'void add(int, int)':
new_home.cpp:77:9: warning: unused variable 'I' [-Wunused-variable]
   77 |     int I = lower_bound(u2.begin(), u2.end(), a) - u2.begin(), J = lower_bound(u2.begin(), u2.end(), b) - u2.begin();
      |         ^
new_home.cpp:77:64: warning: unused variable 'J' [-Wunused-variable]
   77 |     int I = lower_bound(u2.begin(), u2.end(), a) - u2.begin(), J = lower_bound(u2.begin(), u2.end(), b) - u2.begin();
      |                                                                ^
new_home.cpp: In function 'void del(int, int)':
new_home.cpp:85:9: warning: unused variable 'I' [-Wunused-variable]
   85 |     int I = lower_bound(u2.begin(), u2.end(), a) - u2.begin(), J = lower_bound(u2.begin(), u2.end(), b) - u2.begin();
      |         ^
new_home.cpp:85:64: warning: unused variable 'J' [-Wunused-variable]
   85 |     int I = lower_bound(u2.begin(), u2.end(), a) - u2.begin(), J = lower_bound(u2.begin(), u2.end(), b) - u2.begin();
      |                                                                ^
new_home.cpp: In function 'std::vector<int> solve(int, int, int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
new_home.cpp:129:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for (int j = 0; j + 1 < ind[i].size(); j++) {
      |                         ~~~~~~^~~~~~~~~~~~~~~
new_home.cpp:159:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |     for (int i = 0; i < qu.size(); i++) {
      |                     ~~^~~~~~~~~~~
new_home.cpp:147:10: warning: variable 'bad' set but not used [-Wunused-but-set-variable]
  147 |     bool bad = 0;
      |          ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 980 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 2 ms 980 KB Output is correct
13 Correct 2 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 3 ms 1108 KB Output is correct
16 Correct 3 ms 1108 KB Output is correct
17 Correct 3 ms 980 KB Output is correct
18 Correct 3 ms 1108 KB Output is correct
19 Correct 3 ms 1108 KB Output is correct
20 Correct 3 ms 980 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 3 ms 1248 KB Output is correct
23 Correct 3 ms 1124 KB Output is correct
24 Correct 3 ms 1104 KB Output is correct
25 Correct 3 ms 980 KB Output is correct
26 Correct 3 ms 980 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 980 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 2 ms 980 KB Output is correct
13 Correct 2 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 3 ms 1108 KB Output is correct
16 Correct 3 ms 1108 KB Output is correct
17 Correct 3 ms 980 KB Output is correct
18 Correct 3 ms 1108 KB Output is correct
19 Correct 3 ms 1108 KB Output is correct
20 Correct 3 ms 980 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 3 ms 1248 KB Output is correct
23 Correct 3 ms 1124 KB Output is correct
24 Correct 3 ms 1104 KB Output is correct
25 Correct 3 ms 980 KB Output is correct
26 Correct 3 ms 980 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 764 ms 104908 KB Output is correct
32 Correct 110 ms 16304 KB Output is correct
33 Correct 698 ms 99168 KB Output is correct
34 Correct 650 ms 99568 KB Output is correct
35 Correct 702 ms 104612 KB Output is correct
36 Correct 721 ms 104416 KB Output is correct
37 Correct 555 ms 96684 KB Output is correct
38 Correct 536 ms 96584 KB Output is correct
39 Correct 440 ms 96136 KB Output is correct
40 Correct 440 ms 96088 KB Output is correct
41 Correct 500 ms 96120 KB Output is correct
42 Correct 524 ms 96480 KB Output is correct
43 Correct 105 ms 19736 KB Output is correct
44 Correct 488 ms 96164 KB Output is correct
45 Correct 462 ms 96100 KB Output is correct
46 Correct 423 ms 95936 KB Output is correct
47 Correct 359 ms 93108 KB Output is correct
48 Correct 352 ms 92344 KB Output is correct
49 Correct 373 ms 94076 KB Output is correct
50 Correct 398 ms 95716 KB Output is correct
51 Correct 367 ms 93604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3825 ms 561616 KB Output is correct
2 Correct 2831 ms 517308 KB Output is correct
3 Correct 4582 ms 736404 KB Output is correct
4 Correct 4145 ms 591232 KB Output is correct
5 Correct 2549 ms 517088 KB Output is correct
6 Correct 2719 ms 517200 KB Output is correct
7 Correct 4168 ms 736544 KB Output is correct
8 Correct 3643 ms 591664 KB Output is correct
9 Correct 3211 ms 540436 KB Output is correct
10 Correct 2826 ms 517832 KB Output is correct
11 Correct 2284 ms 508952 KB Output is correct
12 Correct 2561 ms 514560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4369 ms 522392 KB Output is correct
2 Correct 861 ms 87936 KB Output is correct
3 Correct 3968 ms 516960 KB Output is correct
4 Execution timed out 5098 ms 736980 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 980 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 2 ms 980 KB Output is correct
13 Correct 2 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 3 ms 1108 KB Output is correct
16 Correct 3 ms 1108 KB Output is correct
17 Correct 3 ms 980 KB Output is correct
18 Correct 3 ms 1108 KB Output is correct
19 Correct 3 ms 1108 KB Output is correct
20 Correct 3 ms 980 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 3 ms 1248 KB Output is correct
23 Correct 3 ms 1124 KB Output is correct
24 Correct 3 ms 1104 KB Output is correct
25 Correct 3 ms 980 KB Output is correct
26 Correct 3 ms 980 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 764 ms 104908 KB Output is correct
32 Correct 110 ms 16304 KB Output is correct
33 Correct 698 ms 99168 KB Output is correct
34 Correct 650 ms 99568 KB Output is correct
35 Correct 702 ms 104612 KB Output is correct
36 Correct 721 ms 104416 KB Output is correct
37 Correct 555 ms 96684 KB Output is correct
38 Correct 536 ms 96584 KB Output is correct
39 Correct 440 ms 96136 KB Output is correct
40 Correct 440 ms 96088 KB Output is correct
41 Correct 500 ms 96120 KB Output is correct
42 Correct 524 ms 96480 KB Output is correct
43 Correct 105 ms 19736 KB Output is correct
44 Correct 488 ms 96164 KB Output is correct
45 Correct 462 ms 96100 KB Output is correct
46 Correct 423 ms 95936 KB Output is correct
47 Correct 359 ms 93108 KB Output is correct
48 Correct 352 ms 92344 KB Output is correct
49 Correct 373 ms 94076 KB Output is correct
50 Correct 398 ms 95716 KB Output is correct
51 Correct 367 ms 93604 KB Output is correct
52 Correct 897 ms 148252 KB Output is correct
53 Correct 903 ms 144660 KB Output is correct
54 Correct 786 ms 119192 KB Output is correct
55 Correct 674 ms 113596 KB Output is correct
56 Correct 693 ms 122276 KB Output is correct
57 Correct 618 ms 101344 KB Output is correct
58 Correct 680 ms 113876 KB Output is correct
59 Correct 726 ms 122476 KB Output is correct
60 Correct 626 ms 101684 KB Output is correct
61 Correct 319 ms 42396 KB Output is correct
62 Correct 820 ms 148336 KB Output is correct
63 Correct 758 ms 125116 KB Output is correct
64 Correct 762 ms 116416 KB Output is correct
65 Correct 661 ms 102964 KB Output is correct
66 Correct 491 ms 96796 KB Output is correct
67 Correct 319 ms 19224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 3 ms 980 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 3 ms 980 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 2 ms 980 KB Output is correct
13 Correct 2 ms 980 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 3 ms 1108 KB Output is correct
16 Correct 3 ms 1108 KB Output is correct
17 Correct 3 ms 980 KB Output is correct
18 Correct 3 ms 1108 KB Output is correct
19 Correct 3 ms 1108 KB Output is correct
20 Correct 3 ms 980 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 3 ms 1248 KB Output is correct
23 Correct 3 ms 1124 KB Output is correct
24 Correct 3 ms 1104 KB Output is correct
25 Correct 3 ms 980 KB Output is correct
26 Correct 3 ms 980 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 764 ms 104908 KB Output is correct
32 Correct 110 ms 16304 KB Output is correct
33 Correct 698 ms 99168 KB Output is correct
34 Correct 650 ms 99568 KB Output is correct
35 Correct 702 ms 104612 KB Output is correct
36 Correct 721 ms 104416 KB Output is correct
37 Correct 555 ms 96684 KB Output is correct
38 Correct 536 ms 96584 KB Output is correct
39 Correct 440 ms 96136 KB Output is correct
40 Correct 440 ms 96088 KB Output is correct
41 Correct 500 ms 96120 KB Output is correct
42 Correct 524 ms 96480 KB Output is correct
43 Correct 105 ms 19736 KB Output is correct
44 Correct 488 ms 96164 KB Output is correct
45 Correct 462 ms 96100 KB Output is correct
46 Correct 423 ms 95936 KB Output is correct
47 Correct 359 ms 93108 KB Output is correct
48 Correct 352 ms 92344 KB Output is correct
49 Correct 373 ms 94076 KB Output is correct
50 Correct 398 ms 95716 KB Output is correct
51 Correct 367 ms 93604 KB Output is correct
52 Correct 3825 ms 561616 KB Output is correct
53 Correct 2831 ms 517308 KB Output is correct
54 Correct 4582 ms 736404 KB Output is correct
55 Correct 4145 ms 591232 KB Output is correct
56 Correct 2549 ms 517088 KB Output is correct
57 Correct 2719 ms 517200 KB Output is correct
58 Correct 4168 ms 736544 KB Output is correct
59 Correct 3643 ms 591664 KB Output is correct
60 Correct 3211 ms 540436 KB Output is correct
61 Correct 2826 ms 517832 KB Output is correct
62 Correct 2284 ms 508952 KB Output is correct
63 Correct 2561 ms 514560 KB Output is correct
64 Correct 4369 ms 522392 KB Output is correct
65 Correct 861 ms 87936 KB Output is correct
66 Correct 3968 ms 516960 KB Output is correct
67 Execution timed out 5098 ms 736980 KB Time limit exceeded
68 Halted 0 ms 0 KB -