답안 #104336

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104336 2019-04-05T09:51:46 Z puyu_liao 새 집 (APIO18_new_home) C++14
100 / 100
2586 ms 144260 KB
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1.01e9;

void print(vector<int> v) {
    for (int x : v) printf("%d\n", x);
}

struct Segtree {
    int n, N;
    vector<int> t;

    Segtree(int _n) {
        n = _n;
        N = 1;
        while (N < n) N <<= 1;
        t.assign(2 * N, INF);
    }

    void upd(int x, int y) {
        x += N;
        t[x] = y;
        while (x > 1) {
            x >>= 1;
            t[x] = min(t[x * 2], t[x * 2 + 1]);
        }
    }

    int get(int l, int r) {
        int ans = INF;
        l += N; r += N;
        while (l <= r) {
            if (l & 1) ans = min(ans, t[l]);
            if (!(r & 1)) ans = min(ans, t[r]);
            l = (l + 1) >> 1;
            r = (r - 1) >> 1;
        }
        return ans;
    }
};

struct Shop {
    int x, type, a, b;
};

struct Query {
    int x, time;
};

vector<int> fast(vector<Shop> a, int k, vector<Query> b) {
    int n = a.size(), m = b.size();
    vector<int> ans(m, 0);
    for (auto &s : a) {
        s.a = s.a * 3;
        s.b = s.b * 3 + 2;
    }
    for (auto &o : b) o.time = o.time * 3 + 1;

    sort(a.begin(), a.end(), [&](const Shop &x, const Shop &y) {
        return x.type < y.type;
    });


    struct Event {
        int time, type, x;
        bool operator< (const Event &e) const {
            return time < e.time;
        }
    };

    struct Update {
        int t1, t2, x1, x2, val;
    };

    vector<Update> vct[2];
    vct[0].reserve(3 * n + k + 10);
    vct[1].reserve(3 * n + k + 10);

    auto addUpdate = [&](int t1, int t2, int l, int r) {
        if (t1 >= t2) return;
        int mid = l + (r - l) / 2;
        vct[0].push_back({ t1, t2, l, mid, l });
        vct[1].push_back({ t1, t2, -r, -(r - (r - l) / 2), -r });
    };

    int ci = 0;
    for (int c = 0; c < k; c++) {
        static multiset<pair<int, int>> st;
        st.clear();
        st.insert({ -INF, -1 });
        st.insert({ +INF, -1 });

        static vector<Event> events;
        events.clear();
        while (ci < n && a[ci].type == c) {
            auto o = a[ci++];
            events.push_back({ o.a, +1, o.x });
            events.push_back({ o.b, -1, o.x });
        }
        sort(events.begin(), events.end());

        for (auto ev : events) {
            if (ev.type == +1) {
                auto it = st.lower_bound({ ev.x, -1 });
                int rightX = it->first;
                int nTime = ev.time;
                if (rightX == ev.x) nTime = it->second;
                it--;
                int leftX = it->first;
                addUpdate(it->second, ev.time, leftX, rightX);
                st.erase(it);
                st.insert({ leftX, ev.time });
                st.insert({ ev.x, nTime });
            }
            else {
                auto it = st.lower_bound({ ev.x, -1 });
                int midX = it->first, midT = it->second;
                st.erase(it);
                it = st.lower_bound({ ev.x, -1 });
                addUpdate(midT, ev.time, midX, it->first);
                it--;
                int leftX = it->first;
                addUpdate(it->second, ev.time, leftX, midX);
                st.erase(it);
                st.insert({ leftX, ev.time });
            }
        }
        assert(st.size() == 2);
        addUpdate(st.begin()->second, INF, -INF, +INF);
    }

    vector<int> perB(m);
    iota(perB.begin(), perB.end(), 0);
    sort(perB.begin(), perB.end(), [&](int i, int j) {
        return b[i].x < b[j].x;
    });
    struct CEvent {
        int time, type, id;
        bool operator< (const CEvent &rhs) const {
            return time < rhs.time;
        }
    };
    vector<CEvent> events;
    events.reserve(m + vct[0].size() * 2);
    for (int i = 0; i < (int)vct[0].size(); i++) {
        events.push_back({ vct[0][i].t1, 1, i });
        events.push_back({ vct[0][i].t2, 2, i });
    }
    for (int i = 0; i < m; i++) events.push_back({ b[i].time, 0, i });
    sort(events.begin(), events.end());
    Segtree tr(vct[0].size());

    for (int ii = 0; ii < 2; ii++) {
        static vector<pair<int, int>> vv;
        vv.clear();
        for (int i = 0; i < (int)vct[ii].size(); i++) vv.push_back({ vct[ii][i].x2, i });

        sort(vv.begin(), vv.end());
        vector<int> lb1(vct[ii].size());
        for (int i = 0; i < (int)vv.size(); i++) lb1[vv[i].second] = i;
        vector<int> lb2(m);
        int ci = 0;
        for (int i = 0; i < m; i++) {
            while (ci < (int)vv.size() && vv[ci].first < b[perB[i]].x) ci++;
            lb2[perB[i]] = ci;
        }

        for (auto ev : events) {
            if (ev.type == 0) {
                int id = lb2[ev.id];
                ans[ev.id] = max(ans[ev.id], b[ev.id].x - tr.get(id, tr.n - 1));
            }
            if (ev.type == 1) {
                int id = lb1[ev.id];
                tr.upd(id, vct[ii][ev.id].x1);
            }
            if (ev.type == 2) {
                int id = lb1[ev.id];
                tr.upd(id, INF);
            }
        }

        for (auto &o : b) o.x *= -1;
        reverse(perB.begin(), perB.end());
    }

    for (int &x : ans) if (x >= INF / 2) x = -1;
    return ans;
}

