Submission #979388

# Submission time Handle Problem Language Result Execution time Memory
979388 2024-05-10T18:46:21 Z Jarif_Rahman New Home (APIO18_new_home) C++17
47 / 100
3226 ms 1048576 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int lim = 7e6;

struct Node{
    map<int, int> inc, dec;
    Node *l = nullptr, *r = nullptr;
    Node(){}
    Node(Node *_l, Node *_r): l(_l), r(_r){}
}Nodes[lim];

int ls_node = 0;
Node* newNode(){
    return &Nodes[ls_node++];
}

struct sparse_segtree{
    Node* root = newNode();
    int n = 1;
    sparse_segtree(int _n){
        while(n < _n) n*=2;
    }

    void add(int l, int r, Node* nd, int a, int b, ll x, bool inc){
        if(a > r || b < l) return;
        if(a >= l && b <= r){
            if(inc) nd->inc[x+a-l]++;
            else nd->dec[x-a+l]++;
            return;
        }
        int m = (a+b)/2;
        if(!nd->l) nd->l = newNode();
        if(!nd->r) nd->r = newNode();
        add(l, r, nd->l, a, m, x, inc);
        add(l, r, nd->r, m+1, b, x, inc);
    }
    void add(int l, int r, int x, bool inc){
        add(l, r, root, 0, n-1, x, inc);
    }
    void remove(int l, int r, Node* nd, int a, int b, int x, bool inc){
        if(a > r || b < l) return;
        if(a >= l && b <= r){
            if(inc){
                nd->inc[x+a-l]--;
                if(nd->inc[x+a-l] == 0) nd->inc.erase(x+a-l);
            }
            else{
                nd->dec[x-a+l]--;
                if(nd->dec[x-a+l] == 0) nd->dec.erase(x-a+l);
            }
            return;
        }
        int m = (a+b)/2;
        if(!nd->l) nd->l = new Node();
        if(!nd->r) nd->r = new Node();
        remove(l, r, nd->l, a, m, x, inc);
        remove(l, r, nd->r, m+1, b, x, inc);
    }
    void remove(int l, int r, int x, bool inc){
        remove(l, r, root, 0, n-1, x, inc);
    }

    int get(int i){
        Node* nd = root;
        int a = 0, b = n-1;
        int mx = 0;
        while(nd){
            if(!nd->inc.empty()) mx = max(mx, nd->inc.rbegin()->first+i-a);
            if(!nd->dec.empty()) mx = max(mx, nd->dec.rbegin()->first-i+a);
            int m = (a+b)/2;
            if(i <= m) nd = nd->l, b = m;
            else nd = nd->r, a = m+1;
        }
        return mx;
    }
};

