답안 #48847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48847 2018-05-19T10:27:31 Z model_code 새 집 (APIO18_new_home) C++17
12 / 100
5000 ms 34816 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 Shop {
    int x, type, a, b;
};

struct Query {
    int x, time;
};

struct fenwick {
    int n;
    vector<int> f;

    fenwick() {}

    fenwick(int N) {
        n = N;
        f.resize(n);
    }

    void add(int x, int y) {
        for (; x < n; x |= x + 1) f[x] += y;
    }

    int get(int x) {
        int res = 0;
        for (; x >= 0; x = (x & (x + 1)) - 1) res += f[x];
        return res;
    }

    int get(int l, int r) {
        return get(r) - get(l - 1);
    }

    // return min i: sum(0, i) > x
    int upper_bound(int x) {
        int k = 1;
        while (2 * k <= n) k *= 2;

        int res = 0;
        while (k > 0) {
            if (res + k - 1 < n && f[res + k - 1] <= x) {
                x -= f[res + k - 1];
                res += k;
            }
            k /= 2;
        }
        return res;
    }
};

vector<int> slow(vector<Shop> a, int k, vector<Query> b) {
    int n = a.size(), m = b.size();
    vector<int> ans(m);
    struct Event {
        int time, type, id;
        bool operator< (const Event &ev) const {
            return make_pair(time, -type) < make_pair(ev.time, -ev.type);
        }
    };
    vector<Event> events;
    for (int i = 0; i < (int)a.size(); i++) {
        events.push_back({ a[i].a, +1, i });
        events.push_back({ a[i].b, -1, i });
    }
    for (int i = 0; i < (int)b.size(); i++) {
        events.push_back({ b[i].time, 0, i });
    }
    sort(events.begin(), events.end());

    vector<int> sum(k);
    vector<vector<int>> vct(k);
    vector<fenwick> f(k);
    for (auto o : a) vct[o.type].push_back(o.x);
    for (int i = 0; i < k; i++) {
        sort(vct[i].begin(), vct[i].end());
        vct[i].resize(unique(vct[i].begin(), vct[i].end()) - vct[i].begin());
        f[i] = fenwick(vct[i].size());
    }

    auto lb2 = [&](int tp, int cx) {
        int id = lower_bound(vct[tp].begin(), vct[tp].end(), cx) - vct[tp].begin();
        int left = f[tp].get(id - 1);
        int ans2 = f[tp].upper_bound(left);
        int ans1 = left ? f[tp].upper_bound(left - 1) : -1;
        return make_pair(ans1, ans2);
    };
  

    int empty = k;
    for (auto ev : events) {        
        if (ev.type == +1) {
            int tp = a[ev.id].type;
            int id = lower_bound(vct[tp].begin(), vct[tp].end(), a[ev.id].x) - vct[tp].begin();
            empty -= sum[tp]++ == 0;
            f[tp].add(id, +1);
        }
        if (ev.type == 0) {
            int cur = 0;
            if (empty) cur = INF;
            for (int i = 0; i < k; i++) {
                int ccur = INF;
                auto o = lb2(i, b[ev.id].x);
                if (0 <= o.first && o.first < (int)vct[i].size()) ccur = min(ccur, abs(b[ev.id].x - vct[i][o.first]));
                if (0 <= o.second && o.second < (int)vct[i].size()) ccur = min(ccur, abs(b[ev.id].x - vct[i][o.second]));
                cur = max(cur, ccur);
                if (cur >= INF / 2) break;
            }
            if (cur >= INF / 2) cur = -1;
            ans[ev.id] = cur;
        }
        if (ev.type == -1) {
            int tp = a[ev.id].type;
            int id = lower_bound(vct[tp].begin(), vct[tp].end(), a[ev.id].x) - vct[tp].begin();
            f[tp].add(id, -1);
            empty += --sum[tp] == 0;
        }
    }
    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(slow(a, k, b));
}

