답안 #465620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
465620 2021-08-16T14:45:41 Z amirmohammad_nezami 새 집 (APIO18_new_home) C++14
100 / 100
2432 ms 110256 KB
//                                                          In the name of God 
//                                           We are everywhere while we are nowhere(Haj NEZAM...)
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define lc 2 * v
#define nt(v) v ^ 1
#define rc 2 * v + 1
#define PB push_back
#define MP make_pair
#define ll long long
// #define int long long
#define ld long double
#define mid (s + e) / 2
#define _sz(e) (int)e.size()
#define pii pair <int , int>
#define _all(e) e.begin() , e.end()
#define pll pair <long long , long long>
#define piii pair <int , pair <int , int> >
#define FAST ios::sync_with_stdio(0);cin.tie(0)
#define UNI(e) e.resize(unique(_all(e)) - e.begin())
 
#define kill(e) cout << e << '\n'
 
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx")
#pragma GCC optimize("O3,unroll-loops")

const int maxn = 6e5 + 5 , N = 400 + 5 , SQ = 55 , base = 879169 , mod = 1e9 + 7 , INF = 1e9 + 5 , lg = 11;
const ld eps = 1e-6;

struct node {
    int mnL = INF , mxR = -INF;
};

node seg[4 * maxn];

int x[maxn] , t[maxn] , a[maxn] , b[maxn] , ql[maxn] , qx[maxn];
int n , k , q , SZ , cnt_zero , res[maxn] , L[maxn] , R[maxn];
vector <piii> vec , comp1;
set <pii> active[maxn];
vector <int> comp2;

void compress() {
    sort(_all(comp1)) , sort(_all(comp2)); 
    UNI(comp2);
    for (int i = 0; i < n; ++i) {
        a[i] = lower_bound(_all(comp2) , a[i]) - comp2.begin();
        b[i] = lower_bound(_all(comp2) , b[i]) - comp2.begin();
        vec.PB({a[i] , {0 , i}}) , vec.PB({b[i] , {2 , i}});
    }
    for (int i = 0; i < q; ++i) {
        ql[i] = lower_bound(_all(comp2) , ql[i]) - comp2.begin();
        vec.PB({ql[i] , {1 , i}});
    }
    for (int i = 0; i < n + q; ++i) {
        if(!comp1[i].S.F) x[comp1[i].S.S] = i;
        else qx[comp1[i].S.S] = i;
    }
    sort(_all(vec));
}

void modify_L(int p , int r , int v = 1 , int s = 0 , int e = SZ) {
    if(e - s == 1) {
        L[s] = r;
        seg[v].mnL = (r != INF && r != -INF ? comp1[r].F + comp1[s].F : r);
        return ;
    }
    if(p < mid) modify_L(p , r , lc , s , mid);
    else modify_L(p , r , rc , mid , e);
    seg[v].mnL = min(seg[lc].mnL , seg[rc].mnL);
}

void modify_R(int p , int r , int v = 1 , int s = 0 , int e = SZ) {
    if(e - s == 1) {
        R[s] = r;
        seg[v].mxR = (r != INF && r != -INF ? comp1[r].F + comp1[s].F : r);
        return ;
    }
    if(p < mid) modify_R(p , r , lc , s , mid);
    else modify_R(p , r , rc , mid , e);
    seg[v].mxR = max(seg[lc].mxR , seg[rc].mxR);
}

int get_L(int l , int r , int p , int v = 1 , int s = 0 , int e = SZ) {
    if(l >= e || s >= r || seg[v].mnL > p) return -1;
    if(e - s == 1) return s;

    int res = get_L(l , r , p , rc , mid , e);
    return (res != -1 ? res : get_L(l , r , p , lc , s , mid));
}

int get_R(int l , int r , int p , int v = 1 , int s = 0 , int e = SZ) {
    if(l >= e || s >= r || seg[v].mxR < p) return -1;
    if(e - s == 1) return s;

    int res = get_R(l , r , p , lc , s , mid);
    return (res != -1 ? res : get_R(l , r , p , rc , mid , e));
}