const int L = 1e8+5;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, k, q; cin >> n >> k >> q;
    vector<tuple<int, int, int, int>> queries;

    for(int i = 0; i < n; i++){
        int x, t, a, b; cin >> x >> t >> a >> b; t--;
        queries.push_back({a, 0, x, t});
        queries.push_back({b, 2, x, t});
    }
    for(int i = 0; i < q; i++){
        int l, y; cin >> l >> y;
        queries.push_back({y, 1, l, i});
    }
    sort(queries.begin(), queries.end());

    vector<set<int>> stores(k);
    vector<map<int, int>> cnt(k);
    sparse_segtree S(L);

    auto add = [&](int t, int x){
        cnt[t][x]++;
        if(cnt[t][x] > 1) return;
        auto it = stores[t].insert(x).first;
        if(stores[t].size() == 1){
            S.add(0, x, x, false);
            S.add(x+1, L-1, 1, true);
        }
        else if(it == stores[t].begin()){
            S.remove(0, *next(it), *next(it), false);
            S.add(0, x, x, false);
            int md = (x+*next(it))/2;
            if(md > x) S.add(x+1, md, 1, true);
            S.add(md+1, *next(it), *next(it)-md-1, false);
        }
        else if(next(it) == stores[t].end()){
            S.remove(*prev(it)+1, L-1, 1, true);
            int md = (*prev(it)+x)/2;
            if(md > *prev(it)) S.add(*prev(it)+1, md, 1, true);
            S.add(md+1, x, x-md-1, false);
            S.add(x+1, L-1, 1, true);
        }
        else{
            int md = (*prev(it)+*next(it))/2;
            if(md > *prev(it)) S.remove(*prev(it)+1, md, 1, true);
            S.remove(md+1, *next(it), *next(it)-md-1, false);

            md = (*prev(it)+x)/2;
            if(md > *prev(it)) S.add(*prev(it)+1, md, 1, true);
            S.add(md+1, x, x-md-1, false);

            md = (x+*next(it))/2;
            if(md > x) S.add(x+1, md, 1, true);
            S.add(md+1, *next(it), *next(it)-md-1, false);
        }
    };
    auto remove = [&](int t, int x){
        cnt[t][x]--;
        if(cnt[t][x] > 0) return;
        auto it = stores[t].find(x);
        if(stores[t].size() == 1){
            S.remove(0, x, x, false);
            S.remove(x+1, L-1, 1, true);
        }
        else if(it == stores[t].begin()){
            S.add(0, *next(it), *next(it), false);
            S.remove(0, x, x, false);
            int md = (x+*next(it))/2;
            if(md > x) S.remove(x+1, md, 1, true);
            S.remove(md+1, *next(it), *next(it)-md-1, false);
        }
        else if(next(it) == stores[t].end()){
            S.add(*prev(it)+1, L-1, 1, true);
            int md = (*prev(it)+x)/2;
            if(md > *prev(it)) S.remove(*prev(it)+1, md, 1, true);
            S.remove(md+1, x, x-md-1, false);
            S.remove(x+1, L-1, 1, true);
        }
        else{
            int md = (*prev(it)+*next(it))/2;
            if(md > *prev(it)) S.add(*prev(it)+1, md, 1, true);
            S.add(md+1, *next(it), *next(it)-md-1, false);

            md = (*prev(it)+x)/2;
            if(md > *prev(it)) S.remove(*prev(it)+1, md, 1, true);
            S.remove(md+1, x, x-md-1, false);

            md = (x+*next(it))/2;
            if(md > x) S.remove(x+1, md, 1, true);
            S.remove(md+1, *next(it), *next(it)-md-1, false);
        }
        stores[t].erase(it);
    };

    vector<int> cnt_t(k, 0);
    int nonzero = 0;

    vector<int> ans(q);

    for(auto [_, type, x, i]: queries){
        if(type == 0){
            cnt_t[i]++;
            if(cnt_t[i] == 1) nonzero++;
            add(i, x);
        }
        else if(type == 2){
            cnt_t[i]--;
            if(cnt_t[i] == 0) nonzero--;
            remove(i, x);
        }
        else{
            if(nonzero < k) ans[i] = -1;
            else ans[i] = S.get(x);
        }
    }

    for(int x: ans) cout << x << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 167 ms 767424 KB Output is correct