Compilation message

new_home.cpp: In function 'std::vector<int> slow(std::vector<Shop>, int, std::vector<Query>)':
new_home.cpp:60:9: warning: unused variable 'n' [-Wunused-variable]
     int n = a.size(), m = b.size();
         ^
new_home.cpp: In function 'int main()':
new_home.cpp:131: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:134: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:138: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 3 ms 356 KB Output is correct
3 Correct 2 ms 392 KB Output is correct
4 Correct 3 ms 392 KB Output is correct
5 Correct 3 ms 440 KB Output is correct
6 Correct 4 ms 492 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 3 ms 620 KB Output is correct
10 Correct 4 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 4 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 5 ms 620 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 4 ms 620 KB Output is correct
20 Correct 4 ms 620 KB Output is correct
21 Correct 4 ms 620 KB Output is correct
22 Correct 4 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 4 ms 620 KB Output is correct
25 Correct 5 ms 620 KB Output is correct
26 Correct 3 ms 620 KB Output is correct
27 Correct 3 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 3 ms 620 KB Output is correct
30 Correct 3 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 356 KB Output is correct
3 Correct 2 ms 392 KB Output is correct
4 Correct 3 ms 392 KB Output is correct
5 Correct 3 ms 440 KB Output is correct
6 Correct 4 ms 492 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 3 ms 620 KB Output is correct
10 Correct 4 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 4 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 5 ms 620 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 4 ms 620 KB Output is correct
20 Correct 4 ms 620 KB Output is correct
21 Correct 4 ms 620 KB Output is correct
22 Correct 4 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 4 ms 620 KB Output is correct
25 Correct 5 ms 620 KB Output is correct
26 Correct 3 ms 620 KB Output is correct
27 Correct 3 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 3 ms 620 KB Output is correct
30 Correct 3 ms 620 KB Output is correct
31 Correct 4042 ms 6736 KB Output is correct
32 Correct 83 ms 6840 KB Output is correct
33 Correct 388 ms 6856 KB Output is correct
34 Correct 3924 ms 6864 KB Output is correct
35 Correct 1718 ms 6864 KB Output is correct
36 Correct 358 ms 6864 KB Output is correct
37 Correct 441 ms 6864 KB Output is correct
38 Correct 188 ms 6864 KB Output is correct
39 Correct 179 ms 6864 KB Output is correct
40 Correct 159 ms 6964 KB Output is correct
41 Correct 2600 ms 6964 KB Output is correct
42 Correct 1945 ms 7008 KB Output is correct
43 Correct 251 ms 7008 KB Output is correct
44 Correct 2670 ms 7008 KB Output is correct
45 Correct 872 ms 7008 KB Output is correct
46 Correct 227 ms 7008 KB Output is correct
47 Correct 136 ms 7008 KB Output is correct
48 Correct 125 ms 7008 KB Output is correct
49 Correct 222 ms 7008 KB Output is correct
50 Correct 838 ms 7008 KB Output is correct
51 Correct 229 ms 7008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5025 ms 34816 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5024 ms 34816 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 356 KB Output is correct
3 Correct 2 ms 392 KB Output is correct
4 Correct 3 ms 392 KB Output is correct
5 Correct 3 ms 440 KB Output is correct
6 Correct 4 ms 492 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 3 ms 620 KB Output is correct
10 Correct 4 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 4 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 5 ms 620 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 4 ms 620 KB Output is correct
20 Correct 4 ms 620 KB Output is correct
21 Correct 4 ms 620 KB Output is correct
22 Correct 4 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 4 ms 620 KB Output is correct
25 Correct 5 ms 620 KB Output is correct
26 Correct 3 ms 620 KB Output is correct
27 Correct 3 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 3 ms 620 KB Output is correct
30 Correct 3 ms 620 KB Output is correct
31 Correct 4042 ms 6736 KB Output is correct
32 Correct 83 ms 6840 KB Output is correct
33 Correct 388 ms 6856 KB Output is correct
34 Correct 3924 ms 6864 KB Output is correct
35 Correct 1718 ms 6864 KB Output is correct
36 Correct 358 ms 6864 KB Output is correct
37 Correct 441 ms 6864 KB Output is correct
38 Correct 188 ms 6864 KB Output is correct
39 Correct 179 ms 6864 KB Output is correct
40 Correct 159 ms 6964 KB Output is correct
41 Correct 2600 ms 6964 KB Output is correct
42 Correct 1945 ms 7008 KB Output is correct
43 Correct 251 ms 7008 KB Output is correct
44 Correct 2670 ms 7008 KB Output is correct
45 Correct 872 ms 7008 KB Output is correct
46 Correct 227 ms 7008 KB Output is correct
47 Correct 136 ms 7008 KB Output is correct
48 Correct 125 ms 7008 KB Output is correct
49 Correct 222 ms 7008 KB Output is correct
50 Correct 838 ms 7008 KB Output is correct
51 Correct 229 ms 7008 KB Output is correct
52 Correct 133 ms 34816 KB Output is correct
53 Correct 127 ms 34816 KB Output is correct
54 Correct 138 ms 34816 KB Output is correct
55 Execution timed out 5007 ms 34816 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 3 ms 356 KB Output is correct
3 Correct 2 ms 392 KB Output is correct
4 Correct 3 ms 392 KB Output is correct
5 Correct 3 ms 440 KB Output is correct
6 Correct 4 ms 492 KB Output is correct
7 Correct 3 ms 492 KB Output is correct
8 Correct 3 ms 620 KB Output is correct
9 Correct 3 ms 620 KB Output is correct
10 Correct 4 ms 620 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 4 ms 620 KB Output is correct
13 Correct 3 ms 620 KB Output is correct
14 Correct 3 ms 620 KB Output is correct
15 Correct 5 ms 620 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 3 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 4 ms 620 KB Output is correct
20 Correct 4 ms 620 KB Output is correct
21 Correct 4 ms 620 KB Output is correct
22 Correct 4 ms 620 KB Output is correct
23 Correct 3 ms 620 KB Output is correct
24 Correct 4 ms 620 KB Output is correct
25 Correct 5 ms 620 KB Output is correct
26 Correct 3 ms 620 KB Output is correct
27 Correct 3 ms 620 KB Output is correct
28 Correct 3 ms 620 KB Output is correct
29 Correct 3 ms 620 KB Output is correct
30 Correct 3 ms 620 KB Output is correct
31 Correct 4042 ms 6736 KB Output is correct
32 Correct 83 ms 6840 KB Output is correct
33 Correct 388 ms 6856 KB Output is correct
34 Correct 3924 ms 6864 KB Output is correct
35 Correct 1718 ms 6864 KB Output is correct
36 Correct 358 ms 6864 KB Output is correct
37 Correct 441 ms 6864 KB Output is correct
38 Correct 188 ms 6864 KB Output is correct
39 Correct 179 ms 6864 KB Output is correct
40 Correct 159 ms 6964 KB Output is correct
41 Correct 2600 ms 6964 KB Output is correct
42 Correct 1945 ms 7008 KB Output is correct
43 Correct 251 ms 7008 KB Output is correct
44 Correct 2670 ms 7008 KB Output is correct
45 Correct 872 ms 7008 KB Output is correct
46 Correct 227 ms 7008 KB Output is correct
47 Correct 136 ms 7008 KB Output is correct
48 Correct 125 ms 7008 KB Output is correct
49 Correct 222 ms 7008 KB Output is correct
50 Correct 838 ms 7008 KB Output is correct
51 Correct 229 ms 7008 KB Output is correct
52 Execution timed out 5025 ms 34816 KB Time limit exceeded
53 Halted 0 ms 0 KB -