Submission #979777

# Submission time Handle Problem Language Result Execution time Memory
979777 2024-05-11T10:58:43 Z Jarif_Rahman New Home (APIO18_new_home) C++17
57 / 100
5000 ms 323164 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 = 6e6;

struct Node{
    int inc = -1e9, dec = -1e9;
    Node *l = nullptr, *r = nullptr;

    Node(){}
    Node(Node *_l, Node *_r): l(_l), r(_r){}
    void update(){
        inc = -1e9;
        if(l) inc = max(inc, l->inc);
        if(r) inc = max(inc, r->inc);

        dec = -1e9;
        if(l) dec = max(dec, l->dec);
        if(r) dec = max(dec, r->dec);
    }
}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<<=1;
    }
    void update(int i, Node *nd, int a, int b, int x, bool inc){
        if(a == b){
            if(inc) nd->inc = x+n-1-i;
            else nd->dec = x+i;
            return;
        }
        int m = (a+b)>>1;
        if(i <= m){
            if(nd->l == nullptr) nd->l = newNode();
            update(i, nd->l, a, m, x, inc);
        }
        else{
             if(nd->r == nullptr) nd->r = newNode();
             update(i, nd->r, m+1, b, x, inc);
        }
        nd->update();
    }
    void update(int i, int x, bool inc){
        update(i, root, 0, n-1, x, inc);
    }

    int get(int l, int r, Node* nd, int a, int b, bool inc){
        if(!nd) return -1e9;
        if(a > r || b < l) return -1e9;
        if(a >= l && b <= r){
            if(inc) return nd->inc;
            else return nd->dec;
        }
        int m = (a+b)>>1;
        return max(get(l, r, nd->l, a, m, inc), get(l, r, nd->r, m+1, b, inc));
    }
    int get(int l, int r, bool inc){
        return get(l, r, root, 0, n-1, inc);
    }
};

const int L = 1e8+5, N = 3e5;
tuple<int, int, int, int> queries[3*N];
set<int> stores[N];
map<int, int> cnt[N];
map<int, map<int, int>> mx[2];
sparse_segtree S(L);
int cnt_t[N];
int nonzero = 0;
int ans[N];

void add_to_mx(int i, int x, bool inc){
    if(mx[inc][i].empty() || x > mx[inc][i].rbegin()->first) S.update(i, x, inc);
    mx[inc][i][x]++;
}
void remove_from_mx(int i, int x, bool inc){
    mx[inc][i][x]--;
    if(mx[inc][i][x] == 0) mx[inc][i].erase(x);
    if(mx[inc][i].empty()) S.update(i, -1e9, inc);
    else if(x > mx[inc][i].rbegin()->first) S.update(i, mx[inc][i].rbegin()->first, inc);
};

void 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){
        add_to_mx(0, x, false);
        add_to_mx(L-1, L-1-x, true);
    }
    else if(it == stores[t].begin()){
        remove_from_mx(0, *next(it), false);
        add_to_mx(0, x, false);
        int md = (x+*next(it))>>1;
        if(md > x) add_to_mx(md, md-x, true);
        add_to_mx(md+1, *next(it)-md-1, false);
    }
    else if(next(it) == stores[t].end()){
        remove_from_mx(L-1, L-1-*prev(it), true);
        int md = (*prev(it)+x)>>1;
        if(md > *prev(it)) add_to_mx(md, md-*prev(it), true);
        add_to_mx(md+1, x-md-1, false);
        add_to_mx(L-1, L-1-x, true);
    }
    else{
        int md = (*prev(it)+*next(it))>>1;
        if(md > *prev(it)) remove_from_mx(md, md-*prev(it), true);
        remove_from_mx(md+1, *next(it)-md-1, false);

        md = (*prev(it)+x)>>1;
        if(md > *prev(it)) add_to_mx(md, md-*prev(it), true);
        add_to_mx(md+1, x-md-1, false);

        md = (x+*next(it))>>1;
        if(md > x) add_to_mx(md, md-x, true);
        add_to_mx(md+1, *next(it)-md-1, false);
    }
};
void 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){
        remove_from_mx(0, x, false);
        remove_from_mx(L-1, L-1-x, true);
    }
    else if(it == stores[t].begin()){
        add_to_mx(0, *next(it), false);
        remove_from_mx(0, x, false);
        int md = (x+*next(it))>>1;
        if(md > x) remove_from_mx(md, md-x, true);
        remove_from_mx(md+1, *next(it)-md-1, false);
    }
    else if(next(it) == stores[t].end()){
        add_to_mx(L-1, L-1-*prev(it), true);
        int md = (*prev(it)+x)>>1;
        if(md > *prev(it)) remove_from_mx(md, md-*prev(it), true);
        remove_from_mx(md+1, x-md-1, false);
        remove_from_mx(L-1, L-1-x, true);
    }
    else{
        int md = (*prev(it)+*next(it))>>1;
        if(md > *prev(it)) add_to_mx(md, md-*prev(it), true);
        add_to_mx(md+1, *next(it)-md-1, false);

        md = (*prev(it)+x)>>1;
        if(md > *prev(it)) remove_from_mx(md, md-*prev(it), true);
        remove_from_mx(md+1, x-md-1, false);

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

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

    int n, k, q; cin >> n >> k >> q;

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

    fill(cnt_t, cnt_t+k, 0);
    for(int qq = 0; qq < 2*n+q; qq++){
        auto [_, type, x, i] = queries[qq];
        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] = max(S.get(0, x, 0)-x, S.get(x, S.n-1, 1)-(S.n-1)+x);
        }
    }

    for(int i = 0; i < q; i++) cout << ans[i] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 35 ms 173332 KB Output is correct
