Submission #389859

# Submission time Handle Problem Language Result Execution time Memory
389859 2021-04-14T16:53:09 Z phathnv New Home (APIO18_new_home) C++11
80 / 100
5000 ms 288864 KB
#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

template<typename T>
void read(T &x){
    char ch;
    for(ch = getchar(); !isdigit(ch); ch = getchar());
    x = ch - '0';
    for(ch = getchar(); isdigit(ch); ch = getchar())
        x = x * 10 + ch - '0';
}

template<typename T>
void write(T x){
    if (x < 0){
        putchar('-');
        write(-x);
        return;
    }
    if (x < 10){
        putchar(x + '0');
    } else {
        write(x / 10);
        putchar(x % 10 + '0');
    }
}

const int N = 3e5 + 10;
const int INF = 4e8 + 10;

struct segment{
    int type, x, y, a, b;
    segment(int _type = 0, int _x = 0, int _y = 0, int _a = 0, int _b = 0){
        type = _type;
        x = _x;
        y = _y;
        a = _a;
        b = _b;
    }
    bool operator < (const segment &other){
        if (type != other.type)
            return type < other.type;
        if (x != other.x)
            return x < other.x;
        return y < other.y;
    }
};

struct query{
    int x, t, id;
    bool operator < (const query &other){
        return x < other.x;
    }
};

struct node{
    int x, id, idL, idR;
    node(int _x = 0, int _id = 0, int _idL = 0, int _idR = 0){
        x = _x;
        id = _id;
        idL = _idL;
        idR = _idR;
    }
};

bool operator < (const node &a, const node &b){
    if (a.x != b.x)
        return a.x < b.x;
    if (a.id != b.id)
        return a.id < b.id;
    if (a.idL != b.idL)
        return a.idL < b.idL;
    return a.idR < b.idR;
}

struct event{
    int t, type, id;
    event(int _t = 0, int _type = 0, int _id = 0){
        t = _t;
        type = _type;
        id = _id;
    }
    bool operator < (const event &other){
        if (t != other.t)
            return t < other.t;
        if (type != other.type)
            return type < other.type;
        return id < other.id;
    }
};

int n, k, nQ;
int x[N], t[N], a[N], b[N], ans[N];
query q[N];

int nSeg, nE;
segment seg[10 * N];
event e[N * 2];

int nTQ, tQ[N];

void readInput(){
    read(n);
    read(k);
    read(nQ);
    for(int i = 1; i <= n; i++){
        read(x[i]);
        read(t[i]);
        read(a[i]);
        read(b[i]);
    }
    for(int i = 1; i <= nQ; i++){
        read(q[i].x);
        read(q[i].t);
        q[i].id = i;
    }
}

void updateSeg(node &l, node &r, int a){
    int mid = (l.x + r.x) >> 1;
    seg[++nSeg] = segment(1, mid, mid - l.x, a, -1);
    if (l.idR != -1)
        seg[l.idR].b = a - 1;
    l.idR = nSeg;

    seg[++nSeg] = segment(-1, mid, r.x - mid, a, -1);
    if (r.idL != -1)
        seg[r.idL].b = a - 1;
    r.idL = nSeg;
}

