답안 #560853

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
560853 2022-05-12T03:53:56 Z hoanghq2004 새 집 (APIO18_new_home) C++14
100 / 100
2632 ms 144396 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 3e5 + 10;
const int M = 3e8 + 10;

int n, k, q;
set <int> s[N];
int ans[N];
int cnt;
vector <pair <int, int>> dta;

struct SegmentTree {
    int maxi[N * 10];
    int mini[N * 10];
    SegmentTree() {
        memset(maxi, -1, sizeof(maxi));
        memset(mini, 60, sizeof(mini));
    }
    void upd(int id, int L, int R, int i, int val) {
        if (L == R) {
            maxi[id] = val;
            mini[id] = val;
            return;
        }
        int mid = L + R >> 1;
        if (i <= mid) upd(id * 2, L, mid, i, val);
        else upd(id * 2 + 1, mid + 1, R, i, val);
        maxi[id] = max(maxi[id * 2], maxi[id * 2 + 1]);
        mini[id] = min(mini[id * 2], mini[id * 2 + 1]);
    }
    int get_left(int id, int L, int R, int i, int k) {
        if (L >= i || maxi[id] < k) return -1;
        if (L == R) return L;
        int mid = L + R >> 1;
        int ans = get_left(id * 2, L, mid, i, k);
        if (ans == -1) ans = get_left(id * 2 + 1, mid + 1, R, i, k);
        return ans;
    }
    int get_right(int id, int L, int R, int i, int k) {
        if (i >= R || mini[id] > k) return -1;
        if (L == R) return L;
        int mid = L + R >> 1;
        int ans = get_right(id * 2 + 1, mid + 1, R, i, k);
        if (ans == -1) ans = get_right(id * 2, L, mid, i, k);
        return ans;
    }
    void upd(int i, int val) {
        upd(1, 0, dta.size() - 1, i, val);
    }
    int get_left(int x) {
        int i = lower_bound(dta.begin(), dta.end(), make_pair(x, 0)) - dta.begin();
        int id = get_left(1, 0, dta.size() - 1, i, x);
        if (id == -1) return 1e9;
        return dta[id].first;
    }
    int get_right(int x) {
        int i = lower_bound(dta.begin(), dta.end(), make_pair(x, 0)) - dta.begin();
        int id = get_right(1, 0, dta.size() - 1, i, x);
        if (id == -1) return -1e9;
        return dta[id].first;
    }
} S[2];

void add_(int L, int R) {
    if (dta[L].first == dta[R].first) {
        S[0].upd(L, dta[L].first);
        S[1].upd(R, dta[R].first);
        return;
    }
    int mid = dta[L].first + dta[R].first >> 1;
    S[0].upd(L, mid);
    S[1].upd(R, mid + 1);
}

void rev_(int L, int R) {
    S[0].upd(L, -1);
    S[1].upd(R, 1e9);
}

void add(int t, int X, int i) {
    int x = lower_bound(dta.begin(), dta.end(), make_pair(X, i)) - dta.begin();
    cnt -= (s[t].size() == 2);
    int L = *(--s[t].upper_bound(x));
    int R = *s[t].upper_bound(x);
    rev_(L, R);
    add_(L, x);
    add_(x, R);
    s[t].insert(x);
}

void rev(int t, int X, int i) {
    int x = lower_bound(dta.begin(), dta.end(), make_pair(X, i)) - dta.begin();
    s[t].erase(x);
    int L = *(--s[t].upper_bound(x));
    int R = *s[t].upper_bound(x);
    rev_(L, x);
    rev_(x, R);
    add_(L, R);
    cnt += (s[t].size() == 2);
}

int main() {
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    ios :: sync_with_stdio(0); cin.tie(0);
    vector <tuple <int, int, int, int, int> > events;
    cin >> n >> k >> q;
    for (int i = 1; i <= n; ++i) {
        int x, t, a, b;
        cin >> x >> t >> a >> b;
        x += 1e8 + 5;
        dta.push_back({x, i});
        events.push_back({a, -1, x, t, i});
        events.push_back({b + 1, 1, x, t, i});
    }
    sort(events.begin(), events.end());

    cnt = k;
    vector <tuple <int, int, int> > work;
    for (int i = 1; i <= q; ++i) {
        int x, y;
        cin >> x >> y;
        x += 1e8 + 5;
        dta.push_back({x, 0});
        work.push_back({y, x, i});
    }
    int L = 0, R = M;
    dta.push_back({L, 0}), dta.push_back({R, 0});
    sort(work.begin(), work.end());
    sort(dta.begin(), dta.end());
    dta.erase(unique(dta.begin(), dta.end()), dta.end());
    add_(0, (int)dta.size() - 1);
    for (int i = 1; i <= k; ++i) s[i].insert(0), s[i].insert((int)dta.size() - 1);
    int i = 0;
    for (auto [y, x, id]: work) {
        while (i < events.size() && get<0>(events[i]) <= y) {
            auto [_, cmd, x, t, _i] = events[i];
            if (cmd == -1) add(t, x, _i);
            else rev(t, x, _i);
            ++i;
        }
        if (cnt) ans[id] = -1;
        else {
            int L = S[0].get_left(x);
            int R = S[1].get_right(x);
            ans[id] = max(x - L, R - x);
        }
    }
    for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
}