2 Correct 31 ms 173340 KB Output is correct
3 Correct 29 ms 173396 KB Output is correct
4 Correct 30 ms 173396 KB Output is correct
5 Correct 30 ms 173404 KB Output is correct
6 Correct 32 ms 173652 KB Output is correct
7 Correct 30 ms 173648 KB Output is correct
8 Correct 32 ms 173492 KB Output is correct
9 Correct 29 ms 173396 KB Output is correct
10 Correct 32 ms 173652 KB Output is correct
11 Correct 31 ms 173380 KB Output is correct
12 Correct 31 ms 173656 KB Output is correct
13 Correct 31 ms 173456 KB Output is correct
14 Correct 31 ms 173460 KB Output is correct
15 Correct 31 ms 173396 KB Output is correct
16 Correct 30 ms 173344 KB Output is correct
17 Correct 31 ms 173392 KB Output is correct
18 Correct 31 ms 173400 KB Output is correct
19 Correct 31 ms 173396 KB Output is correct
20 Correct 31 ms 173712 KB Output is correct
21 Correct 30 ms 173404 KB Output is correct
22 Correct 29 ms 173400 KB Output is correct
23 Correct 31 ms 173396 KB Output is correct
24 Correct 31 ms 173416 KB Output is correct
25 Correct 31 ms 173396 KB Output is correct
26 Correct 35 ms 173388 KB Output is correct
27 Correct 31 ms 173396 KB Output is correct
28 Correct 30 ms 173392 KB Output is correct
29 Correct 31 ms 173404 KB Output is correct
30 Correct 30 ms 173552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 173332 KB Output is correct
2 Correct 31 ms 173340 KB Output is correct
3 Correct 29 ms 173396 KB Output is correct
4 Correct 30 ms 173396 KB Output is correct
5 Correct 30 ms 173404 KB Output is correct
6 Correct 32 ms 173652 KB Output is correct
7 Correct 30 ms 173648 KB Output is correct
8 Correct 32 ms 173492 KB Output is correct
9 Correct 29 ms 173396 KB Output is correct
10 Correct 32 ms 173652 KB Output is correct
11 Correct 31 ms 173380 KB Output is correct
12 Correct 31 ms 173656 KB Output is correct
13 Correct 31 ms 173456 KB Output is correct
14 Correct 31 ms 173460 KB Output is correct
15 Correct 31 ms 173396 KB Output is correct
16 Correct 30 ms 173344 KB Output is correct
17 Correct 31 ms 173392 KB Output is correct
18 Correct 31 ms 173400 KB Output is correct
19 Correct 31 ms 173396 KB Output is correct
20 Correct 31 ms 173712 KB Output is correct
21 Correct 30 ms 173404 KB Output is correct
22 Correct 29 ms 173400 KB Output is correct
23 Correct 31 ms 173396 KB Output is correct
24 Correct 31 ms 173416 KB Output is correct
25 Correct 31 ms 173396 KB Output is correct
26 Correct 35 ms 173388 KB Output is correct
27 Correct 31 ms 173396 KB Output is correct
28 Correct 30 ms 173392 KB Output is correct
29 Correct 31 ms 173404 KB Output is correct
30 Correct 30 ms 173552 KB Output is correct
31 Correct 1075 ms 220088 KB Output is correct
32 Correct 79 ms 177748 KB Output is correct
33 Correct 1018 ms 214264 KB Output is correct
34 Correct 1036 ms 212228 KB Output is correct
35 Correct 1051 ms 220620 KB Output is correct
36 Correct 1020 ms 221524 KB Output is correct
37 Correct 660 ms 212072 KB Output is correct
38 Correct 669 ms 212552 KB Output is correct
39 Correct 543 ms 210860 KB Output is correct
40 Correct 586 ms 211612 KB Output is correct
41 Correct 468 ms 196168 KB Output is correct
42 Correct 448 ms 195752 KB Output is correct
43 Correct 84 ms 178092 KB Output is correct
44 Correct 462 ms 195944 KB Output is correct
45 Correct 522 ms 196116 KB Output is correct
46 Correct 429 ms 195916 KB Output is correct
47 Correct 264 ms 191912 KB Output is correct
48 Correct 279 ms 193108 KB Output is correct
49 Correct 326 ms 194676 KB Output is correct
50 Correct 330 ms 193948 KB Output is correct
51 Correct 367 ms 195076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2559 ms 290160 KB Output is correct
2 Correct 1506 ms 300116 KB Output is correct
3 Correct 610 ms 244820 KB Output is correct
4 Correct 2328 ms 282448 KB Output is correct
5 Correct 1516 ms 299520 KB Output is correct
6 Correct 1515 ms 299820 KB Output is correct
7 Correct 674 ms 244924 KB Output is correct
8 Correct 2503 ms 282588 KB Output is correct
9 Correct 2679 ms 295468 KB Output is correct
10 Correct 1866 ms 300692 KB Output is correct
11 Correct 1417 ms 297736 KB Output is correct
12 Correct 1657 ms 299872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5028 ms 323164 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 173332 KB Output is correct
2 Correct 31 ms 173340 KB Output is correct
3 Correct 29 ms 173396 KB Output is correct
4 Correct 30 ms 173396 KB Output is correct
5 Correct 30 ms 173404 KB Output is correct
6 Correct 32 ms 173652 KB Output is correct
7 Correct 30 ms 173648 KB Output is correct
8 Correct 32 ms 173492 KB Output is correct
9 Correct 29 ms 173396 KB Output is correct
10 Correct 32 ms 173652 KB Output is correct
11 Correct 31 ms 173380 KB Output is correct
12 Correct 31 ms 173656 KB Output is correct
13 Correct 31 ms 173456 KB Output is correct
14 Correct 31 ms 173460 KB Output is correct
15 Correct 31 ms 173396 KB Output is correct
16 Correct 30 ms 173344 KB Output is correct
17 Correct 31 ms 173392 KB Output is correct
18 Correct 31 ms 173400 KB Output is correct
19 Correct 31 ms 173396 KB Output is correct
20 Correct 31 ms 173712 KB Output is correct
21 Correct 30 ms 173404 KB Output is correct
22 Correct 29 ms 173400 KB Output is correct
23 Correct 31 ms 173396 KB Output is correct
24 Correct 31 ms 173416 KB Output is correct
25 Correct 31 ms 173396 KB Output is correct
26 Correct 35 ms 173388 KB Output is correct
27 Correct 31 ms 173396 KB Output is correct
28 Correct 30 ms 173392 KB Output is correct
29 Correct 31 ms 173404 KB Output is correct
30 Correct 30 ms 173552 KB Output is correct
31 Correct 1075 ms 220088 KB Output is correct
32 Correct 79 ms 177748 KB Output is correct
33 Correct 1018 ms 214264 KB Output is correct
34 Correct 1036 ms 212228 KB Output is correct
35 Correct 1051 ms 220620 KB Output is correct
36 Correct 1020 ms 221524 KB Output is correct
37 Correct 660 ms 212072 KB Output is correct
38 Correct 669 ms 212552 KB Output is correct
39 Correct 543 ms 210860 KB Output is correct
40 Correct 586 ms 211612 KB Output is correct
41 Correct 468 ms 196168 KB Output is correct
42 Correct 448 ms 195752 KB Output is correct
43 Correct 84 ms 178092 KB Output is correct
44 Correct 462 ms 195944 KB Output is correct
45 Correct 522 ms 196116 KB Output is correct
46 Correct 429 ms 195916 KB Output is correct
47 Correct 264 ms 191912 KB Output is correct
48 Correct 279 ms 193108 KB Output is correct
49 Correct 326 ms 194676 KB Output is correct
50 Correct 330 ms 193948 KB Output is correct
51 Correct 367 ms 195076 KB Output is correct
52 Correct 167 ms 189020 KB Output is correct
53 Correct 167 ms 182908 KB Output is correct
54 Correct 492 ms 198956 KB Output is correct
55 Correct 374 ms 193780 KB Output is correct
56 Correct 315 ms 192592 KB Output is correct
57 Correct 434 ms 195156 KB Output is correct
58 Correct 373 ms 192080 KB Output is correct
59 Correct 324 ms 190804 KB Output is correct
60 Correct 435 ms 194640 KB Output is correct
61 Correct 80 ms 183888 KB Output is correct
62 Correct 178 ms 189292 KB Output is correct
63 Correct 353 ms 193876 KB Output is correct
64 Correct 443 ms 194684 KB Output is correct
65 Correct 463 ms 195412 KB Output is correct
66 Correct 472 ms 195892 KB Output is correct
67 Correct 135 ms 180912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 173332 KB Output is correct
2 Correct 31 ms 173340 KB Output is correct
3 Correct 29 ms 173396 KB Output is correct
4 Correct 30 ms 173396 KB Output is correct
5 Correct 30 ms 173404 KB Output is correct
6 Correct 32 ms 173652 KB Output is correct
7 Correct 30 ms 173648 KB Output is correct
8 Correct 32 ms 173492 KB Output is correct
9 Correct 29 ms 173396 KB Output is correct
10 Correct 32 ms 173652 KB Output is correct
11 Correct 31 ms 173380 KB Output is correct
12 Correct 31 ms 173656 KB Output is correct
13 Correct 31 ms 173456 KB Output is correct
14 Correct 31 ms 173460 KB Output is correct
15 Correct 31 ms 173396 KB Output is correct
16 Correct 30 ms 173344 KB Output is correct
17 Correct 31 ms 173392 KB Output is correct
18 Correct 31 ms 173400 KB Output is correct
19 Correct 31 ms 173396 KB Output is correct
20 Correct 31 ms 173712 KB Output is correct
21 Correct 30 ms 173404 KB Output is correct
22 Correct 29 ms 173400 KB Output is correct
23 Correct 31 ms 173396 KB Output is correct
24 Correct 31 ms 173416 KB Output is correct
25 Correct 31 ms 173396 KB Output is correct
26 Correct 35 ms 173388 KB Output is correct
27 Correct 31 ms 173396 KB Output is correct
28 Correct 30 ms 173392 KB Output is correct
29 Correct 31 ms 173404 KB Output is correct
30 Correct 30 ms 173552 KB Output is correct
31 Correct 1075 ms 220088 KB Output is correct
32 Correct 79 ms 177748 KB Output is correct
33 Correct 1018 ms 214264 KB Output is correct
34 Correct 1036 ms 212228 KB Output is correct
35 Correct 1051 ms 220620 KB Output is correct
36 Correct 1020 ms 221524 KB Output is correct
37 Correct 660 ms 212072 KB Output is correct
38 Correct 669 ms 212552 KB Output is correct
39 Correct 543 ms 210860 KB Output is correct
40 Correct 586 ms 211612 KB Output is correct
41 Correct 468 ms 196168 KB Output is correct
42 Correct 448 ms 195752 KB Output is correct
43 Correct 84 ms 178092 KB Output is correct
44 Correct 462 ms 195944 KB Output is correct
45 Correct 522 ms 196116 KB Output is correct
46 Correct 429 ms 195916 KB Output is correct
47 Correct 264 ms 191912 KB Output is correct
48 Correct 279 ms 193108 KB Output is correct
49 Correct 326 ms 194676 KB Output is correct
50 Correct 330 ms 193948 KB Output is correct
51 Correct 367 ms 195076 KB Output is correct
52 Correct 2559 ms 290160 KB Output is correct
53 Correct 1506 ms 300116 KB Output is correct
54 Correct 610 ms 244820 KB Output is correct
55 Correct 2328 ms 282448 KB Output is correct
56 Correct 1516 ms 299520 KB Output is correct
57 Correct 1515 ms 299820 KB Output is correct
58 Correct 674 ms 244924 KB Output is correct
59 Correct 2503 ms 282588 KB Output is correct
60 Correct 2679 ms 295468 KB Output is correct
61 Correct 1866 ms 300692 KB Output is correct
62 Correct 1417 ms 297736 KB Output is correct
63 Correct 1657 ms 299872 KB Output is correct
64 Execution timed out 5028 ms 323164 KB Time limit exceeded
65 Halted 0 ms 0 KB -