vector <int> type[N];
void init(){
    for(int i = 1; i <= n; i++)
        x[i] <<= 1;
    for(int i = 1; i <= nQ; i++)
        q[i].x <<= 1;

    for(int i = 1; i <= n; i++)
        type[t[i]].push_back(i);

    for(int typ = 1; typ <= k; typ++){
        nE = 0;
        for(int pos : type[typ]){
            e[++nE] = event(a[pos], 1, pos);
            e[++nE] = event(b[pos] + 1, -1, pos);
        }
        sort(e + 1, e + 1 + nE);

        set <node> S;
        node l(-INF, -1, -1, -1), r(INF, -1, -1, -1);
        updateSeg(l, r, 0);
        S.insert(l);
        S.insert(r);
        for(int i = 1; i <= nE; i++){
            if (e[i].type == -1){
                auto it = S.lower_bound(node(x[e[i].id], e[i].id, -1, -1));
                auto itL = S.end(), itR = S.end();
                if (it != S.end()){
                    itR = it;
                    ++itR;
                }
                if (it != S.begin()){
                    itL = it;
                    --itL;
                }
                node d = *it;
                if (d.idL != -1)
                    seg[d.idL].b = e[i].t - 1;
                if (d.idR != -1)
                    seg[d.idR].b = e[i].t - 1;
                S.erase(it);

                if (itL != S.end() && itR != S.end()){
                    node nL = *itL, nR = *itR;
                    S.erase(itL);
                    S.erase(itR);
                    updateSeg(nL, nR, e[i].t);
                    S.insert(nL);
                    S.insert(nR);
                }

            } else {
                auto it = S.lower_bound(node(x[e[i].id], e[i].id, -1, -1));
                auto itR = it, itL = S.end();
                if (itR != S.begin()){
                    itL = itR;
                    --itL;
                }
                node d = node(x[e[i].id], e[i].id, -1, -1);
                if (itL != S.end()){
                    node nL = *itL;
                    S.erase(itL);
                    updateSeg(nL, d, e[i].t);
                    S.insert(nL);
                }
                if (itR != S.end()){
                    node nR = *itR;
                    S.erase(itR);
                    updateSeg(d, nR, e[i].t);
                    S.insert(nR);
                }
                S.insert(d);
            }
        }

        node nL = *S.begin(), nR = *(++S.begin());
        seg[nL.idR].b = INF;
        seg[nR.idL].b = INF;
    }
}

struct segmentTree{
    vector <int> d[N * 4];
    int cnt[N * 4], cur[N * 4];

    void fakeAddSeg(int id, int l, int r, int u, int v){
        if (v < l || r < u)
            return;
        if (u <= l && r <= v){
            cnt[id]++;
            return;
        }
        int mid = (l + r) >> 1;
        fakeAddSeg(id << 1, l, mid, u, v);
        fakeAddSeg(id << 1 | 1, mid + 1, r, u, v);
    }
    void normalize(int id, int l, int r){
        d[id].resize(cnt[id]);
        if (l == r)
            return;
        int mid = (l + r) >> 1;
        normalize(id << 1, l, mid);
        normalize(id << 1 | 1, mid + 1, r);
    }
    void addSeg(int id, int l, int r, int u, int v, int pos){
        if (v < l || r < u)
            return;
        if (u <= l && r <= v){
            d[id][cur[id]++] = pos;
            return;
        }
        int mid = (l + r) >> 1;
        addSeg(id << 1, l, mid, u, v, pos);
        addSeg(id << 1 | 1, mid + 1, r, u, v, pos);
    }
    void solve(int id, int l, int r, vector<query> &curQ){
        if (curQ.empty())
            return;

        auto ptr1 = d[id].begin();
        int maxVal = -INF;
        for(auto ptrQ = curQ.begin(); ptrQ != curQ.end(); ++ptrQ){
            while (ptr1 != d[id].end() && seg[*ptr1].type == -1 && seg[*ptr1].x <= ptrQ -> x){
                maxVal = max(maxVal, seg[*ptr1].x + seg[*ptr1].y);
                ++ptr1;
            }
            ans[ptrQ -> id] = max(ans[ptrQ -> id], maxVal - ptrQ -> x);
        }

        auto ptr2 = d[id].rbegin();
        maxVal = -INF;
        for(auto ptrQ = curQ.rbegin(); ptrQ != curQ.rend(); ++ptrQ){
            while (ptr2 != d[id].rend() && seg[*ptr2].type == 1 && seg[*ptr2].x >= ptrQ -> x){
                maxVal = max(maxVal, seg[*ptr2].y - seg[*ptr2].x);
                ++ptr2;
            }
            ans[ptrQ -> id] = max(ans[ptrQ -> id], maxVal + ptrQ -> x);
        }

        if (l == r)
            return;

        int mid = (l + r) >> 1;
        vector <query> lQ, rQ;
            for(query q : curQ)
                if (q.t <= mid)
                    lQ.push_back(q);
                else
                    rQ.push_back(q);

        solve(id << 1, l, mid, lQ);
        solve(id << 1 | 1, mid + 1, r, rQ);
    }
} ST;