int main() {
    int n, k, q;
    scanf("%d%d%d", &n, &k, &q);
    vector<Shop> a(n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d%d%d", &a[i].x, &a[i].type, &a[i].a, &a[i].b);
        a[i].type--;
    }
    vector<Query> b(q);
    for (int i = 0; i < q; i++) scanf("%d%d", &b[i].x, &b[i].time);

    print(fast(a, k, b));
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:193:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &k, &q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:196:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d", &a[i].x, &a[i].type, &a[i].a, &a[i].b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:200:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < q; i++) scanf("%d%d", &b[i].x, &b[i].time);
                                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 4 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 4 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 381 ms 23072 KB Output is correct
32 Correct 113 ms 10900 KB Output is correct
33 Correct 324 ms 22900 KB Output is correct
34 Correct 281 ms 22888 KB Output is correct
35 Correct 324 ms 22864 KB Output is correct
36 Correct 329 ms 23080 KB Output is correct
37 Correct 266 ms 23084 KB Output is correct
38 Correct 265 ms 23604 KB Output is correct
39 Correct 225 ms 23148 KB Output is correct
40 Correct 255 ms 23532 KB Output is correct
41 Correct 259 ms 22888 KB Output is correct
42 Correct 289 ms 23016 KB Output is correct
43 Correct 115 ms 12016 KB Output is correct
44 Correct 273 ms 23084 KB Output is correct
45 Correct 277 ms 22888 KB Output is correct
46 Correct 298 ms 23276 KB Output is correct
47 Correct 208 ms 22188 KB Output is correct
48 Correct 215 ms 22284 KB Output is correct
49 Correct 246 ms 22572 KB Output is correct
50 Correct 239 ms 22760 KB Output is correct
51 Correct 224 ms 22312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1111 ms 61280 KB Output is correct
2 Correct 1375 ms 48616 KB Output is correct
3 Correct 1775 ms 128560 KB Output is correct
4 Correct 1222 ms 76644 KB Output is correct
5 Correct 1631 ms 58060 KB Output is correct
6 Correct 1439 ms 51088 KB Output is correct
7 Correct 1767 ms 130524 KB Output is correct
8 Correct 1274 ms 76632 KB Output is correct
9 Correct 1002 ms 56904 KB Output is correct
10 Correct 885 ms 49504 KB Output is correct
11 Correct 899 ms 48880 KB Output is correct
12 Correct 873 ms 60828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1674 ms 75348 KB Output is correct
2 Correct 720 ms 48868 KB Output is correct
3 Correct 1819 ms 75748 KB Output is correct
4 Correct 1939 ms 128508 KB Output is correct
5 Correct 1581 ms 85332 KB Output is correct
6 Correct 1841 ms 91092 KB Output is correct
7 Correct 2540 ms 87088 KB Output is correct
8 Correct 2024 ms 77160 KB Output is correct
9 Correct 1997 ms 128984 KB Output is correct
10 Correct 1691 ms 85772 KB Output is correct
11 Correct 1514 ms 76724 KB Output is correct
12 Correct 1739 ms 74596 KB Output is correct
13 Correct 1045 ms 73828 KB Output is correct
14 Correct 1069 ms 86360 KB Output is correct
15 Correct 1076 ms 86124 KB Output is correct
16 Correct 1267 ms 86628 KB Output is correct
17 Correct 1221 ms 86372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 4 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 381 ms 23072 KB Output is correct
32 Correct 113 ms 10900 KB Output is correct
33 Correct 324 ms 22900 KB Output is correct
34 Correct 281 ms 22888 KB Output is correct
35 Correct 324 ms 22864 KB Output is correct
36 Correct 329 ms 23080 KB Output is correct
37 Correct 266 ms 23084 KB Output is correct
38 Correct 265 ms 23604 KB Output is correct
39 Correct 225 ms 23148 KB Output is correct
40 Correct 255 ms 23532 KB Output is correct
41 Correct 259 ms 22888 KB Output is correct
42 Correct 289 ms 23016 KB Output is correct
43 Correct 115 ms 12016 KB Output is correct
44 Correct 273 ms 23084 KB Output is correct
45 Correct 277 ms 22888 KB Output is correct
46 Correct 298 ms 23276 KB Output is correct
47 Correct 208 ms 22188 KB Output is correct
48 Correct 215 ms 22284 KB Output is correct
49 Correct 246 ms 22572 KB Output is correct
50 Correct 239 ms 22760 KB Output is correct
51 Correct 224 ms 22312 KB Output is correct
52 Correct 347 ms 27496 KB Output is correct
53 Correct 360 ms 27496 KB Output is correct
54 Correct 345 ms 24552 KB Output is correct
55 Correct 304 ms 24652 KB Output is correct
56 Correct 311 ms 25328 KB Output is correct
57 Correct 268 ms 23564 KB Output is correct
58 Correct 338 ms 24680 KB Output is correct
59 Correct 316 ms 25320 KB Output is correct
60 Correct 290 ms 23608 KB Output is correct
61 Correct 223 ms 26984 KB Output is correct
62 Correct 371 ms 27700 KB Output is correct
63 Correct 296 ms 25320 KB Output is correct
64 Correct 352 ms 24552 KB Output is correct
65 Correct 336 ms 23528 KB Output is correct
66 Correct 268 ms 23016 KB Output is correct
67 Correct 300 ms 21864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 4 ms 512 KB Output is correct
16 Correct 4 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 381 ms 23072 KB Output is correct
32 Correct 113 ms 10900 KB Output is correct
33 Correct 324 ms 22900 KB Output is correct
34 Correct 281 ms 22888 KB Output is correct
35 Correct 324 ms 22864 KB Output is correct
36 Correct 329 ms 23080 KB Output is correct
37 Correct 266 ms 23084 KB Output is correct
38 Correct 265 ms 23604 KB Output is correct
39 Correct 225 ms 23148 KB Output is correct
40 Correct 255 ms 23532 KB Output is correct
41 Correct 259 ms 22888 KB Output is correct
42 Correct 289 ms 23016 KB Output is correct
43 Correct 115 ms 12016 KB Output is correct
44 Correct 273 ms 23084 KB Output is correct
45 Correct 277 ms 22888 KB Output is correct
46 Correct 298 ms 23276 KB Output is correct
47 Correct 208 ms 22188 KB Output is correct
48 Correct 215 ms 22284 KB Output is correct
49 Correct 246 ms 22572 KB Output is correct
50 Correct 239 ms 22760 KB Output is correct
51 Correct 224 ms 22312 KB Output is correct
52 Correct 1111 ms 61280 KB Output is correct
53 Correct 1375 ms 48616 KB Output is correct
54 Correct 1775 ms 128560 KB Output is correct
55 Correct 1222 ms 76644 KB Output is correct
56 Correct 1631 ms 58060 KB Output is correct
57 Correct 1439 ms 51088 KB Output is correct
58 Correct 1767 ms 130524 KB Output is correct
59 Correct 1274 ms 76632 KB Output is correct
60 Correct 1002 ms 56904 KB Output is correct
61 Correct 885 ms 49504 KB Output is correct
62 Correct 899 ms 48880 KB Output is correct
63 Correct 873 ms 60828 KB Output is correct
64 Correct 1674 ms 75348 KB Output is correct
65 Correct 720 ms 48868 KB Output is correct
66 Correct 1819 ms 75748 KB Output is correct
67 Correct 1939 ms 128508 KB Output is correct
68 Correct 1581 ms 85332 KB Output is correct
69 Correct 1841 ms 91092 KB Output is correct
70 Correct 2540 ms 87088 KB Output is correct
71 Correct 2024 ms 77160 KB Output is correct
72 Correct 1997 ms 128984 KB Output is correct
73 Correct 1691 ms 85772 KB Output is correct
74 Correct 1514 ms 76724 KB Output is correct
75 Correct 1739 ms 74596 KB Output is correct
76 Correct 1045 ms 73828 KB Output is correct
77 Correct 1069 ms 86360 KB Output is correct
78 Correct 1076 ms 86124 KB Output is correct
79 Correct 1267 ms 86628 KB Output is correct
80 Correct 1221 ms 86372 KB Output is correct
81 Correct 347 ms 27496 KB Output is correct
82 Correct 360 ms 27496 KB Output is correct
83 Correct 345 ms 24552 KB Output is correct
84 Correct 304 ms 24652 KB Output is correct
85 Correct 311 ms 25328 KB Output is correct
86 Correct 268 ms 23564 KB Output is correct
87 Correct 338 ms 24680 KB Output is correct
88 Correct 316 ms 25320 KB Output is correct
89 Correct 290 ms 23608 KB Output is correct
90 Correct 223 ms 26984 KB Output is correct
91 Correct 371 ms 27700 KB Output is correct
92 Correct 296 ms 25320 KB Output is correct
93 Correct 352 ms 24552 KB Output is correct
94 Correct 336 ms 23528 KB Output is correct
95 Correct 268 ms 23016 KB Output is correct
96 Correct 300 ms 21864 KB Output is correct
97 Correct 2493 ms 144120 KB Output is correct
98 Correct 921 ms 54504 KB Output is correct
99 Correct 2181 ms 111832 KB Output is correct
100 Correct 2586 ms 144092 KB Output is correct
101 Correct 2107 ms 119508 KB Output is correct
102 Correct 2478 ms 111700 KB Output is correct
103 Correct 1812 ms 111572 KB Output is correct
104 Correct 1757 ms 113432 KB Output is correct
105 Correct 1418 ms 112532 KB Output is correct
106 Correct 1360 ms 113244 KB Output is correct
107 Correct 2009 ms 119508 KB Output is correct
108 Correct 2168 ms 134444 KB Output is correct
109 Correct 2037 ms 114260 KB Output is correct
110 Correct 1975 ms 119892 KB Output is correct
111 Correct 2110 ms 134776 KB Output is correct
112 Correct 1790 ms 114516 KB Output is correct
113 Correct 1227 ms 141140 KB Output is correct
114 Correct 2321 ms 144260 KB Output is correct
115 Correct 2242 ms 134612 KB Output is correct
116 Correct 2256 ms 119508 KB Output is correct
117 Correct 2233 ms 114516 KB Output is correct
118 Correct 2206 ms 111912 KB Output is correct
119 Correct 2117 ms 105320 KB Output is correct
120 Correct 1058 ms 104276 KB Output is correct
121 Correct 1109 ms 108304 KB Output is correct
122 Correct 1101 ms 108116 KB Output is correct
123 Correct 1176 ms 109396 KB Output is correct
124 Correct 1311 ms 110932 KB Output is correct
125 Correct 1269 ms 109924 KB Output is correct
126 Correct 1506 ms 111316 KB Output is correct