답안 #48844

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48844 2018-05-19T10:25:59 Z model_code 새 집 (APIO18_new_home) C++17
100 / 100
2557 ms 128940 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 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 2 ms 432 KB Output is correct
5 Correct 4 ms 636 KB Output is correct
6 Correct 4 ms 840 KB Output is correct
7 Correct 3 ms 840 KB Output is correct
8 Correct 3 ms 840 KB Output is correct
9 Correct 3 ms 840 KB Output is correct
10 Correct 4 ms 840 KB Output is correct
11 Correct 3 ms 880 KB Output is correct
12 Correct 3 ms 880 KB Output is correct
13 Correct 3 ms 880 KB Output is correct
14 Correct 5 ms 880 KB Output is correct
15 Correct 4 ms 880 KB Output is correct
16 Correct 5 ms 880 KB Output is correct
17 Correct 3 ms 880 KB Output is correct
18 Correct 4 ms 880 KB Output is correct
19 Correct 3 ms 880 KB Output is correct
20 Correct 4 ms 880 KB Output is correct
21 Correct 4 ms 880 KB Output is correct
22 Correct 5 ms 880 KB Output is correct
23 Correct 4 ms 880 KB Output is correct
24 Correct 5 ms 880 KB Output is correct
25 Correct 4 ms 880 KB Output is correct
26 Correct 4 ms 880 KB Output is correct
27 Correct 4 ms 880 KB Output is correct
28 Correct 4 ms 880 KB Output is correct
29 Correct 4 ms 880 KB Output is correct
30 Correct 3 ms 880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 2 ms 432 KB Output is correct
5 Correct 4 ms 636 KB Output is correct
6 Correct 4 ms 840 KB Output is correct
7 Correct 3 ms 840 KB Output is correct
8 Correct 3 ms 840 KB Output is correct
9 Correct 3 ms 840 KB Output is correct
10 Correct 4 ms 840 KB Output is correct
11 Correct 3 ms 880 KB Output is correct
12 Correct 3 ms 880 KB Output is correct
13 Correct 3 ms 880 KB Output is correct
14 Correct 5 ms 880 KB Output is correct
15 Correct 4 ms 880 KB Output is correct
16 Correct 5 ms 880 KB Output is correct
17 Correct 3 ms 880 KB Output is correct
18 Correct 4 ms 880 KB Output is correct
19 Correct 3 ms 880 KB Output is correct
20 Correct 4 ms 880 KB Output is correct
21 Correct 4 ms 880 KB Output is correct
22 Correct 5 ms 880 KB Output is correct
23 Correct 4 ms 880 KB Output is correct
24 Correct 5 ms 880 KB Output is correct
25 Correct 4 ms 880 KB Output is correct
26 Correct 4 ms 880 KB Output is correct
27 Correct 4 ms 880 KB Output is correct
28 Correct 4 ms 880 KB Output is correct
29 Correct 4 ms 880 KB Output is correct
30 Correct 3 ms 880 KB Output is correct
31 Correct 504 ms 20328 KB Output is correct
32 Correct 153 ms 20328 KB Output is correct
33 Correct 356 ms 20432 KB Output is correct
34 Correct 410 ms 20444 KB Output is correct
35 Correct 379 ms 20444 KB Output is correct
36 Correct 412 ms 20532 KB Output is correct
37 Correct 330 ms 20592 KB Output is correct
38 Correct 316 ms 21044 KB Output is correct
39 Correct 261 ms 21044 KB Output is correct
40 Correct 268 ms 21044 KB Output is correct
41 Correct 361 ms 21044 KB Output is correct
42 Correct 413 ms 21044 KB Output is correct
43 Correct 115 ms 21044 KB Output is correct
44 Correct 300 ms 21044 KB Output is correct
45 Correct 369 ms 21044 KB Output is correct
46 Correct 282 ms 21044 KB Output is correct
47 Correct 232 ms 21044 KB Output is correct
48 Correct 294 ms 21044 KB Output is correct
49 Correct 240 ms 21044 KB Output is correct
50 Correct 236 ms 21044 KB Output is correct
51 Correct 208 ms 21044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1128 ms 61724 KB Output is correct
2 Correct 1211 ms 61724 KB Output is correct
3 Correct 1828 ms 128724 KB Output is correct
4 Correct 1345 ms 128724 KB Output is correct
5 Correct 1734 ms 128724 KB Output is correct
6 Correct 1670 ms 128724 KB Output is correct
7 Correct 1863 ms 128792 KB Output is correct
8 Correct 1278 ms 128792 KB Output is correct
9 Correct 1035 ms 128792 KB Output is correct
10 Correct 1154 ms 128792 KB Output is correct
11 Correct 907 ms 128792 KB Output is correct
12 Correct 937 ms 128792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1740 ms 128792 KB Output is correct
2 Correct 718 ms 128792 KB Output is correct
3 Correct 1862 ms 128792 KB Output is correct
4 Correct 2144 ms 128792 KB Output is correct
5 Correct 1764 ms 128792 KB Output is correct
6 Correct 1921 ms 128792 KB Output is correct
7 Correct 2241 ms 128792 KB Output is correct
8 Correct 1721 ms 128792 KB Output is correct
9 Correct 1787 ms 128792 KB Output is correct
10 Correct 1690 ms 128792 KB Output is correct
11 Correct 1646 ms 128792 KB Output is correct
12 Correct 1493 ms 128792 KB Output is correct
13 Correct 1099 ms 128792 KB Output is correct
14 Correct 1104 ms 128792 KB Output is correct
15 Correct 1098 ms 128792 KB Output is correct
16 Correct 1175 ms 128792 KB Output is correct
17 Correct 1202 ms 128792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 2 ms 432 KB Output is correct
5 Correct 4 ms 636 KB Output is correct
6 Correct 4 ms 840 KB Output is correct
7 Correct 3 ms 840 KB Output is correct
8 Correct 3 ms 840 KB Output is correct
9 Correct 3 ms 840 KB Output is correct
10 Correct 4 ms 840 KB Output is correct
11 Correct 3 ms 880 KB Output is correct
12 Correct 3 ms 880 KB Output is correct
13 Correct 3 ms 880 KB Output is correct
14 Correct 5 ms 880 KB Output is correct
15 Correct 4 ms 880 KB Output is correct
16 Correct 5 ms 880 KB Output is correct
17 Correct 3 ms 880 KB Output is correct
18 Correct 4 ms 880 KB Output is correct
19 Correct 3 ms 880 KB Output is correct
20 Correct 4 ms 880 KB Output is correct
21 Correct 4 ms 880 KB Output is correct
22 Correct 5 ms 880 KB Output is correct
23 Correct 4 ms 880 KB Output is correct
24 Correct 5 ms 880 KB Output is correct
25 Correct 4 ms 880 KB Output is correct
26 Correct 4 ms 880 KB Output is correct
27 Correct 4 ms 880 KB Output is correct
28 Correct 4 ms 880 KB Output is correct
29 Correct 4 ms 880 KB Output is correct
30 Correct 3 ms 880 KB Output is correct
31 Correct 504 ms 20328 KB Output is correct
32 Correct 153 ms 20328 KB Output is correct
33 Correct 356 ms 20432 KB Output is correct
34 Correct 410 ms 20444 KB Output is correct
35 Correct 379 ms 20444 KB Output is correct
36 Correct 412 ms 20532 KB Output is correct
37 Correct 330 ms 20592 KB Output is correct
38 Correct 316 ms 21044 KB Output is correct
39 Correct 261 ms 21044 KB Output is correct
40 Correct 268 ms 21044 KB Output is correct
41 Correct 361 ms 21044 KB Output is correct
42 Correct 413 ms 21044 KB Output is correct
43 Correct 115 ms 21044 KB Output is correct
44 Correct 300 ms 21044 KB Output is correct
45 Correct 369 ms 21044 KB Output is correct
46 Correct 282 ms 21044 KB Output is correct
47 Correct 232 ms 21044 KB Output is correct
48 Correct 294 ms 21044 KB Output is correct
49 Correct 240 ms 21044 KB Output is correct
50 Correct 236 ms 21044 KB Output is correct
51 Correct 208 ms 21044 KB Output is correct
52 Correct 358 ms 128792 KB Output is correct
53 Correct 319 ms 128792 KB Output is correct
54 Correct 320 ms 128792 KB Output is correct
55 Correct 307 ms 128792 KB Output is correct
56 Correct 297 ms 128792 KB Output is correct
57 Correct 308 ms 128792 KB Output is correct
58 Correct 303 ms 128792 KB Output is correct
59 Correct 306 ms 128792 KB Output is correct
60 Correct 299 ms 128792 KB Output is correct
61 Correct 248 ms 128792 KB Output is correct
62 Correct 416 ms 128792 KB Output is correct
63 Correct 342 ms 128792 KB Output is correct
64 Correct 359 ms 128792 KB Output is correct
65 Correct 306 ms 128792 KB Output is correct
66 Correct 289 ms 128792 KB Output is correct
67 Correct 265 ms 128792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 432 KB Output is correct
4 Correct 2 ms 432 KB Output is correct
5 Correct 4 ms 636 KB Output is correct
6 Correct 4 ms 840 KB Output is correct
7 Correct 3 ms 840 KB Output is correct
8 Correct 3 ms 840 KB Output is correct
9 Correct 3 ms 840 KB Output is correct
10 Correct 4 ms 840 KB Output is correct
11 Correct 3 ms 880 KB Output is correct
12 Correct 3 ms 880 KB Output is correct
13 Correct 3 ms 880 KB Output is correct
14 Correct 5 ms 880 KB Output is correct
15 Correct 4 ms 880 KB Output is correct
16 Correct 5 ms 880 KB Output is correct
17 Correct 3 ms 880 KB Output is correct
18 Correct 4 ms 880 KB Output is correct
19 Correct 3 ms 880 KB Output is correct
20 Correct 4 ms 880 KB Output is correct
21 Correct 4 ms 880 KB Output is correct
22 Correct 5 ms 880 KB Output is correct
23 Correct 4 ms 880 KB Output is correct
24 Correct 5 ms 880 KB Output is correct
25 Correct 4 ms 880 KB Output is correct
26 Correct 4 ms 880 KB Output is correct
27 Correct 4 ms 880 KB Output is correct
28 Correct 4 ms 880 KB Output is correct
29 Correct 4 ms 880 KB Output is correct
30 Correct 3 ms 880 KB Output is correct
31 Correct 504 ms 20328 KB Output is correct
32 Correct 153 ms 20328 KB Output is correct
33 Correct 356 ms 20432 KB Output is correct
34 Correct 410 ms 20444 KB Output is correct
35 Correct 379 ms 20444 KB Output is correct
36 Correct 412 ms 20532 KB Output is correct
37 Correct 330 ms 20592 KB Output is correct
38 Correct 316 ms 21044 KB Output is correct
39 Correct 261 ms 21044 KB Output is correct
40 Correct 268 ms 21044 KB Output is correct
41 Correct 361 ms 21044 KB Output is correct
42 Correct 413 ms 21044 KB Output is correct
43 Correct 115 ms 21044 KB Output is correct
44 Correct 300 ms 21044 KB Output is correct
45 Correct 369 ms 21044 KB Output is correct
46 Correct 282 ms 21044 KB Output is correct
47 Correct 232 ms 21044 KB Output is correct
48 Correct 294 ms 21044 KB Output is correct
49 Correct 240 ms 21044 KB Output is correct
50 Correct 236 ms 21044 KB Output is correct
51 Correct 208 ms 21044 KB Output is correct
52 Correct 1128 ms 61724 KB Output is correct
53 Correct 1211 ms 61724 KB Output is correct
54 Correct 1828 ms 128724 KB Output is correct
55 Correct 1345 ms 128724 KB Output is correct
56 Correct 1734 ms 128724 KB Output is correct
57 Correct 1670 ms 128724 KB Output is correct
58 Correct 1863 ms 128792 KB Output is correct
59 Correct 1278 ms 128792 KB Output is correct
60 Correct 1035 ms 128792 KB Output is correct
61 Correct 1154 ms 128792 KB Output is correct
62 Correct 907 ms 128792 KB Output is correct
63 Correct 937 ms 128792 KB Output is correct
64 Correct 1740 ms 128792 KB Output is correct
65 Correct 718 ms 128792 KB Output is correct
66 Correct 1862 ms 128792 KB Output is correct
67 Correct 2144 ms 128792 KB Output is correct
68 Correct 1764 ms 128792 KB Output is correct
69 Correct 1921 ms 128792 KB Output is correct
70 Correct 2241 ms 128792 KB Output is correct
71 Correct 1721 ms 128792 KB Output is correct
72 Correct 1787 ms 128792 KB Output is correct
73 Correct 1690 ms 128792 KB Output is correct
74 Correct 1646 ms 128792 KB Output is correct
75 Correct 1493 ms 128792 KB Output is correct
76 Correct 1099 ms 128792 KB Output is correct
77 Correct 1104 ms 128792 KB Output is correct
78 Correct 1098 ms 128792 KB Output is correct
79 Correct 1175 ms 128792 KB Output is correct
80 Correct 1202 ms 128792 KB Output is correct
81 Correct 358 ms 128792 KB Output is correct
82 Correct 319 ms 128792 KB Output is correct
83 Correct 320 ms 128792 KB Output is correct
84 Correct 307 ms 128792 KB Output is correct
85 Correct 297 ms 128792 KB Output is correct
86 Correct 308 ms 128792 KB Output is correct
87 Correct 303 ms 128792 KB Output is correct
88 Correct 306 ms 128792 KB Output is correct
89 Correct 299 ms 128792 KB Output is correct
90 Correct 248 ms 128792 KB Output is correct
91 Correct 416 ms 128792 KB Output is correct
92 Correct 342 ms 128792 KB Output is correct
93 Correct 359 ms 128792 KB Output is correct
94 Correct 306 ms 128792 KB Output is correct
95 Correct 289 ms 128792 KB Output is correct
96 Correct 265 ms 128792 KB Output is correct
97 Correct 2400 ms 128792 KB Output is correct
98 Correct 811 ms 128792 KB Output is correct
99 Correct 2545 ms 128792 KB Output is correct
100 Correct 2392 ms 128792 KB Output is correct
101 Correct 2359 ms 128792 KB Output is correct
102 Correct 2557 ms 128792 KB Output is correct
103 Correct 1818 ms 128792 KB Output is correct
104 Correct 1921 ms 128792 KB Output is correct
105 Correct 1520 ms 128792 KB Output is correct
106 Correct 1632 ms 128792 KB Output is correct
107 Correct 2001 ms 128792 KB Output is correct
108 Correct 2113 ms 128792 KB Output is correct
109 Correct 1861 ms 128792 KB Output is correct
110 Correct 1949 ms 128792 KB Output is correct
111 Correct 2098 ms 128792 KB Output is correct
112 Correct 2059 ms 128792 KB Output is correct
113 Correct 1273 ms 128792 KB Output is correct
114 Correct 2305 ms 128940 KB Output is correct
115 Correct 2229 ms 128940 KB Output is correct
116 Correct 2077 ms 128940 KB Output is correct
117 Correct 2189 ms 128940 KB Output is correct
118 Correct 1822 ms 128940 KB Output is correct
119 Correct 1984 ms 128940 KB Output is correct
120 Correct 1145 ms 128940 KB Output is correct
121 Correct 1198 ms 128940 KB Output is correct
122 Correct 1123 ms 128940 KB Output is correct
123 Correct 1285 ms 128940 KB Output is correct
124 Correct 1362 ms 128940 KB Output is correct
125 Correct 1359 ms 128940 KB Output is correct
126 Correct 1448 ms 128940 KB Output is correct