답안 #1100440

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100440 2024-10-13T21:48:08 Z vladilius 새 집 (APIO18_new_home) C++17
57 / 100
5000 ms 327596 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
#define arr3 array<int, 3>
const int inf = 1e8;
const int inff = inf + 1;
const int N = 2e6;
 
struct IT{
    struct node{
        int m1, m2, i, l, r;
        node(){
            l = r = 0;
            m1 = inff; m2 = 0;
        }
    };
    multiset<int> s[N];
    vector<node> all;
    int cc, cc1;
    void init(){
        all.pb(node());
        all.pb(node());
        cc = 1; cc1 = 0;
    }
    int nw(int tl, int tr){
        all.pb(node()); cc++;
        if (tl == tr){
            cc1++;
            all[cc].i = cc1;
        }
        return cc;
    }
    void add2(int v, int tl, int tr, int p, int x){
        if (tl == tr){
            s[all[v].i].insert(x);
            all[v].m2 = max(all[v].m2, x);
            return;
        }
        int tm = (tl + tr) / 2;
        if (p <= tm){
            if (!all[v].l) all[v].l = nw(tl, tm);
            add2(all[v].l, tl, tm, p, x);
        }
        else {
            if (!all[v].r) all[v].r = nw(tm + 1, tr);
            add2(all[v].r, tm + 1, tr, p, x);
        }
        all[v].m2 = max(all[v].m2, x);
    }
    void add1(int v, int tl, int tr, int p, int x){
        if (tl == tr){
            s[all[v].i].insert(x);
            all[v].m1 = min(all[v].m1, x);
            return;
        }
        int tm = (tl + tr) / 2;
        if (p <= tm){
            if (!all[v].l) all[v].l = nw(tl, tm);
            add1(all[v].l, tl, tm, p, x);
        }
        else {
            if (!all[v].r) all[v].r = nw(tm + 1, tr);
            add1(all[v].r, tm + 1, tr, p, x);
        }
        all[v].m1 = min(all[v].m1, x);
    }
    void add(int l, int r){
        if (l == 1){
            add2(1, 1, inf, l, r + 1);
            return;
        }
        if (r == inf){
            add1(1, 1, inf, inf, l - 1);
            return;
        }
        int m = (l + r) / 2;
        if (l <= m) add1(1, 1, inf, m, l - 1);
        if (m < r) add2(1, 1, inf, m + 1, r + 1);
    }
    void rem2(int v, int tl, int tr, int p, int x, bool t){
        if (tl == tr){
            s[all[v].i].erase(s[all[v].i].find(x));
            all[v].m2 = s[all[v].i].empty() ? 0 : *prev(s[all[v].i].end());
            return;
        }
        int tm = (tl + tr) / 2;
        if (p <= tm){
            rem2(all[v].l, tl, tm, p, x, t);
        }
        else {
            rem2(all[v].r, tm + 1, tr, p, x, t);
        }
        all[v].m2 = 0;
        if (all[v].l) all[v].m2 = max(all[v].m2, all[all[v].l].m2);
        if (all[v].r) all[v].m2 = max(all[v].m2, all[all[v].r].m2);
    }
    void rem1(int v, int tl, int tr, int p, int x, bool t){
        if (tl == tr){
            s[all[v].i].erase(s[all[v].i].find(x));
            all[v].m1 = s[all[v].i].empty() ? inff : *s[all[v].i].begin();
            return;
        }
        int tm = (tl + tr) / 2;
        if (p <= tm){
            rem1(all[v].l, tl, tm, p, x, t);
        }
        else {
            rem1(all[v].r, tm + 1, tr, p, x, t);
        }
        all[v].m1 = inff;
        if (all[v].l) all[v].m1 = min(all[v].m1, all[all[v].l].m1);
        if (all[v].r) all[v].m1 = min(all[v].m1, all[all[v].r].m1);
    }
    void rem(int l, int r){
        if (l == 1){
            rem2(1, 1, inf, l, r + 1, 1);
            return;
        }
        if (r == inf){
            rem1(1, 1, inf, inf, l - 1, 0);
            return;
        }
        int m = (l + r) / 2;
        if (l <= m) rem1(1, 1, inf, m, l - 1, 0);
        if (m < r) rem2(1, 1, inf, m + 1, r + 1, 1);
    }
    int get2(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].m2;
        int tm = (tl + tr) / 2, out = 0;
        if (all[v].l){
            out = max(out, get2(all[v].l, tl, tm, l, r));
        }
        if (all[v].r){
            out = max(out, get2(all[v].r, tm + 1, tr, l, r));
        }
        return out;
    }
    int get1(int v, int tl, int tr, int l, int r){
        if (l > tr || r < tl) return inff;
        if (l <= tl && tr <= r) return all[v].m1;
        int tm = (tl + tr) / 2, out = inff;
        if (all[v].l){
            out = min(out, get1(all[v].l, tl, tm, l, r));
        }
        if (all[v].r){
            out = min(out, get1(all[v].r, tm + 1, tr, l, r));
        }
        return out;
    }
    pii get(int x){
        return {get1(1, 1, inf, x, inf), get2(1, 1, inf, 1, x)};
    }
};
 
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);
    bool ind = 1;
    for (int i = 1; i <= n; i++){
        cin>>x[i]>>t[i]>>a[i]>>b[i];
        if (a[i] != 1) ind = 0;
    }
    vector<int> xq(q + 1), yq(q + 1);
    for (int i = 1; i <= q; i++){
        cin>>xq[i]>>yq[i];
    }
 
    vector<int> out(q + 1), cnt(k + 1);
    multiset<int> st[k + 1];
    int df = 0, l, r;
    IT T; T.init();
    vector<arr3> mp;
    if (!ind){
        for (int i = 1; i <= n; i++){
            mp.pb({a[i], 1, i});
            if (b[i] < inf) mp.pb({b[i] + 1, 2, i});
        }
        for (int i = 1; i <= q; i++){
            mp.pb({yq[i], i + 2, xq[i]});
        }
    }
    else {
        vector<int> f[k + 1];
        for (int i = 1; i <= n; i++) f[t[i]].pb(x[i]);
        for (int i = 1; i <= k; i++){
            if (f[i].empty()) continue;
            sort(f[i].begin(), f[i].end());
            int pre = 1;
            for (int j: f[i]){
                if (pre < j){
                    T.add(pre, j - 1);
                }
                pre = j + 1;
            }
            if (pre <= inf) T.add(pre, inf);
        }
        for (int i = 1; i <= n; i++){
            df += !cnt[t[i]]; cnt[t[i]]++;
            st[t[i]].insert(x[i]);
            if (b[i] < inf) mp.pb({b[i] + 1, 2, i});
        }
        for (int i = 1; i <= q; i++){
            mp.pb({yq[i], i + 2, xq[i]});
        }
    }
    
    auto cmp = [&](arr3 x, arr3 y){
        if (x[0] == y[0]){
            return x[1] < y[1];
        }
        return x[0] < y[0];
    };
    
    sort(mp.begin(), mp.end(), cmp);
    
    for (auto [X, tt, i]: mp){
        if (tt == 1){
            df += !cnt[t[i]];
            cnt[t[i]]++;
            if (cnt[t[i]] == 1){
                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 {
            int ii = tt - 2;
            if (df < k){
                out[ii] = -1;
                continue;
            }
            
            auto [l, r] = T.get(i);
            if (l != inff) out[ii] = max(out[ii], i - l);
            if (r != 0) out[ii] = max(out[ii], r - i);
        }
    }
    
    for (int i = 1; i <= q; i++){
        cout<<out[i]<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 94136 KB Output is correct
2 Correct 104 ms 94280 KB Output is correct
3 Correct 95 ms 94280 KB Output is correct
4 Correct 96 ms 94280 KB Output is correct
5 Correct 94 ms 94296 KB Output is correct
6 Correct 103 ms 95128 KB Output is correct
7 Correct 92 ms 94392 KB Output is correct
8 Correct 96 ms 94788 KB Output is correct
9 Correct 96 ms 94284 KB Output is correct
10 Correct 97 ms 95088 KB Output is correct
11 Correct 95 ms 94548 KB Output is correct
12 Correct 98 ms 94872 KB Output is correct
13 Correct 98 ms 94536 KB Output is correct
14 Correct 92 ms 94512 KB Output is correct
15 Correct 96 ms 94540 KB Output is correct
16 Correct 97 ms 94540 KB Output is correct
17 Correct 100 ms 94772 KB Output is correct
18 Correct 99 ms 94536 KB Output is correct
19 Correct 92 ms 94536 KB Output is correct
20 Correct 83 ms 94776 KB Output is correct
21 Correct 86 ms 94280 KB Output is correct
22 Correct 84 ms 94288 KB Output is correct
23 Correct 86 ms 94560 KB Output is correct
24 Correct 87 ms 94592 KB Output is correct
25 Correct 95 ms 94776 KB Output is correct
26 Correct 87 ms 94788 KB Output is correct
27 Correct 83 ms 94280 KB Output is correct
28 Correct 86 ms 94536 KB Output is correct
29 Correct 84 ms 94536 KB Output is correct
30 Correct 85 ms 94448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 94136 KB Output is correct
2 Correct 104 ms 94280 KB Output is correct
3 Correct 95 ms 94280 KB Output is correct
4 Correct 96 ms 94280 KB Output is correct
5 Correct 94 ms 94296 KB Output is correct
6 Correct 103 ms 95128 KB Output is correct
7 Correct 92 ms 94392 KB Output is correct
8 Correct 96 ms 94788 KB Output is correct
9 Correct 96 ms 94284 KB Output is correct
10 Correct 97 ms 95088 KB Output is correct
11 Correct 95 ms 94548 KB Output is correct
12 Correct 98 ms 94872 KB Output is correct
13 Correct 98 ms 94536 KB Output is correct
14 Correct 92 ms 94512 KB Output is correct
15 Correct 96 ms 94540 KB Output is correct
16 Correct 97 ms 94540 KB Output is correct
17 Correct 100 ms 94772 KB Output is correct
18 Correct 99 ms 94536 KB Output is correct
19 Correct 92 ms 94536 KB Output is correct
20 Correct 83 ms 94776 KB Output is correct
21 Correct 86 ms 94280 KB Output is correct
22 Correct 84 ms 94288 KB Output is correct
23 Correct 86 ms 94560 KB Output is correct
24 Correct 87 ms 94592 KB Output is correct
25 Correct 95 ms 94776 KB Output is correct
26 Correct 87 ms 94788 KB Output is correct
27 Correct 83 ms 94280 KB Output is correct
28 Correct 86 ms 94536 KB Output is correct
29 Correct 84 ms 94536 KB Output is correct
30 Correct 85 ms 94448 KB Output is correct
31 Correct 462 ms 148120 KB Output is correct
32 Correct 337 ms 99516 KB Output is correct
33 Correct 473 ms 144284 KB Output is correct
34 Correct 472 ms 144140 KB Output is correct
35 Correct 518 ms 148120 KB Output is correct
36 Correct 536 ms 190672 KB Output is correct
37 Correct 424 ms 143656 KB Output is correct
38 Correct 445 ms 143772 KB Output is correct
39 Correct 350 ms 143772 KB Output is correct
40 Correct 381 ms 143736 KB Output is correct
41 Correct 327 ms 143776 KB Output is correct
42 Correct 305 ms 143768 KB Output is correct
43 Correct 136 ms 100544 KB Output is correct
44 Correct 333 ms 143776 KB Output is correct
45 Correct 322 ms 143620 KB Output is correct
46 Correct 313 ms 143780 KB Output is correct
47 Correct 263 ms 123128 KB Output is correct
48 Correct 259 ms 123048 KB Output is correct
49 Correct 261 ms 123052 KB Output is correct
50 Correct 265 ms 123136 KB Output is correct
51 Correct 263 ms 123044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 728 ms 221360 KB Output is correct
2 Correct 735 ms 227152 KB Output is correct
3 Correct 710 ms 183788 KB Output is correct
4 Correct 820 ms 221536 KB Output is correct
5 Correct 693 ms 228060 KB Output is correct
6 Correct 731 ms 228128 KB Output is correct
7 Correct 605 ms 183668 KB Output is correct
8 Correct 742 ms 221612 KB Output is correct
9 Correct 703 ms 224672 KB Output is correct
10 Correct 708 ms 228304 KB Output is correct
11 Correct 620 ms 225808 KB Output is correct
12 Correct 610 ms 225948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1924 ms 327596 KB Output is correct
2 Execution timed out 5073 ms 132092 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 94136 KB Output is correct
2 Correct 104 ms 94280 KB Output is correct
3 Correct 95 ms 94280 KB Output is correct
4 Correct 96 ms 94280 KB Output is correct
5 Correct 94 ms 94296 KB Output is correct
6 Correct 103 ms 95128 KB Output is correct
7 Correct 92 ms 94392 KB Output is correct
8 Correct 96 ms 94788 KB Output is correct
9 Correct 96 ms 94284 KB Output is correct
10 Correct 97 ms 95088 KB Output is correct
11 Correct 95 ms 94548 KB Output is correct
12 Correct 98 ms 94872 KB Output is correct
13 Correct 98 ms 94536 KB Output is correct
14 Correct 92 ms 94512 KB Output is correct
15 Correct 96 ms 94540 KB Output is correct
16 Correct 97 ms 94540 KB Output is correct
17 Correct 100 ms 94772 KB Output is correct
18 Correct 99 ms 94536 KB Output is correct
19 Correct 92 ms 94536 KB Output is correct
20 Correct 83 ms 94776 KB Output is correct
21 Correct 86 ms 94280 KB Output is correct
22 Correct 84 ms 94288 KB Output is correct
23 Correct 86 ms 94560 KB Output is correct
24 Correct 87 ms 94592 KB Output is correct
25 Correct 95 ms 94776 KB Output is correct
26 Correct 87 ms 94788 KB Output is correct
27 Correct 83 ms 94280 KB Output is correct
28 Correct 86 ms 94536 KB Output is correct
29 Correct 84 ms 94536 KB Output is correct
30 Correct 85 ms 94448 KB Output is correct
31 Correct 462 ms 148120 KB Output is correct
32 Correct 337 ms 99516 KB Output is correct
33 Correct 473 ms 144284 KB Output is correct
34 Correct 472 ms 144140 KB Output is correct
35 Correct 518 ms 148120 KB Output is correct
36 Correct 536 ms 190672 KB Output is correct
37 Correct 424 ms 143656 KB Output is correct
38 Correct 445 ms 143772 KB Output is correct
39 Correct 350 ms 143772 KB Output is correct
40 Correct 381 ms 143736 KB Output is correct
41 Correct 327 ms 143776 KB Output is correct
42 Correct 305 ms 143768 KB Output is correct
43 Correct 136 ms 100544 KB Output is correct
44 Correct 333 ms 143776 KB Output is correct
45 Correct 322 ms 143620 KB Output is correct
46 Correct 313 ms 143780 KB Output is correct
47 Correct 263 ms 123128 KB Output is correct
48 Correct 259 ms 123048 KB Output is correct
49 Correct 261 ms 123052 KB Output is correct
50 Correct 265 ms 123136 KB Output is correct
51 Correct 263 ms 123044 KB Output is correct
52 Correct 249 ms 109660 KB Output is correct
53 Correct 240 ms 104508 KB Output is correct
54 Correct 354 ms 128536 KB Output is correct
55 Correct 308 ms 127144 KB Output is correct
56 Correct 310 ms 128932 KB Output is correct
57 Correct 317 ms 124336 KB Output is correct
58 Correct 301 ms 124596 KB Output is correct
59 Correct 284 ms 126372 KB Output is correct
60 Correct 311 ms 123568 KB Output is correct
61 Correct 147 ms 109312 KB Output is correct
62 Correct 224 ms 109748 KB Output is correct
63 Correct 301 ms 117436 KB Output is correct
64 Correct 317 ms 127144 KB Output is correct
65 Correct 335 ms 124336 KB Output is correct
66 Correct 323 ms 123300 KB Output is correct
67 Correct 331 ms 100528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 94136 KB Output is correct
2 Correct 104 ms 94280 KB Output is correct
3 Correct 95 ms 94280 KB Output is correct
4 Correct 96 ms 94280 KB Output is correct
5 Correct 94 ms 94296 KB Output is correct
6 Correct 103 ms 95128 KB Output is correct
7 Correct 92 ms 94392 KB Output is correct
8 Correct 96 ms 94788 KB Output is correct
9 Correct 96 ms 94284 KB Output is correct
10 Correct 97 ms 95088 KB Output is correct
11 Correct 95 ms 94548 KB Output is correct
12 Correct 98 ms 94872 KB Output is correct
13 Correct 98 ms 94536 KB Output is correct
14 Correct 92 ms 94512 KB Output is correct
15 Correct 96 ms 94540 KB Output is correct
16 Correct 97 ms 94540 KB Output is correct
17 Correct 100 ms 94772 KB Output is correct
18 Correct 99 ms 94536 KB Output is correct
19 Correct 92 ms 94536 KB Output is correct
20 Correct 83 ms 94776 KB Output is correct
21 Correct 86 ms 94280 KB Output is correct
22 Correct 84 ms 94288 KB Output is correct
23 Correct 86 ms 94560 KB Output is correct
24 Correct 87 ms 94592 KB Output is correct
25 Correct 95 ms 94776 KB Output is correct
26 Correct 87 ms 94788 KB Output is correct
27 Correct 83 ms 94280 KB Output is correct
28 Correct 86 ms 94536 KB Output is correct
29 Correct 84 ms 94536 KB Output is correct
30 Correct 85 ms 94448 KB Output is correct
31 Correct 462 ms 148120 KB Output is correct
32 Correct 337 ms 99516 KB Output is correct
33 Correct 473 ms 144284 KB Output is correct
34 Correct 472 ms 144140 KB Output is correct
35 Correct 518 ms 148120 KB Output is correct
36 Correct 536 ms 190672 KB Output is correct
37 Correct 424 ms 143656 KB Output is correct
38 Correct 445 ms 143772 KB Output is correct
39 Correct 350 ms 143772 KB Output is correct
40 Correct 381 ms 143736 KB Output is correct
41 Correct 327 ms 143776 KB Output is correct
42 Correct 305 ms 143768 KB Output is correct
43 Correct 136 ms 100544 KB Output is correct
44 Correct 333 ms 143776 KB Output is correct
45 Correct 322 ms 143620 KB Output is correct
46 Correct 313 ms 143780 KB Output is correct
47 Correct 263 ms 123128 KB Output is correct
48 Correct 259 ms 123048 KB Output is correct
49 Correct 261 ms 123052 KB Output is correct
50 Correct 265 ms 123136 KB Output is correct
51 Correct 263 ms 123044 KB Output is correct
52 Correct 728 ms 221360 KB Output is correct
53 Correct 735 ms 227152 KB Output is correct
54 Correct 710 ms 183788 KB Output is correct
55 Correct 820 ms 221536 KB Output is correct
56 Correct 693 ms 228060 KB Output is correct
57 Correct 731 ms 228128 KB Output is correct
58 Correct 605 ms 183668 KB Output is correct
59 Correct 742 ms 221612 KB Output is correct
60 Correct 703 ms 224672 KB Output is correct
61 Correct 708 ms 228304 KB Output is correct
62 Correct 620 ms 225808 KB Output is correct
63 Correct 610 ms 225948 KB Output is correct
64 Correct 1924 ms 327596 KB Output is correct
65 Execution timed out 5073 ms 132092 KB Time limit exceeded
66 Halted 0 ms 0 KB -