void solve(){
    sort(seg + 1, seg + 1 + nSeg);
    sort(q + 1, q + 1 + nQ);
    nTQ = 0;
    for(int i = 1; i <= nQ; i++)
        tQ[++nTQ] = q[i].t;
    sort(tQ + 1, tQ + 1 + nTQ);
    nTQ = unique(tQ + 1, tQ + 1 + nTQ) - (tQ + 1);
    for(int i = 1; i <= nQ; i++)
        q[i].t = lower_bound(tQ + 1, tQ + 1 + nTQ, q[i].t) - tQ;
    for(int i = 1; i <= nSeg; i++){
        seg[i].a = lower_bound(tQ + 1, tQ + 1 + nTQ, seg[i].a) - tQ;
        seg[i].b = upper_bound(tQ + 1, tQ + 1 + nTQ, seg[i].b) - tQ - 1;
        if (seg[i].a <= seg[i].b)
            ST.fakeAddSeg(1, 1, nTQ, seg[i].a, seg[i].b);
    }
    ST.normalize(1, 1, nTQ);
    for(int i = 1; i <= nSeg; i++)
        if (seg[i].a <= seg[i].b)
            ST.addSeg(1, 1, nTQ, seg[i].a, seg[i].b, i);
    vector <query> curQ;
    for(int i = 1; i <= nQ; i++)
        curQ.push_back(q[i]);
    ST.solve(1, 1, nTQ, curQ);

    for(int i = 1; i <= nQ; i++){
        write(ans[i] >= (INF >> 1)? -1 : (ans[i] >> 1));
        putchar('\n');
    }
}

