답안 #1108032

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108032 2024-11-02T15:46:44 Z mispertion 새 집 (APIO18_new_home) C++17
80 / 100
5000 ms 382496 KB
#include<bits/stdc++.h>
 
using namespace std;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
#define F first
#define S second
 
const ld PI = 3.1415926535;
const int N = 5e5 + 2;
const int M = 8e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
 
int mult(int a, int b) {
    return a * 1LL * b % mod;
}
 
int sum(int a, int b) {
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}
 
 
ll binpow(ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        ll b = binpow(a, n / 2);
        return b * b % mod;
    }
}
 
struct segtree{
    int t[M], l[M], r[M];
    int cur = 0;
 
    segtree(){
        t[0] = -infi;
        l[0] = -1;
        r[0] = -1;
    }
 
    void add(int v, int tl, int tr, int i, int x){
        if(tl == tr){
            t[v] = max(t[v], x);
            //cout << v << ' ' << l[v] << ' ' << r[v] << ' ' << tl << ' ' << tr << ' ' << t[v] << '\n';
            return;
        }
        int tm = (tl + tr) / 2;
        if(i <= tm){
            if(l[v] == -1){
                l[v] = ++cur;
                t[cur] = -infi;
                l[cur] = -1;
                r[cur] = -1;
            }
            add(l[v], tl, tm, i, x);
        }else{
            if(r[v] == -1){
                r[v] = ++cur;
                t[cur] = -infi;
                l[cur] = -1;
                r[cur] = -1;
            }
            add(r[v], tm + 1, tr, i, x);
        }
        t[v] = -infi;
        if(l[v] != -1)
            t[v] = max(t[v], t[l[v]]);
        if(r[v] != -1)
            t[v] = max(t[v], t[r[v]]);
        //cout << v << ' ' << l[v] << ' ' << r[v] << ' ' << tl << ' ' << tr << ' ' << t[v] << '\n';
    }
 
    void sett(int v, int tl, int tr, int i, int x){
        if(tl == tr){
            t[v] = x;
            //cout << v << ' ' << l[v] << ' ' << r[v] << ' ' << tl << ' ' << tr << ' ' << t[v] << '\n';
            return;
        }
        int tm = (tl + tr) / 2;
        if(i <= tm){
            if(l[v] == -1){
                l[v] = ++cur;
                t[cur] = -infi;
                l[cur] = -1;
                r[cur] = -1;
            }
            sett(l[v], tl, tm, i, x);
        }else{
            if(r[v] == -1){
                r[v] = ++cur;
                t[cur] = -infi;
                l[cur] = -1;
                r[cur] = -1;
            }
            sett(r[v], tm + 1, tr, i, x);
        }
        t[v] = -infi;
        if(l[v] != -1)
            t[v] = max(t[v], t[l[v]]);
        if(r[v] != -1)
            t[v] = max(t[v], t[r[v]]);
        //cout << v << ' ' << l[v] << ' ' << r[v] << ' ' << tl << ' ' << tr << ' ' << t[v] << '\n';
    }
 
    int get(int v, int tl, int tr, int lu, int ru){
        if(v == -1)
            return -infi;
        if(tl > ru || lu > tr)
            return -infi;
        if(lu <= tl && tr <= ru)
            return t[v];
        int tm = (tl + tr) / 2;
        return max(get(l[v], tl, tm, lu, ru), get(r[v], tm + 1, tr, lu, ru));
    }
};
 
int n, q, k, ans[N], L = 0, R = 2e8;
vector<pair<pii, pii>> evs;
multiset<int> cr[N];
unordered_map<int, multiset<int>> spl, spr;
segtree lt = segtree(), rt = segtree();
 
void add(int i, int x1, int x2){
    lt.add(0, L, R, i, x1);
    rt.add(0, L, R, i, x2);
    spl[i].insert(x1);
    spr[i].insert(x2);
}
 
void rem(int i, int x1, int x2){
    auto da = spl.find(i);
    auto ha = spr.find(i);
    auto ed = (--da->S.end());
    auto hd = (--ha->S.end());
    if(*ed == x1){
        da->S.erase(ed);
        if(da->S.size() > 0){
            lt.sett(0, L, R, i, *(--da->S.end()));
        }else{
            lt.sett(0, L, R, i, -infi);
        }
    }else{
        da->S.erase(da->S.find(x1));
    }
    if(*hd == x2){
        ha->S.erase(hd);
        if(ha->S.size() > 0)
            rt.sett(0, L, R, i, *(--ha->S.end()));
        else
            rt.sett(0, L, R, i, -infi);
    }else{
        ha->S.erase(ha->S.find(x2));
    }
}
 
