답안 #1100230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100230 2024-10-13T10:30:25 Z vladilius 새 집 (APIO18_new_home) C++17
57 / 100
5000 ms 150300 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const int inf = 1e8;
const int N = 3e5 + 1;
const int NN = 4 * N;

struct IT{
    struct node{
        int m, l, r, s;
        node(){
            l = r = m = 0;
        }
    };
    node all[NN];
    multiset<int> st[N];
    vector<int> f;
    vector<int> :: iterator it;
    int rt, cc, cc1, n;
    void init(vector<int> f1){
        f = f1;
        n = (int) f.size();
        all[1] = node();
        rt = cc = 1;
        cc1 = -1;
    }
    int nw(int tl, int tr){
        all[++cc] = node();
        if (tl == tr){
            st[++cc1] = {};
            all[cc].s = cc1;
        }
        return cc;
    }
    void add(int v, int tl, int tr, int& p, int& x){
        if (tl == tr){
            st[all[v].s].insert(x);
            all[v].m = max(all[v].m, x);
            return;
        }
        int tm = (tl + tr) / 2;
        if (p <= tm){
            if (!all[v].l) all[v].l = nw(tl, tm);
            add(all[v].l, tl, tm, p, x);
        }
        else {
            if (!all[v].r) all[v].r = nw(tm + 1, tr);
            add(all[v].r, tm + 1, tr, p, x);
        }
        all[v].m = 0;
        if (all[v].l) all[v].m = max(all[v].m, all[all[v].l].m);
        if (all[v].r) all[v].m = max(all[v].m, all[all[v].r].m);
    }
    void add(int l, int r){
        it = lower_bound(f.begin(), f.end(), l);
        l = (int) (it - f.begin()) + 1;
        add(rt, 1, inf, l, r);
    }
    void rem(int v, int tl, int tr, int& p, int& x){
        if (tl == tr){
            st[all[v].s].erase(st[all[v].s].find(x));
            all[v].m = (st[all[v].s].empty()) ? 0 : *prev(st[all[v].s].end());
            return;
        }
        int tm = (tl + tr) / 2;
        if (p <= tm){
            rem(all[v].l, tl, tm, p, x);
        }
        else {
            rem(all[v].r, tm + 1, tr, p, x);
        }
        all[v].m = 0;
        if (all[v].l) all[v].m = max(all[v].m, all[all[v].l].m);
        if (all[v].r) all[v].m = max(all[v].m, all[all[v].r].m);
    }
    void rem(int l, int r){
        it = lower_bound(f.begin(), f.end(), l);
        l = (int) (it - f.begin()) + 1;
        rem(rt, 1, inf, l, r);
    }
    int get(int v, int tl, int tr, int l, int r){
        if (l > tr || r < tl) return 0;
        if (l <= tl && tr <= r) return all[v].m;
        int tm = (tl + tr) / 2, out = 0;
        if (all[v].l) out = max(out, get(all[v].l, tl, tm, l, r));
        if (all[v].r) out = max(out, get(all[v].r, tm + 1, tr, l, r));
        return out;
    }
    bool get(int l, int r){
        it = upper_bound(f.begin(), f.end(), l);
        if (it == f.begin()) return 0;
        l = (int) (it - f.begin());
        return get(rt, 1, inf, 1, l) >= r;
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, k, q; cin>>n>>k>>q;
    vector<int> x(n + 1), t(n + 1), a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>x[i]>>t[i]>>a[i]>>b[i];
    }
    vector<int> xq(q + 1), yq(q + 1);
    for (int i = 1; i <= q; i++){
        cin>>xq[i]>>yq[i];
    }
    
    vector<int> all1 = {1};
    for (int i = 1; i <= n; i++){
        all1.pb(x[i] + 1);
    }
    sort(all1.begin(), all1.end());
    
    vector<int> all;
    int i = 0;
    while (i < all1.size()){
        int j = i;
        while (j < all1.size() && all1[i] == all1[j]){
            j++;
        }
        all.pb(all1[i]);
        i = j;
    }