2 Correct 165 ms 767316 KB Output is correct
3 Correct 167 ms 767316 KB Output is correct
4 Correct 168 ms 767364 KB Output is correct
5 Correct 166 ms 767568 KB Output is correct
6 Correct 170 ms 767828 KB Output is correct
7 Correct 168 ms 767716 KB Output is correct
8 Correct 169 ms 768388 KB Output is correct
9 Correct 166 ms 768388 KB Output is correct
10 Correct 162 ms 768376 KB Output is correct
11 Correct 162 ms 767612 KB Output is correct
12 Correct 162 ms 767568 KB Output is correct
13 Correct 161 ms 767612 KB Output is correct
14 Correct 158 ms 767616 KB Output is correct
15 Correct 162 ms 767800 KB Output is correct
16 Correct 161 ms 767928 KB Output is correct
17 Correct 159 ms 767572 KB Output is correct
18 Correct 161 ms 767828 KB Output is correct
19 Correct 162 ms 768080 KB Output is correct
20 Correct 158 ms 767788 KB Output is correct
21 Correct 156 ms 767564 KB Output is correct
22 Correct 160 ms 768124 KB Output is correct
23 Correct 159 ms 768040 KB Output is correct
24 Correct 162 ms 768084 KB Output is correct
25 Correct 161 ms 767572 KB Output is correct
26 Correct 161 ms 767476 KB Output is correct
27 Correct 160 ms 767620 KB Output is correct
28 Correct 159 ms 767564 KB Output is correct
29 Correct 163 ms 767700 KB Output is correct
30 Correct 155 ms 767612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 767424 KB Output is correct
2 Correct 165 ms 767316 KB Output is correct
3 Correct 167 ms 767316 KB Output is correct
4 Correct 168 ms 767364 KB Output is correct
5 Correct 166 ms 767568 KB Output is correct
6 Correct 170 ms 767828 KB Output is correct
7 Correct 168 ms 767716 KB Output is correct
8 Correct 169 ms 768388 KB Output is correct
9 Correct 166 ms 768388 KB Output is correct
10 Correct 162 ms 768376 KB Output is correct
11 Correct 162 ms 767612 KB Output is correct
12 Correct 162 ms 767568 KB Output is correct
13 Correct 161 ms 767612 KB Output is correct
14 Correct 158 ms 767616 KB Output is correct
15 Correct 162 ms 767800 KB Output is correct
16 Correct 161 ms 767928 KB Output is correct
17 Correct 159 ms 767572 KB Output is correct
18 Correct 161 ms 767828 KB Output is correct
19 Correct 162 ms 768080 KB Output is correct
20 Correct 158 ms 767788 KB Output is correct
21 Correct 156 ms 767564 KB Output is correct
22 Correct 160 ms 768124 KB Output is correct
23 Correct 159 ms 768040 KB Output is correct
24 Correct 162 ms 768084 KB Output is correct
25 Correct 161 ms 767572 KB Output is correct
26 Correct 161 ms 767476 KB Output is correct
27 Correct 160 ms 767620 KB Output is correct
28 Correct 159 ms 767564 KB Output is correct
29 Correct 163 ms 767700 KB Output is correct
30 Correct 155 ms 767612 KB Output is correct
31 Correct 3204 ms 874752 KB Output is correct
32 Correct 208 ms 771792 KB Output is correct
33 Correct 1995 ms 802236 KB Output is correct
34 Correct 3226 ms 812944 KB Output is correct
35 Correct 2710 ms 866068 KB Output is correct
36 Correct 1826 ms 845740 KB Output is correct
37 Correct 1625 ms 779744 KB Output is correct
38 Correct 1238 ms 778664 KB Output is correct
39 Correct 808 ms 774200 KB Output is correct
40 Correct 812 ms 774592 KB Output is correct
41 Correct 1215 ms 775124 KB Output is correct
42 Correct 1278 ms 775088 KB Output is correct
43 Correct 194 ms 771748 KB Output is correct
44 Correct 1201 ms 774644 KB Output is correct
45 Correct 1033 ms 774236 KB Output is correct
46 Correct 843 ms 773824 KB Output is correct
47 Correct 639 ms 773564 KB Output is correct
48 Correct 616 ms 774452 KB Output is correct
49 Correct 739 ms 773820 KB Output is correct
50 Correct 996 ms 774536 KB Output is correct
51 Correct 705 ms 774848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2068 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2363 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 167 ms 767424 KB Output is correct
2 Correct 165 ms 767316 KB Output is correct
3 Correct 167 ms 767316 KB Output is correct
4 Correct 168 ms 767364 KB Output is correct
5 Correct 166 ms 767568 KB Output is correct
6 Correct 170 ms 767828 KB Output is correct
7 Correct 168 ms 767716 KB Output is correct
8 Correct 169 ms 768388 KB Output is correct
9 Correct 166 ms 768388 KB Output is correct
10 Correct 162 ms 768376 KB Output is correct
11 Correct 162 ms 767612 KB Output is correct
12 Correct 162 ms 767568 KB Output is correct
13 Correct 161 ms 767612 KB Output is correct
14 Correct 158 ms 767616 KB Output is correct
15 Correct 162 ms 767800 KB Output is correct
16 Correct 161 ms 767928 KB Output is correct
17 Correct 159 ms 767572 KB Output is correct
18 Correct 161 ms 767828 KB Output is correct
19 Correct 162 ms 768080 KB Output is correct
20 Correct 158 ms 767788 KB Output is correct
21 Correct 156 ms 767564 KB Output is correct
22 Correct 160 ms 768124 KB Output is correct
23 Correct 159 ms 768040 KB Output is correct
24 Correct 162 ms 768084 KB Output is correct
25 Correct 161 ms 767572 KB Output is correct
26 Correct 161 ms 767476 KB Output is correct
27 Correct 160 ms 767620 KB Output is correct
28 Correct 159 ms 767564 KB Output is correct
29 Correct 163 ms 767700 KB Output is correct
30 Correct 155 ms 767612 KB Output is correct
31 Correct 3204 ms 874752 KB Output is correct
32 Correct 208 ms 771792 KB Output is correct
33 Correct 1995 ms 802236 KB Output is correct
34 Correct 3226 ms 812944 KB Output is correct
35 Correct 2710 ms 866068 KB Output is correct
36 Correct 1826 ms 845740 KB Output is correct
37 Correct 1625 ms 779744 KB Output is correct
38 Correct 1238 ms 778664 KB Output is correct
39 Correct 808 ms 774200 KB Output is correct
40 Correct 812 ms 774592 KB Output is correct
41 Correct 1215 ms 775124 KB Output is correct
42 Correct 1278 ms 775088 KB Output is correct
43 Correct 194 ms 771748 KB Output is correct
44 Correct 1201 ms 774644 KB Output is correct
45 Correct 1033 ms 774236 KB Output is correct
46 Correct 843 ms 773824 KB Output is correct
47 Correct 639 ms 773564 KB Output is correct
48 Correct 616 ms 774452 KB Output is correct
49 Correct 739 ms 773820 KB Output is correct
50 Correct 996 ms 774536 KB Output is correct
51 Correct 705 ms 774848 KB Output is correct
52 Correct 1280 ms 889416 KB Output is correct
53 Correct 1260 ms 819212 KB Output is correct
54 Correct 2858 ms 897472 KB Output is correct
55 Correct 1357 ms 814600 KB Output is correct
56 Correct 1328 ms 835108 KB Output is correct
57 Correct 1350 ms 787856 KB Output is correct
58 Correct 1596 ms 814396 KB Output is correct
59 Correct 1499 ms 834844 KB Output is correct
60 Correct 1627 ms 786624 KB Output is correct
61 Correct 239 ms 783956 KB Output is correct
62 Correct 1282 ms 889896 KB Output is correct
63 Correct 2269 ms 874936 KB Output is correct
64 Correct 2712 ms 854976 KB Output is correct
65 Correct 3202 ms 800872 KB Output is correct
66 Correct 1442 ms 776748 KB Output is correct
67 Correct 393 ms 774572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 767424 KB Output is correct
2 Correct 165 ms 767316 KB Output is correct
3 Correct 167 ms 767316 KB Output is correct
4 Correct 168 ms 767364 KB Output is correct
5 Correct 166 ms 767568 KB Output is correct
6 Correct 170 ms 767828 KB Output is correct
7 Correct 168 ms 767716 KB Output is correct
8 Correct 169 ms 768388 KB Output is correct
9 Correct 166 ms 768388 KB Output is correct
10 Correct 162 ms 768376 KB Output is correct
11 Correct 162 ms 767612 KB Output is correct
12 Correct 162 ms 767568 KB Output is correct
13 Correct 161 ms 767612 KB Output is correct
14 Correct 158 ms 767616 KB Output is correct
15 Correct 162 ms 767800 KB Output is correct
16 Correct 161 ms 767928 KB Output is correct
17 Correct 159 ms 767572 KB Output is correct
18 Correct 161 ms 767828 KB Output is correct
19 Correct 162 ms 768080 KB Output is correct
20 Correct 158 ms 767788 KB Output is correct
21 Correct 156 ms 767564 KB Output is correct
22 Correct 160 ms 768124 KB Output is correct
23 Correct 159 ms 768040 KB Output is correct
24 Correct 162 ms 768084 KB Output is correct
25 Correct 161 ms 767572 KB Output is correct
26 Correct 161 ms 767476 KB Output is correct
27 Correct 160 ms 767620 KB Output is correct
28 Correct 159 ms 767564 KB Output is correct
29 Correct 163 ms 767700 KB Output is correct
30 Correct 155 ms 767612 KB Output is correct
31 Correct 3204 ms 874752 KB Output is correct
32 Correct 208 ms 771792 KB Output is correct
33 Correct 1995 ms 802236 KB Output is correct
34 Correct 3226 ms 812944 KB Output is correct
35 Correct 2710 ms 866068 KB Output is correct
36 Correct 1826 ms 845740 KB Output is correct
37 Correct 1625 ms 779744 KB Output is correct
38 Correct 1238 ms 778664 KB Output is correct
39 Correct 808 ms 774200 KB Output is correct
40 Correct 812 ms 774592 KB Output is correct
41 Correct 1215 ms 775124 KB Output is correct
42 Correct 1278 ms 775088 KB Output is correct
43 Correct 194 ms 771748 KB Output is correct
44 Correct 1201 ms 774644 KB Output is correct
45 Correct 1033 ms 774236 KB Output is correct
46 Correct 843 ms 773824 KB Output is correct
47 Correct 639 ms 773564 KB Output is correct
48 Correct 616 ms 774452 KB Output is correct
49 Correct 739 ms 773820 KB Output is correct
50 Correct 996 ms 774536 KB Output is correct
51 Correct 705 ms 774848 KB Output is correct
52 Runtime error 2068 ms 1048576 KB Execution killed with signal 9
53 Halted 0 ms 0 KB -