답안 #48851

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48851 2018-05-19T10:28:08 Z model_code 새 집 (APIO18_new_home) C++17
100 / 100
4512 ms 146380 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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, t, a, b;
};

struct Query {
    int x, t;
};

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.t = o.t * 3 + 1;

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

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

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

    vector<Update> vct[2];

    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 });
        if (mid < r) vct[1].push_back({ t1, t2, -r, -(mid + 1), -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].t == c) {
            auto o = a[ci++];
            events.push_back({ o.a, +1, o.t, o.x });
            events.push_back({ o.b, -1, o.t, 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);
    }

    for (int ii = 0; ii < 2; ii++) {
        struct CEvent {
            int t, type, id;
            bool operator< (const CEvent &rhs) const {
                return t < rhs.t;
            }
        };
        static vector<pair<int, int>> vv;
        vv.clear();
        static vector<CEvent> events;
        events.clear();
        for (int i = 0; i < (int)vct[ii].size(); i++) {
            vv.push_back({ vct[ii][i].x2, i });
            events.push_back({ vct[ii][i].t1, 1, i });
            events.push_back({ vct[ii][i].t2, 2, i });
        }
        for (int i = 0; i < m; i++) events.push_back({ b[i].t, 0, i });

        sort(vv.begin(), vv.end());
        Segtree tr(vv.size());
        sort(events.begin(), events.end());

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

        for (auto &o : b) o.x *= -1;
    }

    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].t, &a[i].a, &a[i].b);
        a[i].t--;
    }
    vector<Query> b(q);
    for (int i = 0; i < q; i++) scanf("%d%d", &b[i].x, &b[i].t);

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

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:178: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:181: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].t, &a[i].a, &a[i].b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:185: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].t);
                                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 476 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 4 ms 616 KB Output is correct
