답안 #49047

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
49047 2018-05-21T13:34:17 Z BThero 새 집 (APIO18_new_home) C++17
12 / 100
3594 ms 295688 KB
// Why am I so stupid? :c
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

typedef long long ll;

using namespace std;

const int MAXN = 3e5+5;

const int INF = 1e9;

struct shop {
    int x, tp, l, r;

    shop() {
        x = tp = l = r = 0;
    }
} arr[MAXN];

struct que {
    int x, t, id;

    que() {
        x = t = id = 0;
    }
} req[MAXN];

vector < pair <int, int> > addEl[MAXN * 3], delEl[MAXN * 3], reqEl[MAXN * 3];

multiset <int> ms[MAXN * 3];

vector <int> mm[MAXN];

int rl[MAXN * 3];

int ans[MAXN];

int mn[MAXN];

int n, k, q;

int m, m2;

void compressTime() {
    vector <int> vv;

    for (int i = 1; i <= n; ++i) {
        vv.pb(arr[i].l);
        vv.pb(arr[i].r);
    }

    for (int i = 1; i <= q; ++i) {
        vv.pb(req[i].t);
    }

    sort(all(vv));
    vv.resize(unique(all(vv)) - vv.begin());

    m = vv.size();

    for (int i = 1; i <= n; ++i) {
        arr[i].l = upper_bound(all(vv), arr[i].l) - vv.begin();
        arr[i].r = upper_bound(all(vv), arr[i].r) - vv.begin();
    }

    for (int i = 1; i <= q; ++i) {
        req[i].t = upper_bound(all(vv), req[i].t) - vv.begin();
    }
}

void compressX() {
    vector <int> vv;

    for (int i = 1; i <= n; ++i) {
        vv.pb(arr[i].x);
    }

    for (int i = 1; i <= q; ++i) {
        vv.pb(req[i].x);
    }

    sort(all(vv));
    vv.resize(unique(all(vv)) - vv.begin());

    m2 = vv.size();

    for (int i = 1, ps; i <= n; ++i) {
        ps = upper_bound(all(vv), arr[i].x) - vv.begin();
        rl[ps] = arr[i].x;
        arr[i].x = ps;
    }

    for (int i = 1, ps; i <= q; ++i) {
        ps = upper_bound(all(vv), req[i].x) - vv.begin();
        rl[ps] = req[i].x;
        req[i].x = ps;
    }
}

int dist(int x, multiset <int> &M) {
    auto it = M.upper_bound(x);
    int ret = INF;

    if (it != M.end()) {
        ret = min(ret, rl[*it] - rl[x]);
    }

    if (it != M.begin()) {
        ret = min(ret, rl[x] - rl[*(--it)]);
    }

    return ret;
}

bool cmp(que a, que b) {
    return a.x < b.x;
}