    map<int, vector<pii>> mp;
    for (int i = 1; i <= n; i++){
        mp[a[i]].pb({1, i});
        mp[b[i] + 1].pb({2, i});
    }
    for (int i = 1; i <= q; i++){
        mp[yq[i]].pb({i + 2, xq[i]});
    }
    
    vector<int> out(q + 1), cnt(k + 1);
    multiset<int> st[k + 1];
    int df = 0, l, r;
    vector<pii> qq;
    IT T; T.init(all);
    for (auto [X, V]: mp){
        qq.clear();
        for (auto [tt, i]: V){
            if (tt == 1){
                df += !cnt[t[i]];
                cnt[t[i]]++;
                if (st[t[i]].empty()){
                    if (1 < x[i]) T.add(1, x[i] - 1);
                    if (x[i] < inf) T.add(x[i] + 1, inf);
                    st[t[i]].insert(x[i]);
                    continue;
                }
                
                if (st[t[i]].find(x[i]) != st[t[i]].end()){
                    st[t[i]].insert(x[i]);
                    continue;
                }
                
                auto it = st[t[i]].upper_bound(x[i]);
                if (it == st[t[i]].end()) r = inf;
                else r = (*it) - 1;
                if (it == st[t[i]].begin()) l = 1;
                else l = *prev(it) + 1;
                
                if (l <= r) T.rem(l, r);
                if (l < x[i]) T.add(l, x[i] - 1);
                if (x[i] < r) T.add(x[i] + 1, r);
                
                st[t[i]].insert(x[i]);
            }
            else if (tt == 2){
                cnt[t[i]]--;
                df -= !cnt[t[i]];
                
                if (st[t[i]].count(x[i]) > 1){
                    st[t[i]].erase(st[t[i]].find(x[i]));
                    continue;
                }
                
                auto it = st[t[i]].find(x[i]);
                
                if (it == st[t[i]].begin()) l = 1;
                else l = *prev(it) + 1;
                if (it == prev(st[t[i]].end())) r = inf;
                else r = *next(it) - 1;
                
                if (l < x[i]) T.rem(l, x[i] - 1);
                if (x[i] < r) T.rem(x[i] + 1, r);
                
                if (cnt[t[i]]) T.add(l, r);
                st[t[i]].erase(it);
            }
            else {
                qq.pb({tt - 2, i});
            }
        }
        for (auto [ii, x]: qq){
            if (df < k){
                out[ii] = -1;
                continue;
            }
            
            auto check = [&](int d){
                return T.get(max(1, x - d), min(inf, x + d));
            };
            
            int l = 0, r = inf;
            while (l + 1 < r){
                int m = (l + r) / 2;
                if (check(m)){
                    l = m + 1;
                }
                else {
                    r = m;
                }
            }
            if (check(l)) l = r;
            
            out[ii] = l;
        }
    }
    