6 Correct 6 ms 616 KB Output is correct
7 Correct 5 ms 684 KB Output is correct
8 Correct 4 ms 684 KB Output is correct
9 Correct 5 ms 684 KB Output is correct
10 Correct 5 ms 740 KB Output is correct
11 Correct 4 ms 740 KB Output is correct
12 Correct 4 ms 740 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 5 ms 740 KB Output is correct
15 Correct 5 ms 740 KB Output is correct
16 Correct 4 ms 740 KB Output is correct
17 Correct 5 ms 740 KB Output is correct
18 Correct 6 ms 740 KB Output is correct
19 Correct 5 ms 740 KB Output is correct
20 Correct 5 ms 740 KB Output is correct
21 Correct 4 ms 740 KB Output is correct
22 Correct 4 ms 740 KB Output is correct
23 Correct 6 ms 740 KB Output is correct
24 Correct 4 ms 740 KB Output is correct
25 Correct 4 ms 740 KB Output is correct
26 Correct 3 ms 740 KB Output is correct
27 Correct 5 ms 868 KB Output is correct
28 Correct 4 ms 868 KB Output is correct
29 Correct 4 ms 868 KB Output is correct
30 Correct 4 ms 868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 476 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 4 ms 616 KB Output is correct
6 Correct 6 ms 616 KB Output is correct
7 Correct 5 ms 684 KB Output is correct
8 Correct 4 ms 684 KB Output is correct
9 Correct 5 ms 684 KB Output is correct
10 Correct 5 ms 740 KB Output is correct
11 Correct 4 ms 740 KB Output is correct
12 Correct 4 ms 740 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 5 ms 740 KB Output is correct
15 Correct 5 ms 740 KB Output is correct
16 Correct 4 ms 740 KB Output is correct
17 Correct 5 ms 740 KB Output is correct
18 Correct 6 ms 740 KB Output is correct
19 Correct 5 ms 740 KB Output is correct
20 Correct 5 ms 740 KB Output is correct
21 Correct 4 ms 740 KB Output is correct
22 Correct 4 ms 740 KB Output is correct
23 Correct 6 ms 740 KB Output is correct
24 Correct 4 ms 740 KB Output is correct
25 Correct 4 ms 740 KB Output is correct
26 Correct 3 ms 740 KB Output is correct
27 Correct 5 ms 868 KB Output is correct
28 Correct 4 ms 868 KB Output is correct
29 Correct 4 ms 868 KB Output is correct
30 Correct 4 ms 868 KB Output is correct
31 Correct 615 ms 20816 KB Output is correct
32 Correct 231 ms 20816 KB Output is correct
33 Correct 590 ms 21316 KB Output is correct
34 Correct 591 ms 21316 KB Output is correct
35 Correct 622 ms 21316 KB Output is correct
36 Correct 583 ms 21316 KB Output is correct
37 Correct 533 ms 21316 KB Output is correct
38 Correct 481 ms 21516 KB Output is correct
39 Correct 406 ms 21560 KB Output is correct
40 Correct 439 ms 21812 KB Output is correct
41 Correct 527 ms 21812 KB Output is correct
42 Correct 555 ms 21812 KB Output is correct
43 Correct 139 ms 21812 KB Output is correct
44 Correct 544 ms 21812 KB Output is correct
45 Correct 587 ms 21812 KB Output is correct
46 Correct 517 ms 21812 KB Output is correct
47 Correct 424 ms 21812 KB Output is correct
48 Correct 324 ms 21812 KB Output is correct
49 Correct 368 ms 21812 KB Output is correct
50 Correct 426 ms 21812 KB Output is correct
51 Correct 362 ms 21812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1795 ms 66592 KB Output is correct
2 Correct 1375 ms 66592 KB Output is correct
3 Correct 3181 ms 146292 KB Output is correct
4 Correct 2051 ms 146292 KB Output is correct
5 Correct 1408 ms 146292 KB Output is correct
6 Correct 1421 ms 146292 KB Output is correct
7 Correct 3107 ms 146292 KB Output is correct
8 Correct 2003 ms 146292 KB Output is correct
9 Correct 1665 ms 146292 KB Output is correct
10 Correct 1535 ms 146292 KB Output is correct
11 Correct 1145 ms 146292 KB Output is correct
12 Correct 1193 ms 146292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2887 ms 146292 KB Output is correct
2 Correct 760 ms 146292 KB Output is correct
3 Correct 2854 ms 146292 KB Output is correct
4 Correct 3750 ms 146292 KB Output is correct
5 Correct 2848 ms 146292 KB Output is correct
6 Correct 2833 ms 146292 KB Output is correct
7 Correct 2579 ms 146292 KB Output is correct
8 Correct 2854 ms 146292 KB Output is correct
9 Correct 3281 ms 146292 KB Output is correct
10 Correct 3080 ms 146292 KB Output is correct
11 Correct 2894 ms 146292 KB Output is correct
12 Correct 2538 ms 146292 KB Output is correct
13 Correct 1587 ms 146292 KB Output is correct
14 Correct 1396 ms 146292 KB Output is correct
15 Correct 1606 ms 146292 KB Output is correct
16 Correct 1928 ms 146292 KB Output is correct
17 Correct 2043 ms 146292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 476 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 4 ms 616 KB Output is correct
6 Correct 6 ms 616 KB Output is correct
7 Correct 5 ms 684 KB Output is correct
8 Correct 4 ms 684 KB Output is correct
9 Correct 5 ms 684 KB Output is correct
10 Correct 5 ms 740 KB Output is correct
11 Correct 4 ms 740 KB Output is correct
12 Correct 4 ms 740 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 5 ms 740 KB Output is correct
15 Correct 5 ms 740 KB Output is correct
16 Correct 4 ms 740 KB Output is correct
17 Correct 5 ms 740 KB Output is correct
18 Correct 6 ms 740 KB Output is correct
19 Correct 5 ms 740 KB Output is correct
20 Correct 5 ms 740 KB Output is correct
21 Correct 4 ms 740 KB Output is correct
22 Correct 4 ms 740 KB Output is correct
23 Correct 6 ms 740 KB Output is correct
24 Correct 4 ms 740 KB Output is correct
25 Correct 4 ms 740 KB Output is correct
26 Correct 3 ms 740 KB Output is correct
27 Correct 5 ms 868 KB Output is correct
28 Correct 4 ms 868 KB Output is correct
29 Correct 4 ms 868 KB Output is correct
30 Correct 4 ms 868 KB Output is correct
31 Correct 615 ms 20816 KB Output is correct
32 Correct 231 ms 20816 KB Output is correct
33 Correct 590 ms 21316 KB Output is correct
34 Correct 591 ms 21316 KB Output is correct
35 Correct 622 ms 21316 KB Output is correct
36 Correct 583 ms 21316 KB Output is correct
37 Correct 533 ms 21316 KB Output is correct
38 Correct 481 ms 21516 KB Output is correct
39 Correct 406 ms 21560 KB Output is correct
40 Correct 439 ms 21812 KB Output is correct
41 Correct 527 ms 21812 KB Output is correct
42 Correct 555 ms 21812 KB Output is correct
43 Correct 139 ms 21812 KB Output is correct
44 Correct 544 ms 21812 KB Output is correct
45 Correct 587 ms 21812 KB Output is correct
46 Correct 517 ms 21812 KB Output is correct
47 Correct 424 ms 21812 KB Output is correct
48 Correct 324 ms 21812 KB Output is correct
49 Correct 368 ms 21812 KB Output is correct
50 Correct 426 ms 21812 KB Output is correct
51 Correct 362 ms 21812 KB Output is correct
52 Correct 672 ms 146292 KB Output is correct
53 Correct 694 ms 146292 KB Output is correct
54 Correct 607 ms 146292 KB Output is correct
55 Correct 551 ms 146292 KB Output is correct
56 Correct 577 ms 146292 KB Output is correct
57 Correct 564 ms 146292 KB Output is correct
58 Correct 567 ms 146292 KB Output is correct
59 Correct 590 ms 146292 KB Output is correct
60 Correct 547 ms 146292 KB Output is correct
61 Correct 382 ms 146292 KB Output is correct
62 Correct 618 ms 146292 KB Output is correct
63 Correct 639 ms 146292 KB Output is correct
64 Correct 614 ms 146292 KB Output is correct
65 Correct 618 ms 146292 KB Output is correct
66 Correct 585 ms 146292 KB Output is correct
67 Correct 534 ms 146292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 476 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 4 ms 616 KB Output is correct
6 Correct 6 ms 616 KB Output is correct
7 Correct 5 ms 684 KB Output is correct
8 Correct 4 ms 684 KB Output is correct
9 Correct 5 ms 684 KB Output is correct
10 Correct 5 ms 740 KB Output is correct
11 Correct 4 ms 740 KB Output is correct
12 Correct 4 ms 740 KB Output is correct
13 Correct 5 ms 740 KB Output is correct
14 Correct 5 ms 740 KB Output is correct
15 Correct 5 ms 740 KB Output is correct
16 Correct 4 ms 740 KB Output is correct
17 Correct 5 ms 740 KB Output is correct
18 Correct 6 ms 740 KB Output is correct
19 Correct 5 ms 740 KB Output is correct
20 Correct 5 ms 740 KB Output is correct
21 Correct 4 ms 740 KB Output is correct
22 Correct 4 ms 740 KB Output is correct
23 Correct 6 ms 740 KB Output is correct
24 Correct 4 ms 740 KB Output is correct
25 Correct 4 ms 740 KB Output is correct
26 Correct 3 ms 740 KB Output is correct
27 Correct 5 ms 868 KB Output is correct
28 Correct 4 ms 868 KB Output is correct
29 Correct 4 ms 868 KB Output is correct
30 Correct 4 ms 868 KB Output is correct
31 Correct 615 ms 20816 KB Output is correct
32 Correct 231 ms 20816 KB Output is correct
33 Correct 590 ms 21316 KB Output is correct
34 Correct 591 ms 21316 KB Output is correct
35 Correct 622 ms 21316 KB Output is correct
36 Correct 583 ms 21316 KB Output is correct
37 Correct 533 ms 21316 KB Output is correct
38 Correct 481 ms 21516 KB Output is correct
39 Correct 406 ms 21560 KB Output is correct
40 Correct 439 ms 21812 KB Output is correct
41 Correct 527 ms 21812 KB Output is correct
42 Correct 555 ms 21812 KB Output is correct
43 Correct 139 ms 21812 KB Output is correct
44 Correct 544 ms 21812 KB Output is correct
45 Correct 587 ms 21812 KB Output is correct
46 Correct 517 ms 21812 KB Output is correct
47 Correct 424 ms 21812 KB Output is correct
48 Correct 324 ms 21812 KB Output is correct
49 Correct 368 ms 21812 KB Output is correct
50 Correct 426 ms 21812 KB Output is correct
51 Correct 362 ms 21812 KB Output is correct
52 Correct 1795 ms 66592 KB Output is correct
53 Correct 1375 ms 66592 KB Output is correct
54 Correct 3181 ms 146292 KB Output is correct
55 Correct 2051 ms 146292 KB Output is correct
56 Correct 1408 ms 146292 KB Output is correct
57 Correct 1421 ms 146292 KB Output is correct
58 Correct 3107 ms 146292 KB Output is correct
59 Correct 2003 ms 146292 KB Output is correct
60 Correct 1665 ms 146292 KB Output is correct
61 Correct 1535 ms 146292 KB Output is correct
62 Correct 1145 ms 146292 KB Output is correct
63 Correct 1193 ms 146292 KB Output is correct
64 Correct 2887 ms 146292 KB Output is correct
65 Correct 760 ms 146292 KB Output is correct
66 Correct 2854 ms 146292 KB Output is correct
67 Correct 3750 ms 146292 KB Output is correct
68 Correct 2848 ms 146292 KB Output is correct
69 Correct 2833 ms 146292 KB Output is correct
70 Correct 2579 ms 146292 KB Output is correct
71 Correct 2854 ms 146292 KB Output is correct
72 Correct 3281 ms 146292 KB Output is correct
73 Correct 3080 ms 146292 KB Output is correct
74 Correct 2894 ms 146292 KB Output is correct
75 Correct 2538 ms 146292 KB Output is correct
76 Correct 1587 ms 146292 KB Output is correct
77 Correct 1396 ms 146292 KB Output is correct
78 Correct 1606 ms 146292 KB Output is correct
79 Correct 1928 ms 146292 KB Output is correct
80 Correct 2043 ms 146292 KB Output is correct
81 Correct 672 ms 146292 KB Output is correct
82 Correct 694 ms 146292 KB Output is correct
83 Correct 607 ms 146292 KB Output is correct
84 Correct 551 ms 146292 KB Output is correct
85 Correct 577 ms 146292 KB Output is correct
86 Correct 564 ms 146292 KB Output is correct
87 Correct 567 ms 146292 KB Output is correct
88 Correct 590 ms 146292 KB Output is correct
89 Correct 547 ms 146292 KB Output is correct
90 Correct 382 ms 146292 KB Output is correct
91 Correct 618 ms 146292 KB Output is correct
92 Correct 639 ms 146292 KB Output is correct
93 Correct 614 ms 146292 KB Output is correct
94 Correct 618 ms 146292 KB Output is correct
95 Correct 585 ms 146292 KB Output is correct
96 Correct 534 ms 146292 KB Output is correct
97 Correct 4168 ms 146380 KB Output is correct
98 Correct 754 ms 146380 KB Output is correct
99 Correct 3743 ms 146380 KB Output is correct
100 Correct 4512 ms 146380 KB Output is correct
101 Correct 4349 ms 146380 KB Output is correct
102 Correct 4075 ms 146380 KB Output is correct
103 Correct 2798 ms 146380 KB Output is correct
104 Correct 2822 ms 146380 KB Output is correct
105 Correct 2283 ms 146380 KB Output is correct
106 Correct 2313 ms 146380 KB Output is correct
107 Correct 3377 ms 146380 KB Output is correct
108 Correct 3456 ms 146380 KB Output is correct
109 Correct 3784 ms 146380 KB Output is correct
110 Correct 4074 ms 146380 KB Output is correct
111 Correct 4119 ms 146380 KB Output is correct
112 Correct 3673 ms 146380 KB Output is correct
113 Correct 2027 ms 146380 KB Output is correct
114 Correct 4038 ms 146380 KB Output is correct
115 Correct 4085 ms 146380 KB Output is correct
116 Correct 3941 ms 146380 KB Output is correct
117 Correct 3901 ms 146380 KB Output is correct
118 Correct 3758 ms 146380 KB Output is correct
119 Correct 3342 ms 146380 KB Output is correct
120 Correct 1598 ms 146380 KB Output is correct
121 Correct 1648 ms 146380 KB Output is correct
122 Correct 1580 ms 146380 KB Output is correct
123 Correct 1929 ms 146380 KB Output is correct
124 Correct 2353 ms 146380 KB Output is correct
125 Correct 2136 ms 146380 KB Output is correct
126 Correct 2336 ms 146380 KB Output is correct