void solve() {
    scanf("%d %d %d", &n, &k, &q);

    for (int i = 1; i <= n; ++i) {
        scanf("%d %d", &arr[i].x, &arr[i].tp);
        scanf("%d %d", &arr[i].l, &arr[i].r);
    }

    for (int i = 1; i <= q; ++i) {
        scanf("%d %d", &req[i].x, &req[i].t);
        req[i].id = i;
    }

    compressX();

    if (k <= 400) {
        compressTime();

        for (int i = 1; i <= n; ++i) {
            addEl[arr[i].l].pb(mp(arr[i].x, arr[i].tp));
            delEl[arr[i].r + 1].pb(mp(arr[i].x, arr[i].tp));
        }

        for (int i = 1; i <= q; ++i) {
            reqEl[req[i].t].pb(mp(req[i].x, i));
        }

        for (int i = 1; i <= m; ++i) {
            for (auto it : addEl[i]) {
                ms[it.se].insert(it.fi);
            }

            for (auto it : delEl[i]) {
                ms[it.se].erase(ms[it.se].find(it.fi));
            }

            for (auto it : reqEl[i]) {
                for (int j = 1; j <= k; ++j) {
                    ans[it.se] = max(ans[it.se], dist(it.fi, ms[j]));
                }
            }
        }
    }
    else {
        sort(req + 1, req + q + 1, cmp);

        for (int i = 1; i <= n; ++i) {
            mm[arr[i].tp].pb(arr[i].x);
        }

        bool ex = 0;

        for (int i = 1; i <= k; ++i) {
            if (mm[i].empty()) {
                ex = 1;
            }

            sort(all(mm[i]));
        }

        if (ex) {
            for (int i = 1; i <= q; ++i) {
                printf("-1\n");
            }

            return;
        }

        for (int i = 1; i <= k; ++i) {
            mm[i].pb(1);

            for (int j = 0; j < mm[i].size(); ++j) {
                int nxt = (j + 1 < mm[i].size() ? mm[i][j + 1] : m2);

                addEl[mm[i][j]].pb(mp(mm[i][j], nxt));
                delEl[nxt + 1].pb(mp(mm[i][j], nxt));
            }
        }

        multiset < pair <int, int> > yu;
        int ptr = 1;

        for (int i = 1; i <= m2; ++i) {
            for (auto it : addEl[i]) {
                yu.insert(it);
            }

            for (auto it : delEl[i]) {
                yu.erase(yu.find(it));
            }

            while (ptr <= q && req[ptr].x <= i) {
                for (auto it : yu) {
                    int curD = min(rl[req[ptr].x] - rl[it.fi], rl[it.se] - rl[req[ptr].x]);
                    ans[req[ptr].id] = max(ans[req[ptr].id], curD);
                }

                ++ptr;
            }
        }
    }

    for (int i = 1; i <= q; ++i) {
        if (ans[i] == INF) {
            ans[i] = -1;
        }

        printf("%d\n", ans[i]);
    }
}

int main() {
#ifdef BThero
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // BThero

    int tt = 1;

    while (tt--) {
        solve();
    }

    return 0;
}

Compilation message