int main(){
    readInput();
    init();
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 55 ms 101292 KB Output is correct
2 Correct 56 ms 101280 KB Output is correct
3 Correct 56 ms 101324 KB Output is correct
4 Correct 56 ms 101248 KB Output is correct
5 Correct 59 ms 101308 KB Output is correct
6 Correct 58 ms 101312 KB Output is correct
7 Correct 57 ms 101412 KB Output is correct
8 Correct 58 ms 101400 KB Output is correct
9 Correct 62 ms 101364 KB Output is correct
10 Correct 58 ms 101360 KB Output is correct
11 Correct 57 ms 101376 KB Output is correct
12 Correct 61 ms 101384 KB Output is correct
13 Correct 61 ms 101376 KB Output is correct
14 Correct 55 ms 101396 KB Output is correct
15 Correct 62 ms 101340 KB Output is correct
16 Correct 61 ms 101444 KB Output is correct
17 Correct 61 ms 101420 KB Output is correct
18 Correct 66 ms 101376 KB Output is correct
19 Correct 68 ms 101388 KB Output is correct
20 Correct 70 ms 101316 KB Output is correct
21 Correct 67 ms 101284 KB Output is correct
22 Correct 61 ms 101360 KB Output is correct
23 Correct 61 ms 101440 KB Output is correct
24 Correct 58 ms 101384 KB Output is correct
25 Correct 61 ms 101364 KB Output is correct
26 Correct 60 ms 101296 KB Output is correct
27 Correct 59 ms 101316 KB Output is correct
28 Correct 65 ms 101400 KB Output is correct
29 Correct 71 ms 101312 KB Output is correct
30 Correct 63 ms 101376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 101292 KB Output is correct
2 Correct 56 ms 101280 KB Output is correct
3 Correct 56 ms 101324 KB Output is correct
4 Correct 56 ms 101248 KB Output is correct
5 Correct 59 ms 101308 KB Output is correct
6 Correct 58 ms 101312 KB Output is correct
7 Correct 57 ms 101412 KB Output is correct
8 Correct 58 ms 101400 KB Output is correct
9 Correct 62 ms 101364 KB Output is correct
10 Correct 58 ms 101360 KB Output is correct
11 Correct 57 ms 101376 KB Output is correct
12 Correct 61 ms 101384 KB Output is correct
13 Correct 61 ms 101376 KB Output is correct
14 Correct 55 ms 101396 KB Output is correct
15 Correct 62 ms 101340 KB Output is correct
16 Correct 61 ms 101444 KB Output is correct
17 Correct 61 ms 101420 KB Output is correct
18 Correct 66 ms 101376 KB Output is correct
19 Correct 68 ms 101388 KB Output is correct
20 Correct 70 ms 101316 KB Output is correct
21 Correct 67 ms 101284 KB Output is correct
22 Correct 61 ms 101360 KB Output is correct
23 Correct 61 ms 101440 KB Output is correct
24 Correct 58 ms 101384 KB Output is correct
25 Correct 61 ms 101364 KB Output is correct
26 Correct 60 ms 101296 KB Output is correct
27 Correct 59 ms 101316 KB Output is correct
28 Correct 65 ms 101400 KB Output is correct
29 Correct 71 ms 101312 KB Output is correct
30 Correct 63 ms 101376 KB Output is correct
31 Correct 760 ms 126052 KB Output is correct
32 Correct 240 ms 107860 KB Output is correct
33 Correct 714 ms 124148 KB Output is correct
34 Correct 717 ms 124844 KB Output is correct
35 Correct 774 ms 126008 KB Output is correct
36 Correct 807 ms 126024 KB Output is correct
37 Correct 564 ms 120896 KB Output is correct
38 Correct 569 ms 120804 KB Output is correct
39 Correct 458 ms 116356 KB Output is correct
40 Correct 481 ms 117356 KB Output is correct
41 Correct 621 ms 118944 KB Output is correct
42 Correct 641 ms 119460 KB Output is correct
43 Correct 141 ms 106052 KB Output is correct
44 Correct 621 ms 118716 KB Output is correct
45 Correct 598 ms 116888 KB Output is correct
46 Correct 473 ms 112064 KB Output is correct
47 Correct 335 ms 112736 KB Output is correct
48 Correct 317 ms 111544 KB Output is correct
49 Correct 409 ms 113804 KB Output is correct
50 Correct 475 ms 118480 KB Output is correct
51 Correct 412 ms 112612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1060 ms 132268 KB Output is correct
2 Correct 1329 ms 141900 KB Output is correct
3 Correct 1080 ms 153064 KB Output is correct
4 Correct 1042 ms 145272 KB Output is correct
5 Correct 1843 ms 146092 KB Output is correct
6 Correct 1362 ms 142172 KB Output is correct
7 Correct 954 ms 153352 KB Output is correct
8 Correct 943 ms 145096 KB Output is correct
9 Correct 1038 ms 143256 KB Output is correct
10 Correct 1135 ms 141676 KB Output is correct
11 Correct 1014 ms 140264 KB Output is correct
12 Correct 1016 ms 140768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3294 ms 197740 KB Output is correct
2 Correct 882 ms 136536 KB Output is correct
3 Correct 3270 ms 206676 KB Output is correct
4 Correct 3233 ms 226676 KB Output is correct
5 Correct 3323 ms 215344 KB Output is correct
6 Correct 3270 ms 214928 KB Output is correct
7 Correct 3818 ms 206516 KB Output is correct
8 Correct 3317 ms 206848 KB Output is correct
9 Correct 2595 ms 218876 KB Output is correct
10 Correct 2933 ms 214228 KB Output is correct
11 Correct 3178 ms 210232 KB Output is correct
12 Correct 3286 ms 208544 KB Output is correct
13 Correct 1679 ms 183780 KB Output is correct
14 Correct 1698 ms 184404 KB Output is correct
15 Correct 1989 ms 191196 KB Output is correct
16 Correct 2085 ms 197192 KB Output is correct
17 Correct 2145 ms 194660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 101292 KB Output is correct
2 Correct 56 ms 101280 KB Output is correct
3 Correct 56 ms 101324 KB Output is correct
4 Correct 56 ms 101248 KB Output is correct
5 Correct 59 ms 101308 KB Output is correct
6 Correct 58 ms 101312 KB Output is correct
7 Correct 57 ms 101412 KB Output is correct
8 Correct 58 ms 101400 KB Output is correct
9 Correct 62 ms 101364 KB Output is correct
10 Correct 58 ms 101360 KB Output is correct
11 Correct 57 ms 101376 KB Output is correct
12 Correct 61 ms 101384 KB Output is correct
13 Correct 61 ms 101376 KB Output is correct
14 Correct 55 ms 101396 KB Output is correct
15 Correct 62 ms 101340 KB Output is correct
16 Correct 61 ms 101444 KB Output is correct
17 Correct 61 ms 101420 KB Output is correct
18 Correct 66 ms 101376 KB Output is correct
19 Correct 68 ms 101388 KB Output is correct
20 Correct 70 ms 101316 KB Output is correct
21 Correct 67 ms 101284 KB Output is correct
22 Correct 61 ms 101360 KB Output is correct
23 Correct 61 ms 101440 KB Output is correct
24 Correct 58 ms 101384 KB Output is correct
25 Correct 61 ms 101364 KB Output is correct
26 Correct 60 ms 101296 KB Output is correct
27 Correct 59 ms 101316 KB Output is correct
28 Correct 65 ms 101400 KB Output is correct
29 Correct 71 ms 101312 KB Output is correct
30 Correct 63 ms 101376 KB Output is correct
31 Correct 760 ms 126052 KB Output is correct
32 Correct 240 ms 107860 KB Output is correct
33 Correct 714 ms 124148 KB Output is correct
34 Correct 717 ms 124844 KB Output is correct
35 Correct 774 ms 126008 KB Output is correct
36 Correct 807 ms 126024 KB Output is correct
37 Correct 564 ms 120896 KB Output is correct
38 Correct 569 ms 120804 KB Output is correct
39 Correct 458 ms 116356 KB Output is correct
40 Correct 481 ms 117356 KB Output is correct
41 Correct 621 ms 118944 KB Output is correct
42 Correct 641 ms 119460 KB Output is correct
43 Correct 141 ms 106052 KB Output is correct
44 Correct 621 ms 118716 KB Output is correct
45 Correct 598 ms 116888 KB Output is correct
46 Correct 473 ms 112064 KB Output is correct
47 Correct 335 ms 112736 KB Output is correct
48 Correct 317 ms 111544 KB Output is correct
49 Correct 409 ms 113804 KB Output is correct
50 Correct 475 ms 118480 KB Output is correct
51 Correct 412 ms 112612 KB Output is correct
52 Correct 739 ms 133136 KB Output is correct
53 Correct 802 ms 131512 KB Output is correct
54 Correct 725 ms 129924 KB Output is correct
55 Correct 602 ms 126292 KB Output is correct
56 Correct 590 ms 129064 KB Output is correct
57 Correct 621 ms 122472 KB Output is correct
58 Correct 647 ms 125668 KB Output is correct
59 Correct 646 ms 127836 KB Output is correct
60 Correct 643 ms 122556 KB Output is correct
61 Correct 143 ms 109728 KB Output is correct
62 Correct 740 ms 132976 KB Output is correct
63 Correct 717 ms 131008 KB Output is correct
64 Correct 723 ms 129964 KB Output is correct
65 Correct 740 ms 127664 KB Output is correct
66 Correct 664 ms 122696 KB Output is correct
67 Correct 220 ms 110468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 101292 KB Output is correct
2 Correct 56 ms 101280 KB Output is correct
3 Correct 56 ms 101324 KB Output is correct
4 Correct 56 ms 101248 KB Output is correct
5 Correct 59 ms 101308 KB Output is correct
6 Correct 58 ms 101312 KB Output is correct
7 Correct 57 ms 101412 KB Output is correct
8 Correct 58 ms 101400 KB Output is correct
9 Correct 62 ms 101364 KB Output is correct
10 Correct 58 ms 101360 KB Output is correct
11 Correct 57 ms 101376 KB Output is correct
12 Correct 61 ms 101384 KB Output is correct
13 Correct 61 ms 101376 KB Output is correct
14 Correct 55 ms 101396 KB Output is correct
15 Correct 62 ms 101340 KB Output is correct
16 Correct 61 ms 101444 KB Output is correct
17 Correct 61 ms 101420 KB Output is correct
18 Correct 66 ms 101376 KB Output is correct
19 Correct 68 ms 101388 KB Output is correct
20 Correct 70 ms 101316 KB Output is correct
21 Correct 67 ms 101284 KB Output is correct
22 Correct 61 ms 101360 KB Output is correct
23 Correct 61 ms 101440 KB Output is correct
24 Correct 58 ms 101384 KB Output is correct
25 Correct 61 ms 101364 KB Output is correct
26 Correct 60 ms 101296 KB Output is correct
27 Correct 59 ms 101316 KB Output is correct
28 Correct 65 ms 101400 KB Output is correct
29 Correct 71 ms 101312 KB Output is correct
30 Correct 63 ms 101376 KB Output is correct
31 Correct 760 ms 126052 KB Output is correct
32 Correct 240 ms 107860 KB Output is correct
33 Correct 714 ms 124148 KB Output is correct
34 Correct 717 ms 124844 KB Output is correct
35 Correct 774 ms 126008 KB Output is correct
36 Correct 807 ms 126024 KB Output is correct
37 Correct 564 ms 120896 KB Output is correct
38 Correct 569 ms 120804 KB Output is correct
39 Correct 458 ms 116356 KB Output is correct
40 Correct 481 ms 117356 KB Output is correct
41 Correct 621 ms 118944 KB Output is correct
42 Correct 641 ms 119460 KB Output is correct
43 Correct 141 ms 106052 KB Output is correct
44 Correct 621 ms 118716 KB Output is correct
45 Correct 598 ms 116888 KB Output is correct
46 Correct 473 ms 112064 KB Output is correct
47 Correct 335 ms 112736 KB Output is correct
48 Correct 317 ms 111544 KB Output is correct
49 Correct 409 ms 113804 KB Output is correct
50 Correct 475 ms 118480 KB Output is correct
51 Correct 412 ms 112612 KB Output is correct
52 Correct 1060 ms 132268 KB Output is correct
53 Correct 1329 ms 141900 KB Output is correct
54 Correct 1080 ms 153064 KB Output is correct
55 Correct 1042 ms 145272 KB Output is correct
56 Correct 1843 ms 146092 KB Output is correct
57 Correct 1362 ms 142172 KB Output is correct
58 Correct 954 ms 153352 KB Output is correct
59 Correct 943 ms 145096 KB Output is correct
60 Correct 1038 ms 143256 KB Output is correct
61 Correct 1135 ms 141676 KB Output is correct
62 Correct 1014 ms 140264 KB Output is correct
63 Correct 1016 ms 140768 KB Output is correct
64 Correct 3294 ms 197740 KB Output is correct
65 Correct 882 ms 136536 KB Output is correct
66 Correct 3270 ms 206676 KB Output is correct
67 Correct 3233 ms 226676 KB Output is correct
68 Correct 3323 ms 215344 KB Output is correct
69 Correct 3270 ms 214928 KB Output is correct
70 Correct 3818 ms 206516 KB Output is correct
71 Correct 3317 ms 206848 KB Output is correct
72 Correct 2595 ms 218876 KB Output is correct
73 Correct 2933 ms 214228 KB Output is correct
74 Correct 3178 ms 210232 KB Output is correct
75 Correct 3286 ms 208544 KB Output is correct
76 Correct 1679 ms 183780 KB Output is correct
77 Correct 1698 ms 184404 KB Output is correct
78 Correct 1989 ms 191196 KB Output is correct
79 Correct 2085 ms 197192 KB Output is correct
80 Correct 2145 ms 194660 KB Output is correct
81 Correct 739 ms 133136 KB Output is correct
82 Correct 802 ms 131512 KB Output is correct
83 Correct 725 ms 129924 KB Output is correct
84 Correct 602 ms 126292 KB Output is correct
85 Correct 590 ms 129064 KB Output is correct
86 Correct 621 ms 122472 KB Output is correct
87 Correct 647 ms 125668 KB Output is correct
88 Correct 646 ms 127836 KB Output is correct
89 Correct 643 ms 122556 KB Output is correct
90 Correct 143 ms 109728 KB Output is correct
91 Correct 740 ms 132976 KB Output is correct
92 Correct 717 ms 131008 KB Output is correct
93 Correct 723 ms 129964 KB Output is correct
94 Correct 740 ms 127664 KB Output is correct
95 Correct 664 ms 122696 KB Output is correct
96 Correct 220 ms 110468 KB Output is correct
97 Execution timed out 5117 ms 288864 KB Time limit exceeded
98 Halted 0 ms 0 KB -