void solve() {
    cin >> n >> k >> q;
    for(int i = 1; i <= n; i++){
        int x, tp, a, b;
        cin >> x >> tp >> a >> b;
        x *= 2;
        evs.push_back({{a, 1}, {x, tp}});
        evs.push_back({{b + 1, -1}, {x, tp}});
    }
    for(int i = 1; i <= q; i++){
        int l, y;
        cin >> l >> y;
        l *= 2;
        evs.push_back({{y, 2}, {l, i}});
    }
    sort(evs.begin(), evs.end());
    int cnt = k;
    for(auto ev : evs){
        //cout << ev.F.F << ' ' << ev.F.S << ' ' << ev.S.F << ' ' << ev.S.S << '\n';
        if(ev.F.S == 1){
            int x = ev.S.F, tp = ev.S.S;
            if(cr[tp].find(x) != cr[tp].end()){
                cr[tp].insert(x);
                continue;
            }
            if(cr[tp].size() == 0){
                cr[tp].insert(x);
                add(L, x, x - 2 * L);
                add(R, 2 * R - x, -x);
                cnt--;
                continue;
            }
            int l = -1, r = -1;
            auto it = cr[tp].upper_bound(x);
            if(it != cr[tp].end()){
                add(((*it) + x) / 2, *it, -x);
                r = *it;
            }else{
                add(R, 2 * R - x, -x);
            }
            it = cr[tp].lower_bound(x);
            if(it != cr[tp].begin()){
                it--;
                add((x + (*it)) / 2, x, -(*it));
                l = *it;
            }else{
                add(L, x, x - 2 * L);
            }
            if(l != -1 && r != -1){
                rem((r + l) / 2, r, -l);
            }
            if(r == -1 && l != -1){
                rem(R, 2 * R - l, -l);
            }
            if(r != -1 && l == -1){
                rem(L, r, r - 2 * L);
            }
            cr[tp].insert(x);
        }else if(ev.F.S == -1){
            int x = ev.S.F, tp = ev.S.S;
            int l = -1, r = -1;
            if(cr[tp].count(x) > 1){
                cr[tp].erase(cr[tp].find(x));
                continue;
            }
            auto it = cr[tp].upper_bound(x);
            if(it != cr[tp].end()){
                r = *it;
                rem((r + x) / 2, r, -x);
            }else{
                rem(R, 2 * R - x, -x);
            }
            it = cr[tp].lower_bound(x);
            if(it != cr[tp].begin()){
                it--;
                l = (*it);
                rem((x + l) / 2, x, -l);
            }else{
                rem(L, x, x - 2 * L);
            }
            cr[tp].erase(cr[tp].find(x));
            if(l == -1 && r == -1){
                cnt++;
            }else if(l == -1){
                add(L, r, r - 2 * L);
            }else if(r == -1){
                add(R, 2 * R - l, -l);
            }else{
                add((r + l) / 2, r, -l);
            }
        }else{
            int x = ev.S.F, i = ev.S.S;
            int ret = max(lt.get(0, L, R, L, x) - x, rt.get(0, L, R, x, R) + x);
            if(cnt != 0)
                ans[i] = -1;
            else
                ans[i] = (ret < 0 ? -1 : ret / 2);
        }    
        //cout << cnt << '\n';   
    }
    for(int i = 1; i <=q; i++)
        cout << ans[i] << ' ';
    cout << '\n';
}
 
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35152 KB Output is correct
2 Correct 7 ms 35152 KB Output is correct
3 Correct 7 ms 35152 KB Output is correct
4 Correct 7 ms 35152 KB Output is correct
5 Correct 7 ms 35408 KB Output is correct
6 Correct 7 ms 35408 KB Output is correct
7 Correct 7 ms 35408 KB Output is correct
8 Correct 7 ms 35408 KB Output is correct
9 Correct 7 ms 35408 KB Output is correct
10 Correct 8 ms 35664 KB Output is correct
11 Correct 8 ms 35408 KB Output is correct
12 Correct 9 ms 35556 KB Output is correct
13 Correct 7 ms 35408 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 7 ms 35408 KB Output is correct
16 Correct 7 ms 35408 KB Output is correct
17 Correct 7 ms 35408 KB Output is correct
18 Correct 7 ms 35408 KB Output is correct
19 Correct 8 ms 35408 KB Output is correct
20 Correct 7 ms 35408 KB Output is correct
21 Correct 7 ms 35408 KB Output is correct
22 Correct 8 ms 35408 KB Output is correct
23 Correct 7 ms 35408 KB Output is correct
24 Correct 7 ms 35408 KB Output is correct
25 Correct 8 ms 35408 KB Output is correct
26 Correct 7 ms 35408 KB Output is correct
27 Correct 7 ms 35408 KB Output is correct
28 Correct 7 ms 35408 KB Output is correct
29 Correct 8 ms 35408 KB Output is correct
30 Correct 7 ms 35436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35152 KB Output is correct
2 Correct 7 ms 35152 KB Output is correct
3 Correct 7 ms 35152 KB Output is correct
4 Correct 7 ms 35152 KB Output is correct
5 Correct 7 ms 35408 KB Output is correct
6 Correct 7 ms 35408 KB Output is correct
7 Correct 7 ms 35408 KB Output is correct
8 Correct 7 ms 35408 KB Output is correct
9 Correct 7 ms 35408 KB Output is correct
10 Correct 8 ms 35664 KB Output is correct
11 Correct 8 ms 35408 KB Output is correct
12 Correct 9 ms 35556 KB Output is correct
13 Correct 7 ms 35408 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 7 ms 35408 KB Output is correct
16 Correct 7 ms 35408 KB Output is correct
17 Correct 7 ms 35408 KB Output is correct
18 Correct 7 ms 35408 KB Output is correct
19 Correct 8 ms 35408 KB Output is correct
20 Correct 7 ms 35408 KB Output is correct
21 Correct 7 ms 35408 KB Output is correct
22 Correct 8 ms 35408 KB Output is correct
23 Correct 7 ms 35408 KB Output is correct
24 Correct 7 ms 35408 KB Output is correct
25 Correct 8 ms 35408 KB Output is correct
26 Correct 7 ms 35408 KB Output is correct
27 Correct 7 ms 35408 KB Output is correct
28 Correct 7 ms 35408 KB Output is correct
29 Correct 8 ms 35408 KB Output is correct
30 Correct 7 ms 35436 KB Output is correct
31 Correct 1012 ms 119288 KB Output is correct
32 Correct 118 ms 42176 KB Output is correct
33 Correct 889 ms 116920 KB Output is correct
34 Correct 863 ms 113312 KB Output is correct
35 Correct 927 ms 122296 KB Output is correct
36 Correct 1006 ms 122964 KB Output is correct
37 Correct 541 ms 114080 KB Output is correct
38 Correct 680 ms 114328 KB Output is correct
39 Correct 500 ms 110776 KB Output is correct
40 Correct 523 ms 113592 KB Output is correct
41 Correct 413 ms 78448 KB Output is correct
42 Correct 397 ms 78372 KB Output is correct
43 Correct 72 ms 42936 KB Output is correct
44 Correct 356 ms 72552 KB Output is correct
45 Correct 371 ms 75408 KB Output is correct
46 Correct 413 ms 76216 KB Output is correct
47 Correct 243 ms 67256 KB Output is correct
48 Correct 238 ms 70308 KB Output is correct
49 Correct 276 ms 73132 KB Output is correct
50 Correct 303 ms 72608 KB Output is correct
51 Correct 293 ms 75960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3003 ms 205880 KB Output is correct
2 Correct 1942 ms 224820 KB Output is correct
3 Correct 847 ms 123784 KB Output is correct
4 Correct 2167 ms 195244 KB Output is correct
5 Correct 1789 ms 224492 KB Output is correct
6 Correct 1755 ms 224908 KB Output is correct
7 Correct 833 ms 123720 KB Output is correct
8 Correct 2442 ms 195340 KB Output is correct
9 Correct 2596 ms 213976 KB Output is correct
10 Correct 1923 ms 223528 KB Output is correct
11 Correct 1503 ms 219168 KB Output is correct
12 Correct 1867 ms 220756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4442 ms 299924 KB Output is correct
2 Correct 2275 ms 65336 KB Output is correct
3 Correct 3804 ms 311720 KB Output is correct
4 Correct 1126 ms 122024 KB Output is correct
5 Correct 4660 ms 240040 KB Output is correct
6 Correct 3577 ms 213832 KB Output is correct
7 Correct 3840 ms 311152 KB Output is correct
8 Correct 4073 ms 311612 KB Output is correct
9 Correct 1050 ms 123048 KB Output is correct
10 Correct 3303 ms 231880 KB Output is correct
11 Correct 4308 ms 283760 KB Output is correct
12 Correct 4351 ms 312092 KB Output is correct
13 Correct 1875 ms 288940 KB Output is correct
14 Correct 2121 ms 293976 KB Output is correct
15 Correct 2317 ms 301224 KB Output is correct
16 Correct 2851 ms 300820 KB Output is correct
17 Correct 2398 ms 303916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35152 KB Output is correct
2 Correct 7 ms 35152 KB Output is correct
3 Correct 7 ms 35152 KB Output is correct
4 Correct 7 ms 35152 KB Output is correct
5 Correct 7 ms 35408 KB Output is correct
6 Correct 7 ms 35408 KB Output is correct
7 Correct 7 ms 35408 KB Output is correct
8 Correct 7 ms 35408 KB Output is correct
9 Correct 7 ms 35408 KB Output is correct
10 Correct 8 ms 35664 KB Output is correct
11 Correct 8 ms 35408 KB Output is correct
12 Correct 9 ms 35556 KB Output is correct
13 Correct 7 ms 35408 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 7 ms 35408 KB Output is correct
16 Correct 7 ms 35408 KB Output is correct
17 Correct 7 ms 35408 KB Output is correct
18 Correct 7 ms 35408 KB Output is correct
19 Correct 8 ms 35408 KB Output is correct
20 Correct 7 ms 35408 KB Output is correct
21 Correct 7 ms 35408 KB Output is correct
22 Correct 8 ms 35408 KB Output is correct
23 Correct 7 ms 35408 KB Output is correct
24 Correct 7 ms 35408 KB Output is correct
25 Correct 8 ms 35408 KB Output is correct
26 Correct 7 ms 35408 KB Output is correct
27 Correct 7 ms 35408 KB Output is correct
28 Correct 7 ms 35408 KB Output is correct
29 Correct 8 ms 35408 KB Output is correct
30 Correct 7 ms 35436 KB Output is correct
31 Correct 1012 ms 119288 KB Output is correct
32 Correct 118 ms 42176 KB Output is correct
33 Correct 889 ms 116920 KB Output is correct
34 Correct 863 ms 113312 KB Output is correct
35 Correct 927 ms 122296 KB Output is correct
36 Correct 1006 ms 122964 KB Output is correct
37 Correct 541 ms 114080 KB Output is correct
38 Correct 680 ms 114328 KB Output is correct
39 Correct 500 ms 110776 KB Output is correct
40 Correct 523 ms 113592 KB Output is correct
41 Correct 413 ms 78448 KB Output is correct
42 Correct 397 ms 78372 KB Output is correct
43 Correct 72 ms 42936 KB Output is correct
44 Correct 356 ms 72552 KB Output is correct
45 Correct 371 ms 75408 KB Output is correct
46 Correct 413 ms 76216 KB Output is correct
47 Correct 243 ms 67256 KB Output is correct
48 Correct 238 ms 70308 KB Output is correct
49 Correct 276 ms 73132 KB Output is correct
50 Correct 303 ms 72608 KB Output is correct
51 Correct 293 ms 75960 KB Output is correct
52 Correct 193 ms 53688 KB Output is correct
53 Correct 163 ms 44732 KB Output is correct
54 Correct 424 ms 77884 KB Output is correct
55 Correct 294 ms 70972 KB Output is correct
56 Correct 268 ms 66488 KB Output is correct
57 Correct 343 ms 74660 KB Output is correct
58 Correct 311 ms 69792 KB Output is correct
59 Correct 292 ms 64952 KB Output is correct
60 Correct 426 ms 73664 KB Output is correct
61 Correct 161 ms 53940 KB Output is correct
62 Correct 179 ms 53688 KB Output is correct
63 Correct 391 ms 67720 KB Output is correct
64 Correct 426 ms 72480 KB Output is correct
65 Correct 437 ms 75196 KB Output is correct
66 Correct 356 ms 78776 KB Output is correct
67 Correct 198 ms 42152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35152 KB Output is correct
2 Correct 7 ms 35152 KB Output is correct
3 Correct 7 ms 35152 KB Output is correct
4 Correct 7 ms 35152 KB Output is correct
5 Correct 7 ms 35408 KB Output is correct
6 Correct 7 ms 35408 KB Output is correct
7 Correct 7 ms 35408 KB Output is correct
8 Correct 7 ms 35408 KB Output is correct
9 Correct 7 ms 35408 KB Output is correct
10 Correct 8 ms 35664 KB Output is correct
11 Correct 8 ms 35408 KB Output is correct
12 Correct 9 ms 35556 KB Output is correct
13 Correct 7 ms 35408 KB Output is correct
14 Correct 8 ms 35420 KB Output is correct
15 Correct 7 ms 35408 KB Output is correct
16 Correct 7 ms 35408 KB Output is correct
17 Correct 7 ms 35408 KB Output is correct
18 Correct 7 ms 35408 KB Output is correct
19 Correct 8 ms 35408 KB Output is correct
20 Correct 7 ms 35408 KB Output is correct
21 Correct 7 ms 35408 KB Output is correct
22 Correct 8 ms 35408 KB Output is correct
23 Correct 7 ms 35408 KB Output is correct
24 Correct 7 ms 35408 KB Output is correct
25 Correct 8 ms 35408 KB Output is correct
26 Correct 7 ms 35408 KB Output is correct
27 Correct 7 ms 35408 KB Output is correct
28 Correct 7 ms 35408 KB Output is correct
29 Correct 8 ms 35408 KB Output is correct
30 Correct 7 ms 35436 KB Output is correct
31 Correct 1012 ms 119288 KB Output is correct
32 Correct 118 ms 42176 KB Output is correct
33 Correct 889 ms 116920 KB Output is correct
34 Correct 863 ms 113312 KB Output is correct
35 Correct 927 ms 122296 KB Output is correct
36 Correct 1006 ms 122964 KB Output is correct
37 Correct 541 ms 114080 KB Output is correct
38 Correct 680 ms 114328 KB Output is correct
39 Correct 500 ms 110776 KB Output is correct
40 Correct 523 ms 113592 KB Output is correct
41 Correct 413 ms 78448 KB Output is correct
42 Correct 397 ms 78372 KB Output is correct
43 Correct 72 ms 42936 KB Output is correct
44 Correct 356 ms 72552 KB Output is correct
45 Correct 371 ms 75408 KB Output is correct
46 Correct 413 ms 76216 KB Output is correct
47 Correct 243 ms 67256 KB Output is correct
48 Correct 238 ms 70308 KB Output is correct
49 Correct 276 ms 73132 KB Output is correct
50 Correct 303 ms 72608 KB Output is correct
51 Correct 293 ms 75960 KB Output is correct
52 Correct 3003 ms 205880 KB Output is correct
53 Correct 1942 ms 224820 KB Output is correct
54 Correct 847 ms 123784 KB Output is correct
55 Correct 2167 ms 195244 KB Output is correct
56 Correct 1789 ms 224492 KB Output is correct
57 Correct 1755 ms 224908 KB Output is correct
58 Correct 833 ms 123720 KB Output is correct
59 Correct 2442 ms 195340 KB Output is correct
60 Correct 2596 ms 213976 KB Output is correct
61 Correct 1923 ms 223528 KB Output is correct
62 Correct 1503 ms 219168 KB Output is correct
63 Correct 1867 ms 220756 KB Output is correct
64 Correct 4442 ms 299924 KB Output is correct
65 Correct 2275 ms 65336 KB Output is correct
66 Correct 3804 ms 311720 KB Output is correct
67 Correct 1126 ms 122024 KB Output is correct
68 Correct 4660 ms 240040 KB Output is correct
69 Correct 3577 ms 213832 KB Output is correct
70 Correct 3840 ms 311152 KB Output is correct
71 Correct 4073 ms 311612 KB Output is correct
72 Correct 1050 ms 123048 KB Output is correct
73 Correct 3303 ms 231880 KB Output is correct
74 Correct 4308 ms 283760 KB Output is correct
75 Correct 4351 ms 312092 KB Output is correct
76 Correct 1875 ms 288940 KB Output is correct
77 Correct 2121 ms 293976 KB Output is correct
78 Correct 2317 ms 301224 KB Output is correct
79 Correct 2851 ms 300820 KB Output is correct
80 Correct 2398 ms 303916 KB Output is correct
81 Correct 193 ms 53688 KB Output is correct
82 Correct 163 ms 44732 KB Output is correct
83 Correct 424 ms 77884 KB Output is correct
84 Correct 294 ms 70972 KB Output is correct
85 Correct 268 ms 66488 KB Output is correct
86 Correct 343 ms 74660 KB Output is correct
87 Correct 311 ms 69792 KB Output is correct
88 Correct 292 ms 64952 KB Output is correct
89 Correct 426 ms 73664 KB Output is correct
90 Correct 161 ms 53940 KB Output is correct
91 Correct 179 ms 53688 KB Output is correct
92 Correct 391 ms 67720 KB Output is correct
93 Correct 426 ms 72480 KB Output is correct
94 Correct 437 ms 75196 KB Output is correct
95 Correct 356 ms 78776 KB Output is correct
96 Correct 198 ms 42152 KB Output is correct
97 Correct 1179 ms 121936 KB Output is correct
98 Correct 1500 ms 60924 KB Output is correct
99 Execution timed out 5081 ms 382496 KB Time limit exceeded
100 Halted 0 ms 0 KB -