Submission #513571

# Submission time Handle Problem Language Result Execution time Memory
513571 2022-01-17T08:51:55 Z blue New Home (APIO18_new_home) C++17
47 / 100
5000 ms 373224 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

using vi = vector<int>;
#define sz(x) int(x.size())


const int mx = 300'000;

int n, k, q;
vi x(1+mx), t(1+mx), a(1+mx), b(1+mx);
vi l(1+mx), y(1+mx);


int get_time(int i)
{
    if(i <= n) return a[i];
    else if(i <= n+q) return y[i-n];
    else return b[i-n-q];
}

bool cmp(int i, int j)
{
    int ti = get_time(i), tj = get_time(j);
    if(ti != tj) return ti < tj;
    else return i < j;
}



const int ss = (1<<20);
const int INF = 2'0000'0000;

vi qlocs;

struct segtree
{
    vi l = vi(ss, 0);
    vi r = vi(ss, 0);
    // vi xpy = vi(ss, -INF);
    // vi xmy = vi(ss, -INF);
    multiset<int>* xpy = new multiset<int>[ss];
    multiset<int>* xmy = new multiset<int>[ss];

    void build(int i, int L, int R)
    {
        // cerr << "build: " << i << ' ' << L << ' ' << R << '\n';
        l[i] = L;
        r[i] = R;
        if(L == R) return;
        build(2*i, L, (L+R)/2);
        build(2*i+1, (L+R)/2+1, R);
    }

    void deploy_xpy(int i, int L, int R, int V)
    {
        if(R < qlocs[l[i]] || qlocs[r[i]] < L || R < L) return;
        else if(L <= qlocs[l[i]] && qlocs[r[i]] <= R)
            xpy[i].insert(V);
        else
        {
            deploy_xpy(2*i, L, R, V);
            deploy_xpy(2*i+1, L, R, V);
        }
    }

    void withdraw_xpy(int i, int L, int R, int V)
    {
        if(R < qlocs[l[i]] || qlocs[r[i]] < L || R < L) return;
        else if(L <= qlocs[l[i]] && qlocs[r[i]] <= R)
            xpy[i].erase(xpy[i].find(V));
        else
        {
            withdraw_xpy(2*i, L, R, V);
            withdraw_xpy(2*i+1, L, R, V);
        }
    }

    void deploy_xmy(int i, int L, int R, int V)
    {
        if(R < qlocs[l[i]] || qlocs[r[i]] < L || R < L) return;
        else if(L <= qlocs[l[i]] && qlocs[r[i]] <= R)
            xmy[i].insert(V);
        else
        {
            deploy_xmy(2*i, L, R, V);
            deploy_xmy(2*i+1, L, R, V);
        }
    }

    void withdraw_xmy(int i, int L, int R, int V)
    {
        if(R < qlocs[l[i]] || qlocs[r[i]] < L || R < L) return;
        else if(L <= qlocs[l[i]] && qlocs[r[i]] <= R)
            xmy[i].erase(xmy[i].find(V));
        else
        {
            withdraw_xmy(2*i, L, R, V);
            withdraw_xmy(2*i+1, L, R, V);
        }
    }

    int solve(int i, int X)
    {
        // cerr << "solve " << i << ' ' << X << '\n';
        int ans = -1;

        if(X < qlocs[l[i]] || qlocs[r[i]] < X) return ans;

        if(qlocs[l[i]] <= X && X <= qlocs[r[i]])
        {
            // cerr << "actually entering\n";





            // for(int ql: qlocs) cerr << "ql = " << ql << '\n';
            // for(int p: xpy[1]) cerr << "xpy = " << p << '\n';
            // for(int mv: xmy[1]) cerr << "xmy = " << mv << '\n';


            if(!xpy[i].empty())
                ans = max(ans, *xpy[i].rbegin() - X);

            if(!xmy[i].empty())
                ans = max(ans, X - *xmy[i].begin());
        }

        if(l[i] != r[i]) ans = max(ans, max(solve(2*i, X), solve(2*i+1, X)));

        return ans;
    }
};

