Submission #487806

# Submission time Handle Problem Language Result Execution time Memory
487806 2021-11-16T17:19:42 Z Redhood New Home (APIO18_new_home) C++14
45 / 100
2464 ms 336748 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define sz(x) (int)(x).size()
typedef long long ll;
typedef long double ld;
using namespace std;

const int N = (int)1.5e6 + 10;
const int inf = 1e9;
struct seg{
    int a[4*N];
    int p[4*N];
    void clr(int x = inf){
        fill(a , a + 4 * N , x);
        fill(p , p + 4 * N , x);
    }
    void push(int v){
        int v1 = v << 1;
        for(auto u : {v1 , v1|1}){
            a[u] = min(a[u] , p[v]);
            p[u] = min(p[u] , p[v]);
        }
        p[v] = inf;
    }
    int get(int v , int tl , int tr , int l , int r){
        if(tl == l && tr == r){
            return a[v];
        }
        int mid = (tl + tr) >> 1 , v1 = v << 1;
        push(v);
        int ans = inf;
        if(l <= mid)
            ans = min(ans , get(v1, tl , mid , l , r));
        if(r > mid)
            ans = min(ans , get(v1 | 1,  mid + 1 , tr , max(l , mid + 1), r));
        return ans;
    }
    void upd(int v , int tl , int tr , int l , int r , int val){
        if(tl == l && tr == r){
            p[v] = min(p[v] , val);
            a[v] = min(a[v] , val);
            return;
        }
        int mid = (tl + tr) >> 1 , v1 = v << 1;
        push(v);
        if(l <= mid)
            upd(v1 , tl , mid , l , min(r , mid) , val);
        if(r > mid)
            upd(v1 | 1 , mid + 1 , tr , max(l , mid + 1) , r, val);
        a[v] = min(a[v1] , a[v1|1]);
    }
}MN,MX;
mt19937 rng(199999999973);
vector < pair < int , int >  > open[N];
vector < pair < int , int > > close[N];
multiset < int > type[N];
vector < int > pos[N];
int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    bool debug = 0;
    int n , k , q;
    if(!debug){
    cin >> n >> k >> q;
    }else{
        n = (int)3e5;
        k = (int)500;
        q = (int)3e5;
    }
    vector < int > x(n), t(n), a(n), b(n);


    vector < int > zipyear;
    for(int i = 0; i < n; ++i){
        if(!debug)
        cin >> x[i] >> t[i] >> a[i] >> b[i];
        else{

            x[i] = rng() % (int)1e8;
            t[i] = rng() % k + 1;
            a[i] = 1;
            b[i] = rng() % int(1e8) + 1;
            if(a[i] > b[i])swap(a[i], b[i]);
        }
        b[i]++;
        zipyear.pb(a[i]), zipyear.pb(b[i]);
    }


    vector < pair < int , int >>  que(q);
    for(auto &i : que){
        if(!debug)
        cin >> i.fi >> i.se;
        else{
            i.fi = rng() % (int)1e8;
            i.se = rng() % (int)1e8;

        }
        zipyear.pb(i.se);
    }

    sort(all(zipyear)), zipyear.erase(unique(all(zipyear)), zipyear.end());
    for(int i = 0; i < n; ++i){
        a[i] = lower_bound(all(zipyear), a[i]) - zipyear.begin();
        b[i] = lower_bound(all(zipyear), b[i]) - zipyear.begin();
    }
    for(int i = 0; i < q; ++i){
        que[i].se = lower_bound(all(zipyear), que[i].se) - zipyear.begin();
    }
    vector < int > answer(q, -1);

    vector < pair < int , int > > pr;
    for(int i = 0; i < q; ++i)
        pr.pb({que[i].se, i});
    sort(all(pr));

        int year = -1;

    bool ONE  = 1;
    for(int i = 0; i < n; ++i){
        if(a[i] != 0)
            ONE = 0;
    }



    if(k <= 400 && !ONE){

     for(int i = 0; i < n; ++i){
            open[a[i]].push_back({t[i], x[i]});
            close[b[i]].push_back({t[i], x[i]});
        }

        /// inc
        for(auto &i : pr){
            while(year + 1 <= i.fi){
                /// lol
                for(auto &shop : open[year + 1]){
                    type[shop.fi].insert(shop.se);
                }
                /// lol
                for(auto &shop : close[year + 1]){
                    type[shop.fi].erase(type[shop.fi].find(shop.se));
                }
                year++;
            }
            int ans = -1;

//
//            cout << "LOL " << i.fi << ' ' << "\n";
//            for(int t  =1 ;t <=k; ++t){
//                cout << "TYPE " << t << '\n';
//                for(auto &u : type[t])
//                    cout << u << ' ';
//                cout << endl;
//            }

            for(int t = 1;t <= k; ++t){
                if(type[t].empty()){
                    ans = -1;
                    break;
                }
                int dist = 1e9;
                auto it = type[t].lower_bound(que[i.se].fi);
                if(it != type[t].end())
                    dist = min(dist , *it - que[i.se].fi);
                if(it != type[t].begin()){
                    --it;
                    dist = min(dist , que[i.se].fi - *it);
                }
                ans = max(ans , dist);
            }
            answer[i.se] = ans;
        }
    }else if(true){
        vector < int > cord;
        cord = x;
        for(auto &i : que)
            cord.pb(i.fi);
        sort(all(cord));
        cord.erase(unique(all(cord)) , cord.end());

        for(int i = 0; i < n; ++i){
            x[i] = lower_bound(all(cord), x[i]) - cord.begin();
        }
     for(int i = 0; i < n; ++i){
            open[a[i]].push_back({t[i], x[i]});
            close[b[i]].push_back({t[i], x[i]});
        }
//        if(sz())




        MN.clr(), MX.clr();
        assert(sz(cord) <= N);
//        return 0;
        for(int i = 0; i < sz(cord); ++i){
            MN.upd(1 , 0 , N-1, i , i , cord[i]);
            MX.upd(1 , 0 , N-1, i , i , -cord[i]);
        }
        for(int i =  0 ; i< n; ++i){
            pos[t[i]].pb(x[i]);
        }


        auto put = [&](int L , int R){
//                return;
                if(L == R){
                    MN.upd(1 , 0, N-1 , L, L, cord[L]);
                    MX.upd(1 , 0, N-1 , L, L, -cord[L]);
                    return;
                }
                int l = L;
                int r = R;
                if(L > R)
                    assert(false);
                while(l != r){
//                    cout << l << ' ' << r << endl;
                    int m = (l + r) >> 1;
                    if(cord[m] - cord[L] <= cord[R] - cord[m]){
                        if(l + 1 == r)
                            l = r;
                        else
                            l = m;
                    }else
                        r = m;
                }
//                if(r - 1 < L)assert(false);
//                if(r > R)assert(false);
                assert(r -  1 >= L);
                assert(r <= R);

                MN.upd(1 , 0 , N - 1 , L , r - 1, cord[L]);
                MX.upd(1 , 0 , N - 1 , r , R , -cord[R]);
        };
        assert(N >= sz(cord));
//        return 0;
        auto putbegin = [&](int X){
//            assert(X>0&&X<sz(cord));
            MX.upd(1 , 0 , N-1, 0 , X, -cord[X]);
        };
        auto putend = [&](int X){
            MN.upd(1 , 0 , N - 1 , X , sz(cord) - 1,  cord[X]);
        };
        auto gt = [&](int X){
            assert(X >= 0 && X < sz(cord));
            int val = MN.get(1 , 0 , N - 1 , X , X);
            int val1 = MX.get(1 , 0 , N-1 , X , X);

            return max((cord[X]-val), -val1 - cord[X]);
        };

        for(int t = 1; t <= k; ++t){
            sort(all(pos[t]));
            /// 0
            if(pos[t].empty())
                continue;
            putbegin(pos[t][0]);
            putend(pos[t].back());

            for(int i = 0; i < sz(pos[t]) - 1; ++i){
                put(pos[t][i], pos[t][i+ 1]);
            }

        }
        bool done = 0;

//                cout << "HERE\n";
//        return 0;
        vector < int > diff(k + 1);
        for(int i = 0; i < n; ++i)
            diff[t[i]] = 1;
        for(int t = 1; t <= k; ++t){
            if(!diff[t]){
                for(int i = 0 ; i < q; ++i)
                    cout << -1 << '\n';
                return 0;
            }
        }
        for(auto &i : pr){
            while(year + 1 <= i.fi){
                /// lol
                for(auto &shop : open[year + 1]){
                    type[shop.fi].insert(shop.se);
                }
                /// lol
                for(auto &shop : close[year + 1]){


                    type[shop.fi].erase(type[shop.fi].find(shop.se));


                    if(type[shop.fi].empty()){
                        done = 1;
                        break;
                    }
                    auto it = type[shop.fi].lower_bound(shop.se);

                    if(it != type[shop.fi].begin() && it != type[shop.fi].end()){
                        auto it1 = it;it1--;
                        put(*it1 , *it);
                    }   else{
                        if(it == type[shop.fi].end()){
                            --it;
                            putend(*it);
                        }else{
                            putbegin(*it);
                        }
                    }
                }
                if(done)
                    break;
                year++;
            }
            if(done)
                break;
//            cout << " ITTER " << i.fi << ' ' << year << endl;
//
//            cout << "YEAR\n";
//            for(int t = 1; t <= k; ++t){
//                cout << "SZ " << t << ' ' << type[t].size() << endl;
//            }
//            cout << "LOL\n";
            int ps = lower_bound(all(cord) , que[i.se].fi) - cord.begin();
            answer[i.se] = gt(ps);
        }



    }
    for(int i = 0 ; i < q; ++i)
        cout << answer[i] << '\n';
    return 0;
}
/*
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10

2 1 3
1 1 1 4
1 1 1 6
1 3
1 5
1 7


1 1 1
100000000 1 1 1
1 1


*/
# Verdict Execution time Memory Grader output
1 Correct 117 ms 270348 KB Output is correct
2 Correct 75 ms 176320 KB Output is correct
3 Correct 74 ms 176428 KB Output is correct
4 Correct 77 ms 176312 KB Output is correct
5 Correct 91 ms 176464 KB Output is correct
6 Correct 83 ms 176452 KB Output is correct
7 Correct 76 ms 176488 KB Output is correct
8 Correct 77 ms 176436 KB Output is correct
9 Correct 77 ms 176392 KB Output is correct
10 Correct 76 ms 176492 KB Output is correct
11 Correct 82 ms 176496 KB Output is correct
12 Correct 78 ms 176452 KB Output is correct
13 Correct 84 ms 176496 KB Output is correct
14 Correct 76 ms 176404 KB Output is correct
15 Correct 75 ms 176412 KB Output is correct
16 Correct 79 ms 176380 KB Output is correct
17 Correct 77 ms 176436 KB Output is correct
18 Correct 80 ms 176492 KB Output is correct
19 Correct 82 ms 176432 KB Output is correct
20 Correct 76 ms 176392 KB Output is correct
21 Correct 118 ms 270324 KB Output is correct
22 Correct 77 ms 176452 KB Output is correct
23 Correct 76 ms 176476 KB Output is correct
24 Correct 80 ms 176500 KB Output is correct
25 Correct 90 ms 176496 KB Output is correct
26 Correct 78 ms 176488 KB Output is correct
27 Correct 77 ms 176476 KB Output is correct
28 Correct 75 ms 176448 KB Output is correct
29 Correct 75 ms 176364 KB Output is correct
30 Correct 92 ms 176484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 270348 KB Output is correct
2 Correct 75 ms 176320 KB Output is correct
3 Correct 74 ms 176428 KB Output is correct
4 Correct 77 ms 176312 KB Output is correct
5 Correct 91 ms 176464 KB Output is correct
6 Correct 83 ms 176452 KB Output is correct
7 Correct 76 ms 176488 KB Output is correct
8 Correct 77 ms 176436 KB Output is correct
9 Correct 77 ms 176392 KB Output is correct
10 Correct 76 ms 176492 KB Output is correct
11 Correct 82 ms 176496 KB Output is correct
12 Correct 78 ms 176452 KB Output is correct
13 Correct 84 ms 176496 KB Output is correct
14 Correct 76 ms 176404 KB Output is correct
15 Correct 75 ms 176412 KB Output is correct
16 Correct 79 ms 176380 KB Output is correct
17 Correct 77 ms 176436 KB Output is correct
18 Correct 80 ms 176492 KB Output is correct
19 Correct 82 ms 176432 KB Output is correct
20 Correct 76 ms 176392 KB Output is correct
21 Correct 118 ms 270324 KB Output is correct
22 Correct 77 ms 176452 KB Output is correct
23 Correct 76 ms 176476 KB Output is correct
24 Correct 80 ms 176500 KB Output is correct
25 Correct 90 ms 176496 KB Output is correct
26 Correct 78 ms 176488 KB Output is correct
27 Correct 77 ms 176476 KB Output is correct
28 Correct 75 ms 176448 KB Output is correct
29 Correct 75 ms 176364 KB Output is correct
30 Correct 92 ms 176484 KB Output is correct
31 Correct 1852 ms 186336 KB Output is correct
32 Correct 163 ms 181708 KB Output is correct
33 Correct 264 ms 184472 KB Output is correct
34 Correct 1443 ms 184472 KB Output is correct
35 Correct 913 ms 186280 KB Output is correct
36 Correct 315 ms 186136 KB Output is correct
37 Correct 245 ms 183616 KB Output is correct
38 Correct 193 ms 183508 KB Output is correct
39 Correct 187 ms 183312 KB Output is correct
40 Correct 174 ms 183356 KB Output is correct
41 Correct 349 ms 183604 KB Output is correct
42 Correct 283 ms 183340 KB Output is correct
43 Correct 212 ms 278304 KB Output is correct
44 Correct 295 ms 183572 KB Output is correct
45 Correct 208 ms 183496 KB Output is correct
46 Correct 166 ms 183680 KB Output is correct
47 Correct 150 ms 183048 KB Output is correct
48 Correct 145 ms 182976 KB Output is correct
49 Correct 174 ms 183172 KB Output is correct
50 Correct 264 ms 183424 KB Output is correct
51 Correct 158 ms 183272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1635 ms 309120 KB Output is correct
2 Correct 1311 ms 306928 KB Output is correct
3 Correct 1507 ms 316584 KB Output is correct
4 Correct 1578 ms 309796 KB Output is correct
5 Correct 1243 ms 306888 KB Output is correct
6 Correct 1280 ms 306748 KB Output is correct
7 Correct 1322 ms 316544 KB Output is correct
8 Correct 1489 ms 309416 KB Output is correct
9 Correct 1553 ms 308104 KB Output is correct
10 Correct 1388 ms 307524 KB Output is correct
11 Correct 1095 ms 306388 KB Output is correct
12 Correct 1211 ms 306920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2464 ms 314416 KB Output is correct
2 Correct 791 ms 311416 KB Output is correct
3 Correct 2237 ms 326080 KB Output is correct
4 Correct 1270 ms 336748 KB Output is correct
5 Correct 1432 ms 329080 KB Output is correct
6 Correct 1397 ms 329784 KB Output is correct
7 Correct 2153 ms 326024 KB Output is correct
8 Correct 2156 ms 326208 KB Output is correct
9 Correct 1311 ms 336692 KB Output is correct
10 Correct 1828 ms 329104 KB Output is correct
11 Correct 2208 ms 328052 KB Output is correct
12 Correct 2268 ms 326956 KB Output is correct
13 Correct 1220 ms 324452 KB Output is correct
14 Correct 1185 ms 324104 KB Output is correct
15 Correct 1326 ms 325352 KB Output is correct
16 Correct 1564 ms 326512 KB Output is correct
17 Correct 1357 ms 324928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 270348 KB Output is correct
2 Correct 75 ms 176320 KB Output is correct
3 Correct 74 ms 176428 KB Output is correct
4 Correct 77 ms 176312 KB Output is correct
5 Correct 91 ms 176464 KB Output is correct
6 Correct 83 ms 176452 KB Output is correct
7 Correct 76 ms 176488 KB Output is correct
8 Correct 77 ms 176436 KB Output is correct
9 Correct 77 ms 176392 KB Output is correct
10 Correct 76 ms 176492 KB Output is correct
11 Correct 82 ms 176496 KB Output is correct
12 Correct 78 ms 176452 KB Output is correct
13 Correct 84 ms 176496 KB Output is correct
14 Correct 76 ms 176404 KB Output is correct
15 Correct 75 ms 176412 KB Output is correct
16 Correct 79 ms 176380 KB Output is correct
17 Correct 77 ms 176436 KB Output is correct
18 Correct 80 ms 176492 KB Output is correct
19 Correct 82 ms 176432 KB Output is correct
20 Correct 76 ms 176392 KB Output is correct
21 Correct 118 ms 270324 KB Output is correct
22 Correct 77 ms 176452 KB Output is correct
23 Correct 76 ms 176476 KB Output is correct
24 Correct 80 ms 176500 KB Output is correct
25 Correct 90 ms 176496 KB Output is correct
26 Correct 78 ms 176488 KB Output is correct
27 Correct 77 ms 176476 KB Output is correct
28 Correct 75 ms 176448 KB Output is correct
29 Correct 75 ms 176364 KB Output is correct
30 Correct 92 ms 176484 KB Output is correct
31 Correct 1852 ms 186336 KB Output is correct
32 Correct 163 ms 181708 KB Output is correct
33 Correct 264 ms 184472 KB Output is correct
34 Correct 1443 ms 184472 KB Output is correct
35 Correct 913 ms 186280 KB Output is correct
36 Correct 315 ms 186136 KB Output is correct
37 Correct 245 ms 183616 KB Output is correct
38 Correct 193 ms 183508 KB Output is correct
39 Correct 187 ms 183312 KB Output is correct
40 Correct 174 ms 183356 KB Output is correct
41 Correct 349 ms 183604 KB Output is correct
42 Correct 283 ms 183340 KB Output is correct
43 Correct 212 ms 278304 KB Output is correct
44 Correct 295 ms 183572 KB Output is correct
45 Correct 208 ms 183496 KB Output is correct
46 Correct 166 ms 183680 KB Output is correct
47 Correct 150 ms 183048 KB Output is correct
48 Correct 145 ms 182976 KB Output is correct
49 Correct 174 ms 183172 KB Output is correct
50 Correct 264 ms 183424 KB Output is correct
51 Correct 158 ms 183272 KB Output is correct
52 Incorrect 371 ms 282852 KB Output isn't correct
53 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 117 ms 270348 KB Output is correct
2 Correct 75 ms 176320 KB Output is correct
3 Correct 74 ms 176428 KB Output is correct
4 Correct 77 ms 176312 KB Output is correct
5 Correct 91 ms 176464 KB Output is correct
6 Correct 83 ms 176452 KB Output is correct
7 Correct 76 ms 176488 KB Output is correct
8 Correct 77 ms 176436 KB Output is correct
9 Correct 77 ms 176392 KB Output is correct
10 Correct 76 ms 176492 KB Output is correct
11 Correct 82 ms 176496 KB Output is correct
12 Correct 78 ms 176452 KB Output is correct
13 Correct 84 ms 176496 KB Output is correct
14 Correct 76 ms 176404 KB Output is correct
15 Correct 75 ms 176412 KB Output is correct
16 Correct 79 ms 176380 KB Output is correct
17 Correct 77 ms 176436 KB Output is correct
18 Correct 80 ms 176492 KB Output is correct
19 Correct 82 ms 176432 KB Output is correct
20 Correct 76 ms 176392 KB Output is correct
21 Correct 118 ms 270324 KB Output is correct
22 Correct 77 ms 176452 KB Output is correct
23 Correct 76 ms 176476 KB Output is correct
24 Correct 80 ms 176500 KB Output is correct
25 Correct 90 ms 176496 KB Output is correct
26 Correct 78 ms 176488 KB Output is correct
27 Correct 77 ms 176476 KB Output is correct
28 Correct 75 ms 176448 KB Output is correct
29 Correct 75 ms 176364 KB Output is correct
30 Correct 92 ms 176484 KB Output is correct
31 Correct 1852 ms 186336 KB Output is correct
32 Correct 163 ms 181708 KB Output is correct
33 Correct 264 ms 184472 KB Output is correct
34 Correct 1443 ms 184472 KB Output is correct
35 Correct 913 ms 186280 KB Output is correct
36 Correct 315 ms 186136 KB Output is correct
37 Correct 245 ms 183616 KB Output is correct
38 Correct 193 ms 183508 KB Output is correct
39 Correct 187 ms 183312 KB Output is correct
40 Correct 174 ms 183356 KB Output is correct
41 Correct 349 ms 183604 KB Output is correct
42 Correct 283 ms 183340 KB Output is correct
43 Correct 212 ms 278304 KB Output is correct
44 Correct 295 ms 183572 KB Output is correct
45 Correct 208 ms 183496 KB Output is correct
46 Correct 166 ms 183680 KB Output is correct
47 Correct 150 ms 183048 KB Output is correct
48 Correct 145 ms 182976 KB Output is correct
49 Correct 174 ms 183172 KB Output is correct
50 Correct 264 ms 183424 KB Output is correct
51 Correct 158 ms 183272 KB Output is correct
52 Correct 1635 ms 309120 KB Output is correct
53 Correct 1311 ms 306928 KB Output is correct
54 Correct 1507 ms 316584 KB Output is correct
55 Correct 1578 ms 309796 KB Output is correct
56 Correct 1243 ms 306888 KB Output is correct
57 Correct 1280 ms 306748 KB Output is correct
58 Correct 1322 ms 316544 KB Output is correct
59 Correct 1489 ms 309416 KB Output is correct
60 Correct 1553 ms 308104 KB Output is correct
61 Correct 1388 ms 307524 KB Output is correct
62 Correct 1095 ms 306388 KB Output is correct
63 Correct 1211 ms 306920 KB Output is correct
64 Correct 2464 ms 314416 KB Output is correct
65 Correct 791 ms 311416 KB Output is correct
66 Correct 2237 ms 326080 KB Output is correct
67 Correct 1270 ms 336748 KB Output is correct
68 Correct 1432 ms 329080 KB Output is correct
69 Correct 1397 ms 329784 KB Output is correct
70 Correct 2153 ms 326024 KB Output is correct
71 Correct 2156 ms 326208 KB Output is correct
72 Correct 1311 ms 336692 KB Output is correct
73 Correct 1828 ms 329104 KB Output is correct
74 Correct 2208 ms 328052 KB Output is correct
75 Correct 2268 ms 326956 KB Output is correct
76 Correct 1220 ms 324452 KB Output is correct
77 Correct 1185 ms 324104 KB Output is correct
78 Correct 1326 ms 325352 KB Output is correct
79 Correct 1564 ms 326512 KB Output is correct
80 Correct 1357 ms 324928 KB Output is correct
81 Incorrect 371 ms 282852 KB Output isn't correct
82 Halted 0 ms 0 KB -