답안 #979805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
979805 2024-05-11T11:48:28 Z Jarif_Rahman 새 집 (APIO18_new_home) C++17
100 / 100
4835 ms 452580 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 = 1e7;

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];
multiset<int> stores[N];
unordered_map<int, multiset<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()) S.update(i, x, inc);
    mx[inc][i].insert(x);
}
void remove_from_mx(int i, int x, bool inc){
    mx[inc][i].erase(mx[inc][i].find(x));
    if(mx[inc][i].empty()) S.update(i, -1e9, inc);
    else if(x > *mx[inc][i].rbegin()) S.update(i, *mx[inc][i].rbegin(), inc);
};

void add(int t, int x){
    auto it = stores[t].insert(x);
    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){
    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;
    mx[0].reserve(n+q);
    mx[1].reserve(n+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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 253136 KB Output is correct
2 Correct 43 ms 253264 KB Output is correct
3 Correct 41 ms 253484 KB Output is correct
4 Correct 40 ms 253340 KB Output is correct
5 Correct 41 ms 253324 KB Output is correct
6 Correct 42 ms 253520 KB Output is correct
7 Correct 41 ms 253272 KB Output is correct
8 Correct 41 ms 253424 KB Output is correct
9 Correct 41 ms 253264 KB Output is correct
10 Correct 42 ms 253520 KB Output is correct
11 Correct 41 ms 253272 KB Output is correct
12 Correct 42 ms 253264 KB Output is correct
13 Correct 44 ms 253268 KB Output is correct
14 Correct 41 ms 253328 KB Output is correct
15 Correct 42 ms 253512 KB Output is correct
16 Correct 41 ms 253268 KB Output is correct
17 Correct 41 ms 253268 KB Output is correct
18 Correct 41 ms 253268 KB Output is correct
19 Correct 41 ms 253420 KB Output is correct
20 Correct 41 ms 253268 KB Output is correct
21 Correct 41 ms 253264 KB Output is correct
22 Correct 41 ms 253268 KB Output is correct
23 Correct 45 ms 253268 KB Output is correct
24 Correct 42 ms 253336 KB Output is correct
25 Correct 43 ms 253252 KB Output is correct
26 Correct 41 ms 253268 KB Output is correct
27 Correct 41 ms 253276 KB Output is correct
28 Correct 41 ms 253256 KB Output is correct
29 Correct 41 ms 253264 KB Output is correct
30 Correct 41 ms 253332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 253136 KB Output is correct
2 Correct 43 ms 253264 KB Output is correct
3 Correct 41 ms 253484 KB Output is correct
4 Correct 40 ms 253340 KB Output is correct
5 Correct 41 ms 253324 KB Output is correct
6 Correct 42 ms 253520 KB Output is correct
7 Correct 41 ms 253272 KB Output is correct
8 Correct 41 ms 253424 KB Output is correct
9 Correct 41 ms 253264 KB Output is correct
10 Correct 42 ms 253520 KB Output is correct
11 Correct 41 ms 253272 KB Output is correct
12 Correct 42 ms 253264 KB Output is correct
13 Correct 44 ms 253268 KB Output is correct
14 Correct 41 ms 253328 KB Output is correct
15 Correct 42 ms 253512 KB Output is correct
16 Correct 41 ms 253268 KB Output is correct
17 Correct 41 ms 253268 KB Output is correct
18 Correct 41 ms 253268 KB Output is correct
19 Correct 41 ms 253420 KB Output is correct
20 Correct 41 ms 253268 KB Output is correct
21 Correct 41 ms 253264 KB Output is correct
22 Correct 41 ms 253268 KB Output is correct
23 Correct 45 ms 253268 KB Output is correct
24 Correct 42 ms 253336 KB Output is correct
25 Correct 43 ms 253252 KB Output is correct
26 Correct 41 ms 253268 KB Output is correct
27 Correct 41 ms 253276 KB Output is correct
28 Correct 41 ms 253256 KB Output is correct
29 Correct 41 ms 253264 KB Output is correct
30 Correct 41 ms 253332 KB Output is correct
31 Correct 743 ms 291736 KB Output is correct
32 Correct 126 ms 261976 KB Output is correct
33 Correct 712 ms 289576 KB Output is correct
34 Correct 658 ms 288100 KB Output is correct
35 Correct 759 ms 292004 KB Output is correct
36 Correct 756 ms 292244 KB Output is correct
37 Correct 487 ms 288152 KB Output is correct
38 Correct 505 ms 288896 KB Output is correct
39 Correct 398 ms 286888 KB Output is correct
40 Correct 420 ms 287640 KB Output is correct
41 Correct 270 ms 272468 KB Output is correct
42 Correct 266 ms 272720 KB Output is correct
43 Correct 120 ms 265556 KB Output is correct
44 Correct 270 ms 272464 KB Output is correct
45 Correct 261 ms 272404 KB Output is correct
46 Correct 257 ms 272600 KB Output is correct
47 Correct 185 ms 269436 KB Output is correct
48 Correct 190 ms 270160 KB Output is correct
49 Correct 208 ms 271444 KB Output is correct
50 Correct 214 ms 270764 KB Output is correct
51 Correct 214 ms 271800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1805 ms 357804 KB Output is correct
2 Correct 1181 ms 365900 KB Output is correct
3 Correct 573 ms 320324 KB Output is correct
4 Correct 1644 ms 351412 KB Output is correct
5 Correct 1198 ms 365688 KB Output is correct
6 Correct 1176 ms 365768 KB Output is correct
7 Correct 573 ms 320248 KB Output is correct
8 Correct 1638 ms 351420 KB Output is correct
9 Correct 1878 ms 362376 KB Output is correct
10 Correct 1413 ms 366512 KB Output is correct
11 Correct 1127 ms 364484 KB Output is correct
12 Correct 1245 ms 365852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3294 ms 382472 KB Output is correct
2 Correct 496 ms 303956 KB Output is correct
3 Correct 3169 ms 389352 KB Output is correct
4 Correct 769 ms 318288 KB Output is correct
5 Correct 2344 ms 359400 KB Output is correct
6 Correct 2144 ms 350540 KB Output is correct
7 Correct 2958 ms 388816 KB Output is correct
8 Correct 3089 ms 389232 KB Output is correct
9 Correct 782 ms 319568 KB Output is correct
10 Correct 2352 ms 358696 KB Output is correct
11 Correct 3127 ms 377048 KB Output is correct
12 Correct 3350 ms 389808 KB Output is correct
13 Correct 1566 ms 379388 KB Output is correct
14 Correct 1550 ms 380048 KB Output is correct
15 Correct 1758 ms 385112 KB Output is correct
16 Correct 2027 ms 384672 KB Output is correct
17 Correct 1885 ms 385732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 253136 KB Output is correct
2 Correct 43 ms 253264 KB Output is correct
3 Correct 41 ms 253484 KB Output is correct
4 Correct 40 ms 253340 KB Output is correct
5 Correct 41 ms 253324 KB Output is correct
6 Correct 42 ms 253520 KB Output is correct
7 Correct 41 ms 253272 KB Output is correct
8 Correct 41 ms 253424 KB Output is correct
9 Correct 41 ms 253264 KB Output is correct
10 Correct 42 ms 253520 KB Output is correct
11 Correct 41 ms 253272 KB Output is correct
12 Correct 42 ms 253264 KB Output is correct
13 Correct 44 ms 253268 KB Output is correct
14 Correct 41 ms 253328 KB Output is correct
15 Correct 42 ms 253512 KB Output is correct
16 Correct 41 ms 253268 KB Output is correct
17 Correct 41 ms 253268 KB Output is correct
18 Correct 41 ms 253268 KB Output is correct
19 Correct 41 ms 253420 KB Output is correct
20 Correct 41 ms 253268 KB Output is correct
21 Correct 41 ms 253264 KB Output is correct
22 Correct 41 ms 253268 KB Output is correct
23 Correct 45 ms 253268 KB Output is correct
24 Correct 42 ms 253336 KB Output is correct
25 Correct 43 ms 253252 KB Output is correct
26 Correct 41 ms 253268 KB Output is correct
27 Correct 41 ms 253276 KB Output is correct
28 Correct 41 ms 253256 KB Output is correct
29 Correct 41 ms 253264 KB Output is correct
30 Correct 41 ms 253332 KB Output is correct
31 Correct 743 ms 291736 KB Output is correct
32 Correct 126 ms 261976 KB Output is correct
33 Correct 712 ms 289576 KB Output is correct
34 Correct 658 ms 288100 KB Output is correct
35 Correct 759 ms 292004 KB Output is correct
36 Correct 756 ms 292244 KB Output is correct
37 Correct 487 ms 288152 KB Output is correct
38 Correct 505 ms 288896 KB Output is correct
39 Correct 398 ms 286888 KB Output is correct
40 Correct 420 ms 287640 KB Output is correct
41 Correct 270 ms 272468 KB Output is correct
42 Correct 266 ms 272720 KB Output is correct
43 Correct 120 ms 265556 KB Output is correct
44 Correct 270 ms 272464 KB Output is correct
45 Correct 261 ms 272404 KB Output is correct
46 Correct 257 ms 272600 KB Output is correct
47 Correct 185 ms 269436 KB Output is correct
48 Correct 190 ms 270160 KB Output is correct
49 Correct 208 ms 271444 KB Output is correct
50 Correct 214 ms 270764 KB Output is correct
51 Correct 214 ms 271800 KB Output is correct
52 Correct 137 ms 268044 KB Output is correct
53 Correct 132 ms 262976 KB Output is correct
54 Correct 345 ms 276304 KB Output is correct
55 Correct 253 ms 271388 KB Output is correct
56 Correct 216 ms 270420 KB Output is correct
57 Correct 265 ms 271952 KB Output is correct
58 Correct 250 ms 269776 KB Output is correct
59 Correct 219 ms 269284 KB Output is correct
60 Correct 262 ms 271416 KB Output is correct
61 Correct 113 ms 268384 KB Output is correct
62 Correct 138 ms 268116 KB Output is correct
63 Correct 259 ms 271952 KB Output is correct
64 Correct 313 ms 272556 KB Output is correct
65 Correct 304 ms 272464 KB Output is correct
66 Correct 282 ms 272652 KB Output is correct
67 Correct 166 ms 261532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 253136 KB Output is correct
2 Correct 43 ms 253264 KB Output is correct
3 Correct 41 ms 253484 KB Output is correct
4 Correct 40 ms 253340 KB Output is correct
5 Correct 41 ms 253324 KB Output is correct
6 Correct 42 ms 253520 KB Output is correct
7 Correct 41 ms 253272 KB Output is correct
8 Correct 41 ms 253424 KB Output is correct
9 Correct 41 ms 253264 KB Output is correct
10 Correct 42 ms 253520 KB Output is correct
11 Correct 41 ms 253272 KB Output is correct
12 Correct 42 ms 253264 KB Output is correct
13 Correct 44 ms 253268 KB Output is correct
14 Correct 41 ms 253328 KB Output is correct
15 Correct 42 ms 253512 KB Output is correct
16 Correct 41 ms 253268 KB Output is correct
17 Correct 41 ms 253268 KB Output is correct
18 Correct 41 ms 253268 KB Output is correct
19 Correct 41 ms 253420 KB Output is correct
20 Correct 41 ms 253268 KB Output is correct
21 Correct 41 ms 253264 KB Output is correct
22 Correct 41 ms 253268 KB Output is correct
23 Correct 45 ms 253268 KB Output is correct
24 Correct 42 ms 253336 KB Output is correct
25 Correct 43 ms 253252 KB Output is correct
26 Correct 41 ms 253268 KB Output is correct
27 Correct 41 ms 253276 KB Output is correct
28 Correct 41 ms 253256 KB Output is correct
29 Correct 41 ms 253264 KB Output is correct
30 Correct 41 ms 253332 KB Output is correct
31 Correct 743 ms 291736 KB Output is correct
32 Correct 126 ms 261976 KB Output is correct
33 Correct 712 ms 289576 KB Output is correct
34 Correct 658 ms 288100 KB Output is correct
35 Correct 759 ms 292004 KB Output is correct
36 Correct 756 ms 292244 KB Output is correct
37 Correct 487 ms 288152 KB Output is correct
38 Correct 505 ms 288896 KB Output is correct
39 Correct 398 ms 286888 KB Output is correct
40 Correct 420 ms 287640 KB Output is correct
41 Correct 270 ms 272468 KB Output is correct
42 Correct 266 ms 272720 KB Output is correct
43 Correct 120 ms 265556 KB Output is correct
44 Correct 270 ms 272464 KB Output is correct
45 Correct 261 ms 272404 KB Output is correct
46 Correct 257 ms 272600 KB Output is correct
47 Correct 185 ms 269436 KB Output is correct
48 Correct 190 ms 270160 KB Output is correct
49 Correct 208 ms 271444 KB Output is correct
50 Correct 214 ms 270764 KB Output is correct
51 Correct 214 ms 271800 KB Output is correct
52 Correct 1805 ms 357804 KB Output is correct
53 Correct 1181 ms 365900 KB Output is correct
54 Correct 573 ms 320324 KB Output is correct
55 Correct 1644 ms 351412 KB Output is correct
56 Correct 1198 ms 365688 KB Output is correct
57 Correct 1176 ms 365768 KB Output is correct
58 Correct 573 ms 320248 KB Output is correct
59 Correct 1638 ms 351420 KB Output is correct
60 Correct 1878 ms 362376 KB Output is correct
61 Correct 1413 ms 366512 KB Output is correct
62 Correct 1127 ms 364484 KB Output is correct
63 Correct 1245 ms 365852 KB Output is correct
64 Correct 3294 ms 382472 KB Output is correct
65 Correct 496 ms 303956 KB Output is correct
66 Correct 3169 ms 389352 KB Output is correct
67 Correct 769 ms 318288 KB Output is correct
68 Correct 2344 ms 359400 KB Output is correct
69 Correct 2144 ms 350540 KB Output is correct
70 Correct 2958 ms 388816 KB Output is correct
71 Correct 3089 ms 389232 KB Output is correct
72 Correct 782 ms 319568 KB Output is correct
73 Correct 2352 ms 358696 KB Output is correct
74 Correct 3127 ms 377048 KB Output is correct
75 Correct 3350 ms 389808 KB Output is correct
76 Correct 1566 ms 379388 KB Output is correct
77 Correct 1550 ms 380048 KB Output is correct
78 Correct 1758 ms 385112 KB Output is correct
79 Correct 2027 ms 384672 KB Output is correct
80 Correct 1885 ms 385732 KB Output is correct
81 Correct 137 ms 268044 KB Output is correct
82 Correct 132 ms 262976 KB Output is correct
83 Correct 345 ms 276304 KB Output is correct
84 Correct 253 ms 271388 KB Output is correct
85 Correct 216 ms 270420 KB Output is correct
86 Correct 265 ms 271952 KB Output is correct
87 Correct 250 ms 269776 KB Output is correct
88 Correct 219 ms 269284 KB Output is correct
89 Correct 262 ms 271416 KB Output is correct
90 Correct 113 ms 268384 KB Output is correct
91 Correct 138 ms 268116 KB Output is correct
92 Correct 259 ms 271952 KB Output is correct
93 Correct 313 ms 272556 KB Output is correct
94 Correct 304 ms 272464 KB Output is correct
95 Correct 282 ms 272652 KB Output is correct
96 Correct 166 ms 261532 KB Output is correct
97 Correct 892 ms 318792 KB Output is correct
98 Correct 501 ms 287500 KB Output is correct
99 Correct 4584 ms 425100 KB Output is correct
100 Correct 785 ms 307536 KB Output is correct
101 Correct 2376 ms 374236 KB Output is correct
102 Correct 4835 ms 452580 KB Output is correct
103 Correct 3748 ms 434708 KB Output is correct
104 Correct 3645 ms 434640 KB Output is correct
105 Correct 2298 ms 429920 KB Output is correct
106 Correct 2476 ms 431056 KB Output is correct
107 Correct 1408 ms 349620 KB Output is correct
108 Correct 1271 ms 345812 KB Output is correct
109 Correct 1608 ms 353732 KB Output is correct
110 Correct 1527 ms 340912 KB Output is correct
111 Correct 1358 ms 339272 KB Output is correct
112 Correct 1572 ms 350800 KB Output is correct
113 Correct 430 ms 332628 KB Output is correct
114 Correct 850 ms 334520 KB Output is correct
115 Correct 1810 ms 353072 KB Output is correct
116 Correct 2163 ms 356364 KB Output is correct
117 Correct 2384 ms 355676 KB Output is correct
118 Correct 1783 ms 355500 KB Output is correct
119 Correct 893 ms 292128 KB Output is correct
120 Correct 702 ms 328812 KB Output is correct
121 Correct 945 ms 348228 KB Output is correct
122 Correct 943 ms 348768 KB Output is correct
123 Correct 1066 ms 351772 KB Output is correct
124 Correct 1097 ms 352088 KB Output is correct
125 Correct 1048 ms 352412 KB Output is correct
126 Correct 1110 ms 346512 KB Output is correct