segtree S;

void insert_pair(int u, int v)
{
    int m = (u+v + 1'000'000'000'000LL)/2LL - 500'000'000'000LL;
    // cerr << "insert " << u << ' ' << v << '\n';

    if(u % 2 == v%2)
    {
        S.deploy_xmy(1, u, m-1, u);
        S.deploy_xpy(1, m, v-1, v);
    }
    else
    {
        S.deploy_xmy(1, u, m, u);
        S.deploy_xpy(1, m+1, v, v);
    }
    // S.deploy_xmy(1, u, m-1, u);
    // S.deploy_xpy(1, m, v-1, v);
}

void erase_pair(int u, int v)
{
    int m = (u+v + 1'000'000'000'000LL)/2LL - 500'000'000'000LL;
    // cerr << "erase " << u << ' ' << v << '\n';
    // S.withdraw_xmy(1, u, m-1, u);
    // S.withdraw_xpy(1, m, v-1, v);

    if(u % 2 == v%2)
    {
        S.withdraw_xmy(1, u, m-1, u);
        S.withdraw_xpy(1, m, v-1, v);
    }
    else
    {
        S.withdraw_xmy(1, u, m, u);
        S.withdraw_xpy(1, m+1, v, v);
    }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> k >> q;
    for(int i = 1; i <= n; i++) cin >> x[i] >> t[i] >> a[i] >> b[i];
    for(int j = 1; j <= q; j++) cin >> l[j] >> y[j];

    vi obj;
    for(int i = 1; i <= n; i++)
    {
        obj.push_back(i);
        obj.push_back(n+q+i);
    }

    for(int j = 1; j <= q; j++)
        obj.push_back(n+j);

    sort(obj.begin(), obj.end(), cmp);

    vi ans(1+q, -1);


    set<int> qlocs_set;
    for(int j = 1; j <= q; j++) qlocs_set.insert(l[j]);

    qlocs = vi(1, 0);
    for(int g: qlocs_set) qlocs.push_back(g);

    int Z = sz(qlocs_set);

    // segtree S;
    S.build(1, 1, Z);

    multiset<int> locs[1+k];
    for(int typ = 1; typ <= k; typ++)
    {
        locs[typ].insert(0 - 1*INF);
        locs[typ].insert(INF + 1*INF);

        insert_pair(-INF, 2*INF);
    }

    vi occ(1+k, 0);
    int occ_ct = 0;

    // for(int ql: qlocs) cerr << "ql = " << ql << '\n';
    // for(int p: S.xpy[1]) cerr << "xpy = " << p << '\n';
    // for(int mv: S.xmy[1]) cerr << "xmy = " << mv << '\n';

    int answ = 0;


    for(int o: obj)
    {
        // cerr << "\n\n\n\n\n" << '\n';
        // cerr << "o = " << o << " : " << "time = " << get_time(o) << '\n';
        if(o <= n)
        {
            // cerr << "adding shop " << x[o] << ' ' << t[o] << '\n';
            int shop = o;
            if(occ[t[shop]] == 0) occ_ct++;
            occ[t[shop]]++;

            auto it = locs[t[shop]].lower_bound(x[shop]);
            int next_loc = *it;

            it--;
            int prev_loc = *it;

            erase_pair(prev_loc, next_loc);

            locs[t[shop]].insert(x[shop]);

            insert_pair(prev_loc, x[shop]);
            insert_pair(x[shop], next_loc);

            // for(int p: S.xpy[1]) cerr << "xpy = " << p << '\n';
            // for(int mv: S.xmy[1]) cerr << "xmy = " << mv << '\n';
        }
        else if(o <= n+q)
        {
            int qr = o-n;
            // cerr << "performing query " << qr << '\n';
            if(occ_ct == k) ans[qr] = S.solve(1, l[qr]);
            else ans[qr] = -1;
            // ans[qr] = S.get(l[qr]);

            answ++;
            if(answ == q) break;
        }
        else
        {
            int shop = o-n-q;

            // cerr << "removing shop " << shop << '\n';

            occ[t[shop]]--;
            if(occ[t[shop]] == 0) occ_ct--;

            locs[t[shop]].erase(locs[t[shop]].find(x[shop]));

            auto it = locs[t[shop]].lower_bound(x[shop]);
            int next_loc = *it;

            it--;
            int prev_loc = *it;

            erase_pair(prev_loc, x[shop]);
            erase_pair(x[shop], next_loc);

            insert_pair(prev_loc, next_loc);
        }
    }

    for(int j = 1; j <= q; j++) cout << ans[j] << '\n';

}
# Verdict Execution time Memory Grader output
1 Correct 48 ms 113996 KB Output is correct
2 Correct 47 ms 113952 KB Output is correct
3 Correct 51 ms 113988 KB Output is correct
4 Correct 57 ms 113984 KB Output is correct
5 Correct 49 ms 113988 KB Output is correct
6 Correct 51 ms 114152 KB Output is correct
7 Correct 46 ms 114220 KB Output is correct
8 Correct 48 ms 114292 KB Output is correct
9 Correct 48 ms 114336 KB Output is correct
10 Correct 52 ms 114164 KB Output is correct
11 Correct 49 ms 114056 KB Output is correct
12 Correct 59 ms 114048 KB Output is correct
13 Correct 52 ms 114064 KB Output is correct
14 Correct 49 ms 114044 KB Output is correct
15 Correct 56 ms 114124 KB Output is correct
16 Correct 50 ms 114244 KB Output is correct
17 Correct 52 ms 114056 KB Output is correct
18 Correct 52 ms 114260 KB Output is correct
19 Correct 55 ms 114228 KB Output is correct
20 Correct 48 ms 114112 KB Output is correct
21 Correct 47 ms 114164 KB Output is correct
22 Correct 50 ms 114244 KB Output is correct
23 Correct 54 ms 114244 KB Output is correct
24 Correct 53 ms 114188 KB Output is correct
25 Correct 56 ms 114124 KB Output is correct
26 Correct 50 ms 114036 KB Output is correct
27 Correct 48 ms 114116 KB Output is correct
28 Correct 52 ms 114000 KB Output is correct
29 Correct 53 ms 114012 KB Output is correct
30 Correct 51 ms 113988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 113996 KB Output is correct
2 Correct 47 ms 113952 KB Output is correct
3 Correct 51 ms 113988 KB Output is correct
4 Correct 57 ms 113984 KB Output is correct
5 Correct 49 ms 113988 KB Output is correct
6 Correct 51 ms 114152 KB Output is correct
7 Correct 46 ms 114220 KB Output is correct
8 Correct 48 ms 114292 KB Output is correct
9 Correct 48 ms 114336 KB Output is correct
10 Correct 52 ms 114164 KB Output is correct
11 Correct 49 ms 114056 KB Output is correct
12 Correct 59 ms 114048 KB Output is correct
13 Correct 52 ms 114064 KB Output is correct
14 Correct 49 ms 114044 KB Output is correct
15 Correct 56 ms 114124 KB Output is correct
16 Correct 50 ms 114244 KB Output is correct
17 Correct 52 ms 114056 KB Output is correct
18 Correct 52 ms 114260 KB Output is correct
19 Correct 55 ms 114228 KB Output is correct
20 Correct 48 ms 114112 KB Output is correct
21 Correct 47 ms 114164 KB Output is correct
22 Correct 50 ms 114244 KB Output is correct
23 Correct 54 ms 114244 KB Output is correct
24 Correct 53 ms 114188 KB Output is correct
25 Correct 56 ms 114124 KB Output is correct
26 Correct 50 ms 114036 KB Output is correct
27 Correct 48 ms 114116 KB Output is correct
28 Correct 52 ms 114000 KB Output is correct
29 Correct 53 ms 114012 KB Output is correct
30 Correct 51 ms 113988 KB Output is correct
31 Correct 2481 ms 159604 KB Output is correct
32 Correct 183 ms 116248 KB Output is correct
33 Correct 992 ms 126076 KB Output is correct
34 Correct 2526 ms 136468 KB Output is correct
35 Correct 1882 ms 150988 KB Output is correct
36 Correct 954 ms 133232 KB Output is correct
37 Correct 705 ms 120368 KB Output is correct
38 Correct 474 ms 119296 KB Output is correct
39 Correct 420 ms 118584 KB Output is correct
40 Correct 424 ms 118580 KB Output is correct
41 Correct 895 ms 119132 KB Output is correct
42 Correct 906 ms 119108 KB Output is correct
43 Correct 115 ms 118336 KB Output is correct
44 Correct 843 ms 119044 KB Output is correct
45 Correct 765 ms 118756 KB Output is correct
46 Correct 580 ms 118844 KB Output is correct
47 Correct 324 ms 118544 KB Output is correct
48 Correct 310 ms 118584 KB Output is correct
49 Correct 425 ms 118568 KB Output is correct
50 Correct 614 ms 118696 KB Output is correct
51 Correct 415 ms 118696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5050 ms 373224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5048 ms 292864 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 48 ms 113996 KB Output is correct
2 Correct 47 ms 113952 KB Output is correct
3 Correct 51 ms 113988 KB Output is correct
4 Correct 57 ms 113984 KB Output is correct
5 Correct 49 ms 113988 KB Output is correct
6 Correct 51 ms 114152 KB Output is correct
7 Correct 46 ms 114220 KB Output is correct
8 Correct 48 ms 114292 KB Output is correct
9 Correct 48 ms 114336 KB Output is correct
10 Correct 52 ms 114164 KB Output is correct
11 Correct 49 ms 114056 KB Output is correct
12 Correct 59 ms 114048 KB Output is correct
13 Correct 52 ms 114064 KB Output is correct
14 Correct 49 ms 114044 KB Output is correct
15 Correct 56 ms 114124 KB Output is correct
16 Correct 50 ms 114244 KB Output is correct
17 Correct 52 ms 114056 KB Output is correct
18 Correct 52 ms 114260 KB Output is correct
19 Correct 55 ms 114228 KB Output is correct
20 Correct 48 ms 114112 KB Output is correct
21 Correct 47 ms 114164 KB Output is correct
22 Correct 50 ms 114244 KB Output is correct
23 Correct 54 ms 114244 KB Output is correct
24 Correct 53 ms 114188 KB Output is correct
25 Correct 56 ms 114124 KB Output is correct
26 Correct 50 ms 114036 KB Output is correct
27 Correct 48 ms 114116 KB Output is correct
28 Correct 52 ms 114000 KB Output is correct
29 Correct 53 ms 114012 KB Output is correct
30 Correct 51 ms 113988 KB Output is correct
31 Correct 2481 ms 159604 KB Output is correct
32 Correct 183 ms 116248 KB Output is correct
33 Correct 992 ms 126076 KB Output is correct
34 Correct 2526 ms 136468 KB Output is correct
35 Correct 1882 ms 150988 KB Output is correct
36 Correct 954 ms 133232 KB Output is correct
37 Correct 705 ms 120368 KB Output is correct
38 Correct 474 ms 119296 KB Output is correct
39 Correct 420 ms 118584 KB Output is correct
40 Correct 424 ms 118580 KB Output is correct
41 Correct 895 ms 119132 KB Output is correct
42 Correct 906 ms 119108 KB Output is correct
43 Correct 115 ms 118336 KB Output is correct
44 Correct 843 ms 119044 KB Output is correct
45 Correct 765 ms 118756 KB Output is correct
46 Correct 580 ms 118844 KB Output is correct
47 Correct 324 ms 118544 KB Output is correct
48 Correct 310 ms 118584 KB Output is correct
49 Correct 425 ms 118568 KB Output is correct
50 Correct 614 ms 118696 KB Output is correct
51 Correct 415 ms 118696 KB Output is correct
52 Correct 860 ms 174568 KB Output is correct
53 Correct 838 ms 146328 KB Output is correct
54 Correct 2154 ms 182180 KB Output is correct
55 Correct 1026 ms 137820 KB Output is correct
56 Correct 974 ms 147212 KB Output is correct
57 Correct 1043 ms 124732 KB Output is correct
58 Correct 1099 ms 137992 KB Output is correct
59 Correct 1071 ms 147152 KB Output is correct
60 Correct 1150 ms 124828 KB Output is correct
61 Correct 139 ms 129864 KB Output is correct
62 Correct 843 ms 174640 KB Output is correct
63 Correct 1653 ms 170228 KB Output is correct
64 Correct 1984 ms 160928 KB Output is correct
65 Correct 2150 ms 132708 KB Output is correct
66 Correct 1029 ms 120024 KB Output is correct
67 Correct 544 ms 120016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 113996 KB Output is correct
2 Correct 47 ms 113952 KB Output is correct
3 Correct 51 ms 113988 KB Output is correct
4 Correct 57 ms 113984 KB Output is correct
5 Correct 49 ms 113988 KB Output is correct
6 Correct 51 ms 114152 KB Output is correct
7 Correct 46 ms 114220 KB Output is correct
8 Correct 48 ms 114292 KB Output is correct
9 Correct 48 ms 114336 KB Output is correct
10 Correct 52 ms 114164 KB Output is correct
11 Correct 49 ms 114056 KB Output is correct
12 Correct 59 ms 114048 KB Output is correct
13 Correct 52 ms 114064 KB Output is correct
14 Correct 49 ms 114044 KB Output is correct
15 Correct 56 ms 114124 KB Output is correct
16 Correct 50 ms 114244 KB Output is correct
17 Correct 52 ms 114056 KB Output is correct
18 Correct 52 ms 114260 KB Output is correct
19 Correct 55 ms 114228 KB Output is correct
20 Correct 48 ms 114112 KB Output is correct
21 Correct 47 ms 114164 KB Output is correct
22 Correct 50 ms 114244 KB Output is correct
23 Correct 54 ms 114244 KB Output is correct
24 Correct 53 ms 114188 KB Output is correct
25 Correct 56 ms 114124 KB Output is correct
26 Correct 50 ms 114036 KB Output is correct
27 Correct 48 ms 114116 KB Output is correct
28 Correct 52 ms 114000 KB Output is correct
29 Correct 53 ms 114012 KB Output is correct
30 Correct 51 ms 113988 KB Output is correct
31 Correct 2481 ms 159604 KB Output is correct
32 Correct 183 ms 116248 KB Output is correct
33 Correct 992 ms 126076 KB Output is correct
34 Correct 2526 ms 136468 KB Output is correct
35 Correct 1882 ms 150988 KB Output is correct
36 Correct 954 ms 133232 KB Output is correct
37 Correct 705 ms 120368 KB Output is correct
38 Correct 474 ms 119296 KB Output is correct
39 Correct 420 ms 118584 KB Output is correct
40 Correct 424 ms 118580 KB Output is correct
41 Correct 895 ms 119132 KB Output is correct
42 Correct 906 ms 119108 KB Output is correct
43 Correct 115 ms 118336 KB Output is correct
44 Correct 843 ms 119044 KB Output is correct
45 Correct 765 ms 118756 KB Output is correct
46 Correct 580 ms 118844 KB Output is correct
47 Correct 324 ms 118544 KB Output is correct
48 Correct 310 ms 118584 KB Output is correct
49 Correct 425 ms 118568 KB Output is correct
50 Correct 614 ms 118696 KB Output is correct
51 Correct 415 ms 118696 KB Output is correct
52 Execution timed out 5050 ms 373224 KB Time limit exceeded
53 Halted 0 ms 0 KB -