Compilation message

new_home.cpp: In member function 'void SegmentTree::upd(int, int, int, int, int)':
new_home.cpp:35:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In member function 'int SegmentTree::get_left(int, int, int, int, int)':
new_home.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In member function 'int SegmentTree::get_right(int, int, int, int, int)':
new_home.cpp:52:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In function 'void add_(int, int)':
new_home.cpp:80:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |     int mid = dta[L].first + dta[R].first >> 1;
new_home.cpp: In function 'int main()':
new_home.cpp:145:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  145 |     for (auto [y, x, id]: work) {
      |               ^
new_home.cpp:146:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |         while (i < events.size() && get<0>(events[i]) <= y) {
      |                ~~^~~~~~~~~~~~~~~
new_home.cpp:147:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  147 |             auto [_, cmd, x, t, _i] = events[i];
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 61268 KB Output is correct
2 Correct 27 ms 61372 KB Output is correct
3 Correct 24 ms 61252 KB Output is correct
4 Correct 26 ms 61524 KB Output is correct
5 Correct 25 ms 61380 KB Output is correct
6 Correct 26 ms 61408 KB Output is correct
7 Correct 25 ms 61452 KB Output is correct
8 Correct 25 ms 61456 KB Output is correct
9 Correct 25 ms 61420 KB Output is correct
10 Correct 26 ms 61388 KB Output is correct
11 Correct 26 ms 61400 KB Output is correct
12 Correct 26 ms 61440 KB Output is correct
13 Correct 25 ms 61376 KB Output is correct
14 Correct 25 ms 61444 KB Output is correct
15 Correct 26 ms 61380 KB Output is correct
16 Correct 25 ms 61420 KB Output is correct
17 Correct 25 ms 61396 KB Output is correct
18 Correct 26 ms 61396 KB Output is correct
19 Correct 29 ms 61356 KB Output is correct
20 Correct 28 ms 61396 KB Output is correct
21 Correct 24 ms 61440 KB Output is correct
22 Correct 26 ms 61480 KB Output is correct
23 Correct 28 ms 61380 KB Output is correct
24 Correct 25 ms 61452 KB Output is correct
25 Correct 25 ms 61396 KB Output is correct
26 Correct 27 ms 61372 KB Output is correct
27 Correct 25 ms 61428 KB Output is correct
28 Correct 24 ms 61396 KB Output is correct
29 Correct 25 ms 61376 KB Output is correct
30 Correct 25 ms 61440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 61268 KB Output is correct
2 Correct 27 ms 61372 KB Output is correct
3 Correct 24 ms 61252 KB Output is correct
4 Correct 26 ms 61524 KB Output is correct
5 Correct 25 ms 61380 KB Output is correct
6 Correct 26 ms 61408 KB Output is correct
7 Correct 25 ms 61452 KB Output is correct
8 Correct 25 ms 61456 KB Output is correct
9 Correct 25 ms 61420 KB Output is correct
10 Correct 26 ms 61388 KB Output is correct
11 Correct 26 ms 61400 KB Output is correct
12 Correct 26 ms 61440 KB Output is correct
13 Correct 25 ms 61376 KB Output is correct
14 Correct 25 ms 61444 KB Output is correct
15 Correct 26 ms 61380 KB Output is correct
16 Correct 25 ms 61420 KB Output is correct
17 Correct 25 ms 61396 KB Output is correct
18 Correct 26 ms 61396 KB Output is correct
19 Correct 29 ms 61356 KB Output is correct
20 Correct 28 ms 61396 KB Output is correct
21 Correct 24 ms 61440 KB Output is correct
22 Correct 26 ms 61480 KB Output is correct
23 Correct 28 ms 61380 KB Output is correct
24 Correct 25 ms 61452 KB Output is correct
25 Correct 25 ms 61396 KB Output is correct
26 Correct 27 ms 61372 KB Output is correct
27 Correct 25 ms 61428 KB Output is correct
28 Correct 24 ms 61396 KB Output is correct
29 Correct 25 ms 61376 KB Output is correct
30 Correct 25 ms 61440 KB Output is correct
31 Correct 310 ms 71892 KB Output is correct
32 Correct 191 ms 68016 KB Output is correct
33 Correct 303 ms 69872 KB Output is correct
34 Correct 289 ms 70104 KB Output is correct
35 Correct 326 ms 71752 KB Output is correct
36 Correct 340 ms 71676 KB Output is correct
37 Correct 260 ms 69960 KB Output is correct
38 Correct 271 ms 69912 KB Output is correct
39 Correct 216 ms 69832 KB Output is correct
40 Correct 223 ms 69784 KB Output is correct
41 Correct 239 ms 70040 KB Output is correct
42 Correct 235 ms 69912 KB Output is correct
43 Correct 148 ms 71304 KB Output is correct
44 Correct 236 ms 70132 KB Output is correct
45 Correct 249 ms 69972 KB Output is correct
46 Correct 230 ms 69968 KB Output is correct
47 Correct 168 ms 69536 KB Output is correct
48 Correct 171 ms 69560 KB Output is correct
49 Correct 183 ms 69724 KB Output is correct
50 Correct 192 ms 69928 KB Output is correct
51 Correct 188 ms 69696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 946 ms 108788 KB Output is correct
2 Correct 984 ms 104372 KB Output is correct
3 Correct 830 ms 133860 KB Output is correct
4 Correct 915 ms 115220 KB Output is correct
5 Correct 958 ms 104180 KB Output is correct
6 Correct 958 ms 104392 KB Output is correct
7 Correct 837 ms 133836 KB Output is correct
8 Correct 913 ms 117112 KB Output is correct
9 Correct 935 ms 110552 KB Output is correct
10 Correct 947 ms 107068 KB Output is correct
11 Correct 752 ms 106100 KB Output is correct
12 Correct 788 ms 107044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1717 ms 103404 KB Output is correct
2 Correct 990 ms 104072 KB Output is correct
3 Correct 1814 ms 113204 KB Output is correct
4 Correct 1166 ms 142004 KB Output is correct
5 Correct 1396 ms 119336 KB Output is correct
6 Correct 1364 ms 122972 KB Output is correct
7 Correct 1821 ms 112816 KB Output is correct
8 Correct 1826 ms 113028 KB Output is correct
9 Correct 1335 ms 143036 KB Output is correct
10 Correct 1543 ms 121832 KB Output is correct
11 Correct 1700 ms 116444 KB Output is correct
12 Correct 1866 ms 113996 KB Output is correct
13 Correct 1000 ms 111848 KB Output is correct
14 Correct 1006 ms 111244 KB Output is correct
15 Correct 1090 ms 112720 KB Output is correct
16 Correct 1171 ms 113696 KB Output is correct
17 Correct 1153 ms 112316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 61268 KB Output is correct
2 Correct 27 ms 61372 KB Output is correct
3 Correct 24 ms 61252 KB Output is correct
4 Correct 26 ms 61524 KB Output is correct
5 Correct 25 ms 61380 KB Output is correct
6 Correct 26 ms 61408 KB Output is correct
7 Correct 25 ms 61452 KB Output is correct
8 Correct 25 ms 61456 KB Output is correct
9 Correct 25 ms 61420 KB Output is correct
10 Correct 26 ms 61388 KB Output is correct
11 Correct 26 ms 61400 KB Output is correct
12 Correct 26 ms 61440 KB Output is correct
13 Correct 25 ms 61376 KB Output is correct
14 Correct 25 ms 61444 KB Output is correct
15 Correct 26 ms 61380 KB Output is correct
16 Correct 25 ms 61420 KB Output is correct
17 Correct 25 ms 61396 KB Output is correct
18 Correct 26 ms 61396 KB Output is correct
19 Correct 29 ms 61356 KB Output is correct
20 Correct 28 ms 61396 KB Output is correct
21 Correct 24 ms 61440 KB Output is correct
22 Correct 26 ms 61480 KB Output is correct
23 Correct 28 ms 61380 KB Output is correct
24 Correct 25 ms 61452 KB Output is correct
25 Correct 25 ms 61396 KB Output is correct
26 Correct 27 ms 61372 KB Output is correct
27 Correct 25 ms 61428 KB Output is correct
28 Correct 24 ms 61396 KB Output is correct
29 Correct 25 ms 61376 KB Output is correct
30 Correct 25 ms 61440 KB Output is correct
31 Correct 310 ms 71892 KB Output is correct
32 Correct 191 ms 68016 KB Output is correct
33 Correct 303 ms 69872 KB Output is correct
34 Correct 289 ms 70104 KB Output is correct
35 Correct 326 ms 71752 KB Output is correct
36 Correct 340 ms 71676 KB Output is correct
37 Correct 260 ms 69960 KB Output is correct
38 Correct 271 ms 69912 KB Output is correct
39 Correct 216 ms 69832 KB Output is correct
40 Correct 223 ms 69784 KB Output is correct
41 Correct 239 ms 70040 KB Output is correct
42 Correct 235 ms 69912 KB Output is correct
43 Correct 148 ms 71304 KB Output is correct
44 Correct 236 ms 70132 KB Output is correct
45 Correct 249 ms 69972 KB Output is correct
46 Correct 230 ms 69968 KB Output is correct
47 Correct 168 ms 69536 KB Output is correct
48 Correct 171 ms 69560 KB Output is correct
49 Correct 183 ms 69724 KB Output is correct
50 Correct 192 ms 69928 KB Output is correct
51 Correct 188 ms 69696 KB Output is correct
52 Correct 271 ms 77272 KB Output is correct
53 Correct 230 ms 75592 KB Output is correct
54 Correct 262 ms 73424 KB Output is correct
55 Correct 236 ms 72028 KB Output is correct
56 Correct 238 ms 73324 KB Output is correct
57 Correct 256 ms 70256 KB Output is correct
58 Correct 237 ms 72032 KB Output is correct
59 Correct 250 ms 73216 KB Output is correct
60 Correct 230 ms 69980 KB Output is correct
61 Correct 127 ms 77032 KB Output is correct
62 Correct 251 ms 77412 KB Output is correct
63 Correct 265 ms 74112 KB Output is correct
64 Correct 261 ms 72740 KB Output is correct
65 Correct 255 ms 70296 KB Output is correct
66 Correct 278 ms 70112 KB Output is correct
67 Correct 213 ms 68232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 61268 KB Output is correct
2 Correct 27 ms 61372 KB Output is correct
3 Correct 24 ms 61252 KB Output is correct
4 Correct 26 ms 61524 KB Output is correct
5 Correct 25 ms 61380 KB Output is correct
6 Correct 26 ms 61408 KB Output is correct
7 Correct 25 ms 61452 KB Output is correct
8 Correct 25 ms 61456 KB Output is correct
9 Correct 25 ms 61420 KB Output is correct
10 Correct 26 ms 61388 KB Output is correct
11 Correct 26 ms 61400 KB Output is correct
12 Correct 26 ms 61440 KB Output is correct
13 Correct 25 ms 61376 KB Output is correct
14 Correct 25 ms 61444 KB Output is correct
15 Correct 26 ms 61380 KB Output is correct
16 Correct 25 ms 61420 KB Output is correct
17 Correct 25 ms 61396 KB Output is correct
18 Correct 26 ms 61396 KB Output is correct
19 Correct 29 ms 61356 KB Output is correct
20 Correct 28 ms 61396 KB Output is correct
21 Correct 24 ms 61440 KB Output is correct
22 Correct 26 ms 61480 KB Output is correct
23 Correct 28 ms 61380 KB Output is correct
24 Correct 25 ms 61452 KB Output is correct
25 Correct 25 ms 61396 KB Output is correct
26 Correct 27 ms 61372 KB Output is correct
27 Correct 25 ms 61428 KB Output is correct
28 Correct 24 ms 61396 KB Output is correct
29 Correct 25 ms 61376 KB Output is correct
30 Correct 25 ms 61440 KB Output is correct
31 Correct 310 ms 71892 KB Output is correct
32 Correct 191 ms 68016 KB Output is correct
33 Correct 303 ms 69872 KB Output is correct
34 Correct 289 ms 70104 KB Output is correct
35 Correct 326 ms 71752 KB Output is correct
36 Correct 340 ms 71676 KB Output is correct
37 Correct 260 ms 69960 KB Output is correct
38 Correct 271 ms 69912 KB Output is correct
39 Correct 216 ms 69832 KB Output is correct
40 Correct 223 ms 69784 KB Output is correct
41 Correct 239 ms 70040 KB Output is correct
42 Correct 235 ms 69912 KB Output is correct
43 Correct 148 ms 71304 KB Output is correct
44 Correct 236 ms 70132 KB Output is correct
45 Correct 249 ms 69972 KB Output is correct
46 Correct 230 ms 69968 KB Output is correct
47 Correct 168 ms 69536 KB Output is correct
48 Correct 171 ms 69560 KB Output is correct
49 Correct 183 ms 69724 KB Output is correct
50 Correct 192 ms 69928 KB Output is correct
51 Correct 188 ms 69696 KB Output is correct
52 Correct 946 ms 108788 KB Output is correct
53 Correct 984 ms 104372 KB Output is correct
54 Correct 830 ms 133860 KB Output is correct
55 Correct 915 ms 115220 KB Output is correct
56 Correct 958 ms 104180 KB Output is correct
57 Correct 958 ms 104392 KB Output is correct
58 Correct 837 ms 133836 KB Output is correct
59 Correct 913 ms 117112 KB Output is correct
60 Correct 935 ms 110552 KB Output is correct
61 Correct 947 ms 107068 KB Output is correct
62 Correct 752 ms 106100 KB Output is correct
63 Correct 788 ms 107044 KB Output is correct
64 Correct 1717 ms 103404 KB Output is correct
65 Correct 990 ms 104072 KB Output is correct
66 Correct 1814 ms 113204 KB Output is correct
67 Correct 1166 ms 142004 KB Output is correct
68 Correct 1396 ms 119336 KB Output is correct
69 Correct 1364 ms 122972 KB Output is correct
70 Correct 1821 ms 112816 KB Output is correct
71 Correct 1826 ms 113028 KB Output is correct
72 Correct 1335 ms 143036 KB Output is correct
73 Correct 1543 ms 121832 KB Output is correct
74 Correct 1700 ms 116444 KB Output is correct
75 Correct 1866 ms 113996 KB Output is correct
76 Correct 1000 ms 111848 KB Output is correct
77 Correct 1006 ms 111244 KB Output is correct
78 Correct 1090 ms 112720 KB Output is correct
79 Correct 1171 ms 113696 KB Output is correct
80 Correct 1153 ms 112316 KB Output is correct
81 Correct 271 ms 77272 KB Output is correct
82 Correct 230 ms 75592 KB Output is correct
83 Correct 262 ms 73424 KB Output is correct
84 Correct 236 ms 72028 KB Output is correct
85 Correct 238 ms 73324 KB Output is correct
86 Correct 256 ms 70256 KB Output is correct
87 Correct 237 ms 72032 KB Output is correct
88 Correct 250 ms 73216 KB Output is correct
89 Correct 230 ms 69980 KB Output is correct
90 Correct 127 ms 77032 KB Output is correct
91 Correct 251 ms 77412 KB Output is correct
92 Correct 265 ms 74112 KB Output is correct
93 Correct 261 ms 72740 KB Output is correct
94 Correct 255 ms 70296 KB Output is correct
95 Correct 278 ms 70112 KB Output is correct
96 Correct 213 ms 68232 KB Output is correct
97 Correct 1423 ms 144184 KB Output is correct
98 Correct 1073 ms 97252 KB Output is correct
99 Correct 2306 ms 106488 KB Output is correct
100 Correct 1429 ms 135140 KB Output is correct
101 Correct 1654 ms 125004 KB Output is correct
102 Correct 2632 ms 115204 KB Output is correct
103 Correct 1969 ms 106524 KB Output is correct
104 Correct 2121 ms 105940 KB Output is correct
105 Correct 1374 ms 106004 KB Output is correct
106 Correct 1361 ms 105952 KB Output is correct
107 Correct 1576 ms 117352 KB Output is correct
108 Correct 1515 ms 124272 KB Output is correct
109 Correct 1565 ms 108292 KB Output is correct
110 Correct 1577 ms 116812 KB Output is correct
111 Correct 1585 ms 123744 KB Output is correct
112 Correct 1569 ms 107576 KB Output is correct
113 Correct 600 ms 142764 KB Output is correct
114 Correct 1462 ms 144396 KB Output is correct
115 Correct 1585 ms 128004 KB Output is correct
116 Correct 1660 ms 121016 KB Output is correct
117 Correct 1729 ms 109040 KB Output is correct
118 Correct 1726 ms 107812 KB Output is correct
119 Correct 1301 ms 99120 KB Output is correct
120 Correct 804 ms 103612 KB Output is correct
121 Correct 957 ms 105244 KB Output is correct
122 Correct 932 ms 105068 KB Output is correct
123 Correct 1181 ms 105744 KB Output is correct
124 Correct 1127 ms 106504 KB Output is correct
125 Correct 1121 ms 106080 KB Output is correct
126 Correct 1164 ms 106496 KB Output is correct