void add_store(int st) {
    // if(a[st] <= ql[0] && ql[0] <= b[st])
    //     cout << "ADD STORE " << st << ' ' << t[st] << ' ' << x[st] << endl;
    if(!_sz(active[t[st]])) cnt_zero--;

    set <pii> ::iterator it = active[t[st]].lower_bound({x[st] , -1});
    R[x[st]] = (it != active[t[st]].end() ? it -> F : INF);
    L[x[st]] = (it != active[t[st]].begin() ? (--it) -> F : -INF);
    active[t[st]].insert({x[st] , st});

    modify_L(x[st] , L[x[st]]) , modify_R(x[st] , R[x[st]]);
    if(R[x[st]] != INF) modify_L(R[x[st]] , x[st]);
    if(L[x[st]] != -INF) modify_R(L[x[st]] , x[st]);
}

void rem_store(int st) {
    // cout << "REM STORE " << st << ' ' << x[st] << endl;
    if(R[x[st]] != INF) modify_L(R[x[st]] , L[x[st]]);
    if(L[x[st]] != -INF) modify_R(L[x[st]] , R[x[st]]);
    modify_L(x[st] , INF) , modify_R(x[st] , -INF);
    active[t[st]].erase({x[st] , st});

    if(!_sz(active[t[st]])) cnt_zero++;
}