new_home.cpp: In function 'void solve()':
new_home.cpp:198:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < mm[i].size(); ++j) {
                             ~~^~~~~~~~~~~~~~
new_home.cpp:199:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 int nxt = (j + 1 < mm[i].size() ? mm[i][j + 1] : m2);
                            ~~~~~~^~~~~~~~~~~~~~
new_home.cpp:128: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:131:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &arr[i].x, &arr[i].tp);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:132:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &arr[i].l, &arr[i].r);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &req[i].x, &req[i].t);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 121304 KB Output is correct
2 Correct 130 ms 121316 KB Output is correct
3 Correct 107 ms 121388 KB Output is correct
4 Correct 117 ms 121416 KB Output is correct
5 Correct 125 ms 121416 KB Output is correct
6 Correct 103 ms 121440 KB Output is correct
7 Correct 119 ms 121444 KB Output is correct
8 Correct 133 ms 121444 KB Output is correct
9 Correct 107 ms 121460 KB Output is correct
10 Correct 100 ms 121460 KB Output is correct
11 Correct 101 ms 121464 KB Output is correct
12 Correct 98 ms 121500 KB Output is correct
13 Correct 99 ms 121532 KB Output is correct
14 Correct 95 ms 121532 KB Output is correct
15 Correct 103 ms 121532 KB Output is correct
16 Correct 96 ms 121532 KB Output is correct
17 Correct 97 ms 121532 KB Output is correct
18 Correct 102 ms 121580 KB Output is correct
19 Correct 96 ms 121580 KB Output is correct
20 Correct 107 ms 121580 KB Output is correct
21 Correct 116 ms 121688 KB Output is correct
22 Correct 107 ms 121688 KB Output is correct
23 Correct 108 ms 121688 KB Output is correct
24 Correct 108 ms 121688 KB Output is correct
25 Correct 116 ms 121688 KB Output is correct
26 Correct 106 ms 121688 KB Output is correct
27 Correct 130 ms 121688 KB Output is correct
28 Correct 104 ms 121688 KB Output is correct
29 Correct 116 ms 121688 KB Output is correct
30 Correct 103 ms 121688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 121304 KB Output is correct
2 Correct 130 ms 121316 KB Output is correct
3 Correct 107 ms 121388 KB Output is correct
4 Correct 117 ms 121416 KB Output is correct
5 Correct 125 ms 121416 KB Output is correct
6 Correct 103 ms 121440 KB Output is correct
7 Correct 119 ms 121444 KB Output is correct
8 Correct 133 ms 121444 KB Output is correct
9 Correct 107 ms 121460 KB Output is correct
10 Correct 100 ms 121460 KB Output is correct
11 Correct 101 ms 121464 KB Output is correct
12 Correct 98 ms 121500 KB Output is correct
13 Correct 99 ms 121532 KB Output is correct
14 Correct 95 ms 121532 KB Output is correct
15 Correct 103 ms 121532 KB Output is correct
16 Correct 96 ms 121532 KB Output is correct
17 Correct 97 ms 121532 KB Output is correct
18 Correct 102 ms 121580 KB Output is correct
19 Correct 96 ms 121580 KB Output is correct
20 Correct 107 ms 121580 KB Output is correct
21 Correct 116 ms 121688 KB Output is correct
22 Correct 107 ms 121688 KB Output is correct
23 Correct 108 ms 121688 KB Output is correct
24 Correct 108 ms 121688 KB Output is correct
25 Correct 116 ms 121688 KB Output is correct
26 Correct 106 ms 121688 KB Output is correct
27 Correct 130 ms 121688 KB Output is correct
28 Correct 104 ms 121688 KB Output is correct
29 Correct 116 ms 121688 KB Output is correct
30 Correct 103 ms 121688 KB Output is correct
31 Correct 3594 ms 131108 KB Output is correct
32 Correct 222 ms 131108 KB Output is correct
33 Correct 473 ms 131108 KB Output is correct
34 Correct 2682 ms 131108 KB Output is correct
35 Correct 1590 ms 131140 KB Output is correct
36 Correct 555 ms 131140 KB Output is correct
37 Correct 481 ms 131140 KB Output is correct
38 Correct 339 ms 131140 KB Output is correct
39 Correct 267 ms 131140 KB Output is correct
40 Correct 285 ms 131140 KB Output is correct
41 Correct 535 ms 131140 KB Output is correct
42 Correct 668 ms 131140 KB Output is correct
43 Correct 546 ms 131140 KB Output is correct
44 Correct 513 ms 131140 KB Output is correct
45 Correct 338 ms 131140 KB Output is correct
46 Correct 271 ms 131140 KB Output is correct
47 Correct 240 ms 131140 KB Output is correct
48 Correct 233 ms 131140 KB Output is correct
49 Correct 257 ms 131140 KB Output is correct
50 Correct 472 ms 131140 KB Output is correct
51 Correct 242 ms 131140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 928 ms 295688 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1004 ms 295688 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 121304 KB Output is correct
2 Correct 130 ms 121316 KB Output is correct
3 Correct 107 ms 121388 KB Output is correct
4 Correct 117 ms 121416 KB Output is correct
5 Correct 125 ms 121416 KB Output is correct
6 Correct 103 ms 121440 KB Output is correct
7 Correct 119 ms 121444 KB Output is correct
8 Correct 133 ms 121444 KB Output is correct
9 Correct 107 ms 121460 KB Output is correct
10 Correct 100 ms 121460 KB Output is correct
11 Correct 101 ms 121464 KB Output is correct
12 Correct 98 ms 121500 KB Output is correct
13 Correct 99 ms 121532 KB Output is correct
14 Correct 95 ms 121532 KB Output is correct
15 Correct 103 ms 121532 KB Output is correct
16 Correct 96 ms 121532 KB Output is correct
17 Correct 97 ms 121532 KB Output is correct
18 Correct 102 ms 121580 KB Output is correct
19 Correct 96 ms 121580 KB Output is correct
20 Correct 107 ms 121580 KB Output is correct
21 Correct 116 ms 121688 KB Output is correct
22 Correct 107 ms 121688 KB Output is correct
23 Correct 108 ms 121688 KB Output is correct
24 Correct 108 ms 121688 KB Output is correct
25 Correct 116 ms 121688 KB Output is correct
26 Correct 106 ms 121688 KB Output is correct
27 Correct 130 ms 121688 KB Output is correct
28 Correct 104 ms 121688 KB Output is correct
29 Correct 116 ms 121688 KB Output is correct
30 Correct 103 ms 121688 KB Output is correct
31 Correct 3594 ms 131108 KB Output is correct
32 Correct 222 ms 131108 KB Output is correct
33 Correct 473 ms 131108 KB Output is correct
34 Correct 2682 ms 131108 KB Output is correct
35 Correct 1590 ms 131140 KB Output is correct
36 Correct 555 ms 131140 KB Output is correct
37 Correct 481 ms 131140 KB Output is correct
38 Correct 339 ms 131140 KB Output is correct
39 Correct 267 ms 131140 KB Output is correct
40 Correct 285 ms 131140 KB Output is correct
41 Correct 535 ms 131140 KB Output is correct
42 Correct 668 ms 131140 KB Output is correct
43 Correct 546 ms 131140 KB Output is correct
44 Correct 513 ms 131140 KB Output is correct
45 Correct 338 ms 131140 KB Output is correct
46 Correct 271 ms 131140 KB Output is correct
47 Correct 240 ms 131140 KB Output is correct
48 Correct 233 ms 131140 KB Output is correct
49 Correct 257 ms 131140 KB Output is correct
50 Correct 472 ms 131140 KB Output is correct
51 Correct 242 ms 131140 KB Output is correct
52 Runtime error 355 ms 295688 KB Execution killed with signal 11 (could be triggered by violating memory limits)
53 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 121304 KB Output is correct
2 Correct 130 ms 121316 KB Output is correct
3 Correct 107 ms 121388 KB Output is correct
4 Correct 117 ms 121416 KB Output is correct
5 Correct 125 ms 121416 KB Output is correct
6 Correct 103 ms 121440 KB Output is correct
7 Correct 119 ms 121444 KB Output is correct
8 Correct 133 ms 121444 KB Output is correct
9 Correct 107 ms 121460 KB Output is correct
10 Correct 100 ms 121460 KB Output is correct
11 Correct 101 ms 121464 KB Output is correct
12 Correct 98 ms 121500 KB Output is correct
13 Correct 99 ms 121532 KB Output is correct
14 Correct 95 ms 121532 KB Output is correct
15 Correct 103 ms 121532 KB Output is correct
16 Correct 96 ms 121532 KB Output is correct
17 Correct 97 ms 121532 KB Output is correct
18 Correct 102 ms 121580 KB Output is correct
19 Correct 96 ms 121580 KB Output is correct
20 Correct 107 ms 121580 KB Output is correct
21 Correct 116 ms 121688 KB Output is correct
22 Correct 107 ms 121688 KB Output is correct
23 Correct 108 ms 121688 KB Output is correct
24 Correct 108 ms 121688 KB Output is correct
25 Correct 116 ms 121688 KB Output is correct
26 Correct 106 ms 121688 KB Output is correct
27 Correct 130 ms 121688 KB Output is correct
28 Correct 104 ms 121688 KB Output is correct
29 Correct 116 ms 121688 KB Output is correct
30 Correct 103 ms 121688 KB Output is correct
31 Correct 3594 ms 131108 KB Output is correct
32 Correct 222 ms 131108 KB Output is correct
33 Correct 473 ms 131108 KB Output is correct
34 Correct 2682 ms 131108 KB Output is correct
35 Correct 1590 ms 131140 KB Output is correct
36 Correct 555 ms 131140 KB Output is correct
37 Correct 481 ms 131140 KB Output is correct
38 Correct 339 ms 131140 KB Output is correct
39 Correct 267 ms 131140 KB Output is correct
40 Correct 285 ms 131140 KB Output is correct
41 Correct 535 ms 131140 KB Output is correct
42 Correct 668 ms 131140 KB Output is correct
43 Correct 546 ms 131140 KB Output is correct
44 Correct 513 ms 131140 KB Output is correct
45 Correct 338 ms 131140 KB Output is correct
46 Correct 271 ms 131140 KB Output is correct
47 Correct 240 ms 131140 KB Output is correct
48 Correct 233 ms 131140 KB Output is correct
49 Correct 257 ms 131140 KB Output is correct
50 Correct 472 ms 131140 KB Output is correct
51 Correct 242 ms 131140 KB Output is correct
52 Runtime error 928 ms 295688 KB Execution killed with signal 11 (could be triggered by violating memory limits)
53 Halted 0 ms 0 KB -