    for (int i = 1; i <= q; i++){
        cout<<out[i]<<"\n";
    }
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:124:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |     while (i < all1.size()){
      |            ~~^~~~~~~~~~~~~
new_home.cpp:126:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |         while (j < all1.size() && all1[i] == all1[j]){
      |                ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 33096 KB Output is correct
2 Correct 33 ms 33104 KB Output is correct
3 Correct 31 ms 33180 KB Output is correct
4 Correct 29 ms 33316 KB Output is correct
5 Correct 31 ms 33360 KB Output is correct
6 Correct 33 ms 33360 KB Output is correct
7 Correct 31 ms 33352 KB Output is correct
8 Correct 30 ms 33360 KB Output is correct
9 Correct 30 ms 33360 KB Output is correct
10 Correct 34 ms 33360 KB Output is correct
11 Correct 31 ms 33360 KB Output is correct
12 Correct 30 ms 33248 KB Output is correct
13 Correct 28 ms 33532 KB Output is correct
14 Correct 28 ms 33360 KB Output is correct
15 Correct 31 ms 33376 KB Output is correct
16 Correct 29 ms 33352 KB Output is correct
17 Correct 30 ms 33372 KB Output is correct
18 Correct 29 ms 33360 KB Output is correct
19 Correct 29 ms 33360 KB Output is correct
20 Correct 29 ms 33360 KB Output is correct
21 Correct 28 ms 33308 KB Output is correct
22 Correct 28 ms 33372 KB Output is correct
23 Correct 28 ms 33360 KB Output is correct
24 Correct 30 ms 33376 KB Output is correct
25 Correct 29 ms 33364 KB Output is correct
26 Correct 29 ms 33352 KB Output is correct
27 Correct 29 ms 33360 KB Output is correct
28 Correct 29 ms 33528 KB Output is correct
29 Correct 29 ms 33360 KB Output is correct
30 Correct 29 ms 33360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 33096 KB Output is correct
2 Correct 33 ms 33104 KB Output is correct
3 Correct 31 ms 33180 KB Output is correct
4 Correct 29 ms 33316 KB Output is correct
5 Correct 31 ms 33360 KB Output is correct
6 Correct 33 ms 33360 KB Output is correct
7 Correct 31 ms 33352 KB Output is correct
8 Correct 30 ms 33360 KB Output is correct
9 Correct 30 ms 33360 KB Output is correct
10 Correct 34 ms 33360 KB Output is correct
11 Correct 31 ms 33360 KB Output is correct
12 Correct 30 ms 33248 KB Output is correct
13 Correct 28 ms 33532 KB Output is correct
14 Correct 28 ms 33360 KB Output is correct
15 Correct 31 ms 33376 KB Output is correct
16 Correct 29 ms 33352 KB Output is correct
17 Correct 30 ms 33372 KB Output is correct
18 Correct 29 ms 33360 KB Output is correct
19 Correct 29 ms 33360 KB Output is correct
20 Correct 29 ms 33360 KB Output is correct
21 Correct 28 ms 33308 KB Output is correct
22 Correct 28 ms 33372 KB Output is correct
23 Correct 28 ms 33360 KB Output is correct
24 Correct 30 ms 33376 KB Output is correct
25 Correct 29 ms 33364 KB Output is correct
26 Correct 29 ms 33352 KB Output is correct
27 Correct 29 ms 33360 KB Output is correct
28 Correct 29 ms 33528 KB Output is correct
29 Correct 29 ms 33360 KB Output is correct
30 Correct 29 ms 33360 KB Output is correct
31 Correct 682 ms 61324 KB Output is correct
32 Correct 435 ms 38468 KB Output is correct
33 Correct 701 ms 57692 KB Output is correct
34 Correct 651 ms 57972 KB Output is correct
35 Correct 680 ms 61404 KB Output is correct
36 Correct 752 ms 61280 KB Output is correct
37 Correct 622 ms 56076 KB Output is correct
38 Correct 633 ms 56068 KB Output is correct
39 Correct 529 ms 55800 KB Output is correct
40 Correct 573 ms 55644 KB Output is correct
41 Correct 591 ms 55940 KB Output is correct
42 Correct 441 ms 55644 KB Output is correct
43 Correct 238 ms 41444 KB Output is correct
44 Correct 533 ms 55900 KB Output is correct
45 Correct 528 ms 55900 KB Output is correct
46 Correct 522 ms 55908 KB Output is correct
47 Correct 345 ms 54888 KB Output is correct
48 Correct 419 ms 54888 KB Output is correct
49 Correct 419 ms 55144 KB Output is correct
50 Correct 374 ms 55644 KB Output is correct
51 Correct 491 ms 55140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3055 ms 126828 KB Output is correct
2 Correct 4162 ms 119876 KB Output is correct
3 Correct 2572 ms 150300 KB Output is correct
4 Correct 2998 ms 130732 KB Output is correct
5 Correct 3990 ms 119164 KB Output is correct
6 Correct 3772 ms 119628 KB Output is correct
7 Correct 2826 ms 150260 KB Output is correct
8 Correct 2633 ms 130708 KB Output is correct
9 Correct 2715 ms 124028 KB Output is correct
10 Correct 3253 ms 120424 KB Output is correct
11 Correct 2585 ms 119396 KB Output is correct
12 Correct 2742 ms 120164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3928 ms 147556 KB Output is correct
2 Execution timed out 5077 ms 69560 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 33096 KB Output is correct
2 Correct 33 ms 33104 KB Output is correct
3 Correct 31 ms 33180 KB Output is correct
4 Correct 29 ms 33316 KB Output is correct
5 Correct 31 ms 33360 KB Output is correct
6 Correct 33 ms 33360 KB Output is correct
7 Correct 31 ms 33352 KB Output is correct
8 Correct 30 ms 33360 KB Output is correct
9 Correct 30 ms 33360 KB Output is correct
10 Correct 34 ms 33360 KB Output is correct
11 Correct 31 ms 33360 KB Output is correct
12 Correct 30 ms 33248 KB Output is correct
13 Correct 28 ms 33532 KB Output is correct
14 Correct 28 ms 33360 KB Output is correct
15 Correct 31 ms 33376 KB Output is correct
16 Correct 29 ms 33352 KB Output is correct
17 Correct 30 ms 33372 KB Output is correct
18 Correct 29 ms 33360 KB Output is correct
19 Correct 29 ms 33360 KB Output is correct
20 Correct 29 ms 33360 KB Output is correct
21 Correct 28 ms 33308 KB Output is correct
22 Correct 28 ms 33372 KB Output is correct
23 Correct 28 ms 33360 KB Output is correct
24 Correct 30 ms 33376 KB Output is correct
25 Correct 29 ms 33364 KB Output is correct
26 Correct 29 ms 33352 KB Output is correct
27 Correct 29 ms 33360 KB Output is correct
28 Correct 29 ms 33528 KB Output is correct
29 Correct 29 ms 33360 KB Output is correct
30 Correct 29 ms 33360 KB Output is correct
31 Correct 682 ms 61324 KB Output is correct
32 Correct 435 ms 38468 KB Output is correct
33 Correct 701 ms 57692 KB Output is correct
34 Correct 651 ms 57972 KB Output is correct
35 Correct 680 ms 61404 KB Output is correct
36 Correct 752 ms 61280 KB Output is correct
37 Correct 622 ms 56076 KB Output is correct
38 Correct 633 ms 56068 KB Output is correct
39 Correct 529 ms 55800 KB Output is correct
40 Correct 573 ms 55644 KB Output is correct
41 Correct 591 ms 55940 KB Output is correct
42 Correct 441 ms 55644 KB Output is correct
43 Correct 238 ms 41444 KB Output is correct
44 Correct 533 ms 55900 KB Output is correct
45 Correct 528 ms 55900 KB Output is correct
46 Correct 522 ms 55908 KB Output is correct
47 Correct 345 ms 54888 KB Output is correct
48 Correct 419 ms 54888 KB Output is correct
49 Correct 419 ms 55144 KB Output is correct
50 Correct 374 ms 55644 KB Output is correct
51 Correct 491 ms 55140 KB Output is correct
52 Correct 369 ms 66964 KB Output is correct
53 Correct 314 ms 61532 KB Output is correct
54 Correct 356 ms 63068 KB Output is correct
55 Correct 542 ms 59740 KB Output is correct
56 Correct 579 ms 61532 KB Output is correct
57 Correct 569 ms 57084 KB Output is correct
58 Correct 494 ms 59488 KB Output is correct
59 Correct 451 ms 61532 KB Output is correct
60 Correct 478 ms 56908 KB Output is correct
61 Correct 259 ms 47084 KB Output is correct
62 Correct 362 ms 67164 KB Output is correct
63 Correct 405 ms 63324 KB Output is correct
64 Correct 442 ms 61432 KB Output is correct
65 Correct 486 ms 57692 KB Output is correct
66 Correct 539 ms 55900 KB Output is correct
67 Correct 280 ms 39116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 33096 KB Output is correct
2 Correct 33 ms 33104 KB Output is correct
3 Correct 31 ms 33180 KB Output is correct
4 Correct 29 ms 33316 KB Output is correct
5 Correct 31 ms 33360 KB Output is correct
6 Correct 33 ms 33360 KB Output is correct
7 Correct 31 ms 33352 KB Output is correct
8 Correct 30 ms 33360 KB Output is correct
9 Correct 30 ms 33360 KB Output is correct
10 Correct 34 ms 33360 KB Output is correct
11 Correct 31 ms 33360 KB Output is correct
12 Correct 30 ms 33248 KB Output is correct
13 Correct 28 ms 33532 KB Output is correct
14 Correct 28 ms 33360 KB Output is correct
15 Correct 31 ms 33376 KB Output is correct
16 Correct 29 ms 33352 KB Output is correct
17 Correct 30 ms 33372 KB Output is correct
18 Correct 29 ms 33360 KB Output is correct
19 Correct 29 ms 33360 KB Output is correct
20 Correct 29 ms 33360 KB Output is correct
21 Correct 28 ms 33308 KB Output is correct
22 Correct 28 ms 33372 KB Output is correct
23 Correct 28 ms 33360 KB Output is correct
24 Correct 30 ms 33376 KB Output is correct
25 Correct 29 ms 33364 KB Output is correct
26 Correct 29 ms 33352 KB Output is correct
27 Correct 29 ms 33360 KB Output is correct
28 Correct 29 ms 33528 KB Output is correct
29 Correct 29 ms 33360 KB Output is correct
30 Correct 29 ms 33360 KB Output is correct
31 Correct 682 ms 61324 KB Output is correct
32 Correct 435 ms 38468 KB Output is correct
33 Correct 701 ms 57692 KB Output is correct
34 Correct 651 ms 57972 KB Output is correct
35 Correct 680 ms 61404 KB Output is correct
36 Correct 752 ms 61280 KB Output is correct
37 Correct 622 ms 56076 KB Output is correct
38 Correct 633 ms 56068 KB Output is correct
39 Correct 529 ms 55800 KB Output is correct
40 Correct 573 ms 55644 KB Output is correct
41 Correct 591 ms 55940 KB Output is correct
42 Correct 441 ms 55644 KB Output is correct
43 Correct 238 ms 41444 KB Output is correct
44 Correct 533 ms 55900 KB Output is correct
45 Correct 528 ms 55900 KB Output is correct
46 Correct 522 ms 55908 KB Output is correct
47 Correct 345 ms 54888 KB Output is correct
48 Correct 419 ms 54888 KB Output is correct
49 Correct 419 ms 55144 KB Output is correct
50 Correct 374 ms 55644 KB Output is correct
51 Correct 491 ms 55140 KB Output is correct
52 Correct 3055 ms 126828 KB Output is correct
53 Correct 4162 ms 119876 KB Output is correct
54 Correct 2572 ms 150300 KB Output is correct
55 Correct 2998 ms 130732 KB Output is correct
56 Correct 3990 ms 119164 KB Output is correct
57 Correct 3772 ms 119628 KB Output is correct
58 Correct 2826 ms 150260 KB Output is correct
59 Correct 2633 ms 130708 KB Output is correct
60 Correct 2715 ms 124028 KB Output is correct
61 Correct 3253 ms 120424 KB Output is correct
62 Correct 2585 ms 119396 KB Output is correct
63 Correct 2742 ms 120164 KB Output is correct
64 Correct 3928 ms 147556 KB Output is correct
65 Execution timed out 5077 ms 69560 KB Time limit exceeded
66 Halted 0 ms 0 KB -