int32_t main() {
    FAST;
    cin >> n >> k >> q;
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> t[i] >> a[i] >> b[i];
        comp1.PB({x[i] , {0 , i}}) , comp2.PB(a[i]) , comp2.PB(b[i]);
    }
    for (int i = 0; i < q; ++i) {
        cin >> qx[i] >> ql[i];
        comp1.PB({qx[i] , {1 , i}}) , comp2.PB(ql[i]);
    }
    compress();

    cnt_zero = k;
    SZ = _sz(comp1);
    fill(L , L + maxn , INF);
    fill(R , R + maxn , -INF);

    // for (int i = 0; i < n; ++i) {
    //     cout << x[i] << ' ' << t[i] << ' ' << a[i] << ' ' << b[i] << endl;
    // }  
    // cout << endl;

    // for (auto cur : comp1) {
    //     cout << cur.F << ' ' << cur.S.F << ' ' << cur.S.S << endl;
    // }
    // cout << endl;

    for (auto f : vec) {
        if(f.S.F == 0) {
            add_store(f.S.S);
        }
        else if(f.S.F == 1) {
            int qu = f.S.S;
            if(cnt_zero) {
                res[qu] = -1; continue;
            }
            // cout << qu << ' ' << qx[qu] << ' ' << ql[qu] << endl;
            int intial = comp1[qx[qu]].F;
            int pos1 = get_R(0 , qx[qu] + 1 , intial * 2);
            int pos2 = get_L(qx[qu] , SZ , intial * 2);

            int val1 = (pos1 == -1 ? INF : comp1[pos1].F);
            int val2 = (pos2 == -1 ? -INF : comp1[pos2].F);
            // cout << pos1 << ' ' << pos2 << endl;
            res[qu] = max(0 , max(intial - val1 , val2 - intial));
        }
        else {
            rem_store(f.S.S);
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << res[i] << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 17 ms 33228 KB Output is correct
3 Correct 18 ms 33228 KB Output is correct
4 Correct 17 ms 33228 KB Output is correct
5 Correct 19 ms 33268 KB Output is correct
6 Correct 19 ms 33268 KB Output is correct
7 Correct 19 ms 33256 KB Output is correct
8 Correct 19 ms 33356 KB Output is correct
9 Correct 19 ms 33264 KB Output is correct
10 Correct 19 ms 33356 KB Output is correct
11 Correct 19 ms 33272 KB Output is correct
12 Correct 19 ms 33356 KB Output is correct
13 Correct 21 ms 33380 KB Output is correct
14 Correct 18 ms 33356 KB Output is correct
15 Correct 19 ms 33356 KB Output is correct
16 Correct 20 ms 33356 KB Output is correct
17 Correct 20 ms 33356 KB Output is correct
18 Correct 18 ms 33248 KB Output is correct
19 Correct 19 ms 33388 KB Output is correct
20 Correct 18 ms 33356 KB Output is correct
21 Correct 18 ms 33356 KB Output is correct
22 Correct 19 ms 33356 KB Output is correct
23 Correct 19 ms 33376 KB Output is correct
24 Correct 19 ms 33356 KB Output is correct
25 Correct 19 ms 33356 KB Output is correct
26 Correct 19 ms 33356 KB Output is correct
27 Correct 19 ms 33356 KB Output is correct
28 Correct 23 ms 33388 KB Output is correct
29 Correct 19 ms 33356 KB Output is correct
30 Correct 19 ms 33264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 17 ms 33228 KB Output is correct
3 Correct 18 ms 33228 KB Output is correct
4 Correct 17 ms 33228 KB Output is correct
5 Correct 19 ms 33268 KB Output is correct
6 Correct 19 ms 33268 KB Output is correct
7 Correct 19 ms 33256 KB Output is correct
8 Correct 19 ms 33356 KB Output is correct
9 Correct 19 ms 33264 KB Output is correct
10 Correct 19 ms 33356 KB Output is correct
11 Correct 19 ms 33272 KB Output is correct
12 Correct 19 ms 33356 KB Output is correct
13 Correct 21 ms 33380 KB Output is correct
14 Correct 18 ms 33356 KB Output is correct
15 Correct 19 ms 33356 KB Output is correct
16 Correct 20 ms 33356 KB Output is correct
17 Correct 20 ms 33356 KB Output is correct
18 Correct 18 ms 33248 KB Output is correct
19 Correct 19 ms 33388 KB Output is correct
20 Correct 18 ms 33356 KB Output is correct
21 Correct 18 ms 33356 KB Output is correct
22 Correct 19 ms 33356 KB Output is correct
23 Correct 19 ms 33376 KB Output is correct
24 Correct 19 ms 33356 KB Output is correct
25 Correct 19 ms 33356 KB Output is correct
26 Correct 19 ms 33356 KB Output is correct
27 Correct 19 ms 33356 KB Output is correct
28 Correct 23 ms 33388 KB Output is correct
29 Correct 19 ms 33356 KB Output is correct
30 Correct 19 ms 33264 KB Output is correct
31 Correct 332 ms 47472 KB Output is correct
32 Correct 281 ms 42552 KB Output is correct
33 Correct 352 ms 45368 KB Output is correct
34 Correct 313 ms 45640 KB Output is correct
35 Correct 349 ms 47312 KB Output is correct
36 Correct 353 ms 47324 KB Output is correct
37 Correct 281 ms 44636 KB Output is correct
38 Correct 289 ms 44528 KB Output is correct
39 Correct 271 ms 44484 KB Output is correct
40 Correct 264 ms 44536 KB Output is correct
41 Correct 254 ms 44680 KB Output is correct
42 Correct 262 ms 44544 KB Output is correct
43 Correct 163 ms 45940 KB Output is correct
44 Correct 254 ms 44768 KB Output is correct
45 Correct 285 ms 44732 KB Output is correct
46 Correct 246 ms 44744 KB Output is correct
47 Correct 209 ms 44112 KB Output is correct
48 Correct 197 ms 44128 KB Output is correct
49 Correct 205 ms 44304 KB Output is correct
50 Correct 216 ms 44440 KB Output is correct
51 Correct 209 ms 44364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1525 ms 95816 KB Output is correct
2 Correct 2069 ms 107120 KB Output is correct
3 Correct 1152 ms 109632 KB Output is correct
4 Correct 1429 ms 109544 KB Output is correct
5 Correct 2027 ms 106688 KB Output is correct
6 Correct 2043 ms 107060 KB Output is correct
7 Correct 1095 ms 109720 KB Output is correct
8 Correct 1214 ms 109556 KB Output is correct
9 Correct 1307 ms 109432 KB Output is correct
10 Correct 1531 ms 108192 KB Output is correct
11 Correct 1172 ms 106624 KB Output is correct
12 Correct 1194 ms 108096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2065 ms 95428 KB Output is correct
2 Correct 1314 ms 90040 KB Output is correct
3 Correct 2409 ms 106976 KB Output is correct
4 Correct 1193 ms 107620 KB Output is correct
5 Correct 1612 ms 107400 KB Output is correct
6 Correct 1498 ms 107252 KB Output is correct
7 Correct 2167 ms 106516 KB Output is correct
8 Correct 2295 ms 106876 KB Output is correct
9 Correct 1248 ms 108756 KB Output is correct
10 Correct 1515 ms 108764 KB Output is correct
11 Correct 1714 ms 108788 KB Output is correct
12 Correct 1914 ms 107816 KB Output is correct
13 Correct 1130 ms 105572 KB Output is correct
14 Correct 1179 ms 104928 KB Output is correct
15 Correct 1281 ms 106420 KB Output is correct
16 Correct 1433 ms 107528 KB Output is correct
17 Correct 1344 ms 106016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 17 ms 33228 KB Output is correct
3 Correct 18 ms 33228 KB Output is correct
4 Correct 17 ms 33228 KB Output is correct
5 Correct 19 ms 33268 KB Output is correct
6 Correct 19 ms 33268 KB Output is correct
7 Correct 19 ms 33256 KB Output is correct
8 Correct 19 ms 33356 KB Output is correct
9 Correct 19 ms 33264 KB Output is correct
10 Correct 19 ms 33356 KB Output is correct
11 Correct 19 ms 33272 KB Output is correct
12 Correct 19 ms 33356 KB Output is correct
13 Correct 21 ms 33380 KB Output is correct
14 Correct 18 ms 33356 KB Output is correct
15 Correct 19 ms 33356 KB Output is correct
16 Correct 20 ms 33356 KB Output is correct
17 Correct 20 ms 33356 KB Output is correct
18 Correct 18 ms 33248 KB Output is correct
19 Correct 19 ms 33388 KB Output is correct
20 Correct 18 ms 33356 KB Output is correct
21 Correct 18 ms 33356 KB Output is correct
22 Correct 19 ms 33356 KB Output is correct
23 Correct 19 ms 33376 KB Output is correct
24 Correct 19 ms 33356 KB Output is correct
25 Correct 19 ms 33356 KB Output is correct
26 Correct 19 ms 33356 KB Output is correct
27 Correct 19 ms 33356 KB Output is correct
28 Correct 23 ms 33388 KB Output is correct
29 Correct 19 ms 33356 KB Output is correct
30 Correct 19 ms 33264 KB Output is correct
31 Correct 332 ms 47472 KB Output is correct
32 Correct 281 ms 42552 KB Output is correct
33 Correct 352 ms 45368 KB Output is correct
34 Correct 313 ms 45640 KB Output is correct
35 Correct 349 ms 47312 KB Output is correct
36 Correct 353 ms 47324 KB Output is correct
37 Correct 281 ms 44636 KB Output is correct
38 Correct 289 ms 44528 KB Output is correct
39 Correct 271 ms 44484 KB Output is correct
40 Correct 264 ms 44536 KB Output is correct
41 Correct 254 ms 44680 KB Output is correct
42 Correct 262 ms 44544 KB Output is correct
43 Correct 163 ms 45940 KB Output is correct
44 Correct 254 ms 44768 KB Output is correct
45 Correct 285 ms 44732 KB Output is correct
46 Correct 246 ms 44744 KB Output is correct
47 Correct 209 ms 44112 KB Output is correct
48 Correct 197 ms 44128 KB Output is correct
49 Correct 205 ms 44304 KB Output is correct
50 Correct 216 ms 44440 KB Output is correct
51 Correct 209 ms 44364 KB Output is correct
52 Correct 225 ms 47172 KB Output is correct
53 Correct 221 ms 45388 KB Output is correct
54 Correct 265 ms 47200 KB Output is correct
55 Correct 255 ms 45704 KB Output is correct
56 Correct 240 ms 46132 KB Output is correct
57 Correct 255 ms 45168 KB Output is correct
58 Correct 256 ms 45516 KB Output is correct
59 Correct 252 ms 46012 KB Output is correct
60 Correct 255 ms 44920 KB Output is correct
61 Correct 139 ms 46016 KB Output is correct
62 Correct 232 ms 47252 KB Output is correct
63 Correct 254 ms 46920 KB Output is correct
64 Correct 264 ms 46504 KB Output is correct
65 Correct 277 ms 45276 KB Output is correct
66 Correct 265 ms 44732 KB Output is correct
67 Correct 220 ms 42784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 17 ms 33228 KB Output is correct
3 Correct 18 ms 33228 KB Output is correct
4 Correct 17 ms 33228 KB Output is correct
5 Correct 19 ms 33268 KB Output is correct
6 Correct 19 ms 33268 KB Output is correct
7 Correct 19 ms 33256 KB Output is correct
8 Correct 19 ms 33356 KB Output is correct
9 Correct 19 ms 33264 KB Output is correct
10 Correct 19 ms 33356 KB Output is correct
11 Correct 19 ms 33272 KB Output is correct
12 Correct 19 ms 33356 KB Output is correct
13 Correct 21 ms 33380 KB Output is correct
14 Correct 18 ms 33356 KB Output is correct
15 Correct 19 ms 33356 KB Output is correct
16 Correct 20 ms 33356 KB Output is correct
17 Correct 20 ms 33356 KB Output is correct
18 Correct 18 ms 33248 KB Output is correct
19 Correct 19 ms 33388 KB Output is correct
20 Correct 18 ms 33356 KB Output is correct
21 Correct 18 ms 33356 KB Output is correct
22 Correct 19 ms 33356 KB Output is correct
23 Correct 19 ms 33376 KB Output is correct
24 Correct 19 ms 33356 KB Output is correct
25 Correct 19 ms 33356 KB Output is correct
26 Correct 19 ms 33356 KB Output is correct
27 Correct 19 ms 33356 KB Output is correct
28 Correct 23 ms 33388 KB Output is correct
29 Correct 19 ms 33356 KB Output is correct
30 Correct 19 ms 33264 KB Output is correct
31 Correct 332 ms 47472 KB Output is correct
32 Correct 281 ms 42552 KB Output is correct
33 Correct 352 ms 45368 KB Output is correct
34 Correct 313 ms 45640 KB Output is correct
35 Correct 349 ms 47312 KB Output is correct
36 Correct 353 ms 47324 KB Output is correct
37 Correct 281 ms 44636 KB Output is correct
38 Correct 289 ms 44528 KB Output is correct
39 Correct 271 ms 44484 KB Output is correct
40 Correct 264 ms 44536 KB Output is correct
41 Correct 254 ms 44680 KB Output is correct
42 Correct 262 ms 44544 KB Output is correct
43 Correct 163 ms 45940 KB Output is correct
44 Correct 254 ms 44768 KB Output is correct
45 Correct 285 ms 44732 KB Output is correct
46 Correct 246 ms 44744 KB Output is correct
47 Correct 209 ms 44112 KB Output is correct
48 Correct 197 ms 44128 KB Output is correct
49 Correct 205 ms 44304 KB Output is correct
50 Correct 216 ms 44440 KB Output is correct
51 Correct 209 ms 44364 KB Output is correct
52 Correct 1525 ms 95816 KB Output is correct
53 Correct 2069 ms 107120 KB Output is correct
54 Correct 1152 ms 109632 KB Output is correct
55 Correct 1429 ms 109544 KB Output is correct
56 Correct 2027 ms 106688 KB Output is correct
57 Correct 2043 ms 107060 KB Output is correct
58 Correct 1095 ms 109720 KB Output is correct
59 Correct 1214 ms 109556 KB Output is correct
60 Correct 1307 ms 109432 KB Output is correct
61 Correct 1531 ms 108192 KB Output is correct
62 Correct 1172 ms 106624 KB Output is correct
63 Correct 1194 ms 108096 KB Output is correct
64 Correct 2065 ms 95428 KB Output is correct
65 Correct 1314 ms 90040 KB Output is correct
66 Correct 2409 ms 106976 KB Output is correct
67 Correct 1193 ms 107620 KB Output is correct
68 Correct 1612 ms 107400 KB Output is correct
69 Correct 1498 ms 107252 KB Output is correct
70 Correct 2167 ms 106516 KB Output is correct
71 Correct 2295 ms 106876 KB Output is correct
72 Correct 1248 ms 108756 KB Output is correct
73 Correct 1515 ms 108764 KB Output is correct
74 Correct 1714 ms 108788 KB Output is correct
75 Correct 1914 ms 107816 KB Output is correct
76 Correct 1130 ms 105572 KB Output is correct
77 Correct 1179 ms 104928 KB Output is correct
78 Correct 1281 ms 106420 KB Output is correct
79 Correct 1433 ms 107528 KB Output is correct
80 Correct 1344 ms 106016 KB Output is correct
81 Correct 225 ms 47172 KB Output is correct
82 Correct 221 ms 45388 KB Output is correct
83 Correct 265 ms 47200 KB Output is correct
84 Correct 255 ms 45704 KB Output is correct
85 Correct 240 ms 46132 KB Output is correct
86 Correct 255 ms 45168 KB Output is correct
87 Correct 256 ms 45516 KB Output is correct
88 Correct 252 ms 46012 KB Output is correct
89 Correct 255 ms 44920 KB Output is correct
90 Correct 139 ms 46016 KB Output is correct
91 Correct 232 ms 47252 KB Output is correct
92 Correct 254 ms 46920 KB Output is correct
93 Correct 264 ms 46504 KB Output is correct
94 Correct 277 ms 45276 KB Output is correct
95 Correct 265 ms 44732 KB Output is correct
96 Correct 220 ms 42784 KB Output is correct
97 Correct 1324 ms 109616 KB Output is correct
98 Correct 1430 ms 82560 KB Output is correct
99 Correct 2341 ms 100240 KB Output is correct
100 Correct 1311 ms 100724 KB Output is correct
101 Correct 1641 ms 109264 KB Output is correct
102 Correct 2432 ms 109160 KB Output is correct
103 Correct 1690 ms 96348 KB Output is correct
104 Correct 1691 ms 95996 KB Output is correct
105 Correct 1379 ms 95100 KB Output is correct
106 Correct 1450 ms 95012 KB Output is correct
107 Correct 1430 ms 101800 KB Output is correct
108 Correct 1391 ms 104152 KB Output is correct
109 Correct 1573 ms 98428 KB Output is correct
110 Correct 1493 ms 101164 KB Output is correct
111 Correct 1514 ms 103488 KB Output is correct
112 Correct 1514 ms 97816 KB Output is correct
113 Correct 652 ms 100360 KB Output is correct
114 Correct 1380 ms 110256 KB Output is correct
115 Correct 1566 ms 107724 KB Output is correct
116 Correct 1648 ms 105616 KB Output is correct
117 Correct 1737 ms 99588 KB Output is correct
118 Correct 1653 ms 96972 KB Output is correct
119 Correct 1331 ms 82556 KB Output is correct
120 Correct 858 ms 92192 KB Output is correct
121 Correct 989 ms 93952 KB Output is correct
122 Correct 989 ms 93744 KB Output is correct
123 Correct 1056 ms 94540 KB Output is correct
124 Correct 1114 ms 95336 KB Output is correct
125 Correct 1112 ms 95048 KB Output is correct
126 Correct 1136 ms 95412 KB Output is correct