Submission #513565

# Submission time Handle Problem Language Result Execution time Memory
513565 2022-01-17T08:47:06 Z blue New Home (APIO18_new_home) C++17
47 / 100
5000 ms 375576 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';


    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]);
        }
        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 53 ms 113960 KB Output is correct
2 Correct 42 ms 113956 KB Output is correct
3 Correct 47 ms 113992 KB Output is correct
4 Correct 43 ms 113988 KB Output is correct
5 Correct 44 ms 114060 KB Output is correct
6 Correct 51 ms 114132 KB Output is correct
7 Correct 43 ms 114120 KB Output is correct
8 Correct 45 ms 114252 KB Output is correct
9 Correct 46 ms 114312 KB Output is correct
10 Correct 50 ms 114220 KB Output is correct
11 Correct 43 ms 114124 KB Output is correct
12 Correct 44 ms 114096 KB Output is correct
13 Correct 44 ms 113980 KB Output is correct
14 Correct 51 ms 114072 KB Output is correct
15 Correct 47 ms 114116 KB Output is correct
16 Correct 53 ms 114244 KB Output is correct
17 Correct 45 ms 114116 KB Output is correct
18 Correct 46 ms 114120 KB Output is correct
19 Correct 45 ms 114176 KB Output is correct
20 Correct 44 ms 114084 KB Output is correct
21 Correct 42 ms 114144 KB Output is correct
22 Correct 43 ms 114296 KB Output is correct
23 Correct 45 ms 114172 KB Output is correct
24 Correct 44 ms 114252 KB Output is correct
25 Correct 44 ms 114040 KB Output is correct
26 Correct 47 ms 114116 KB Output is correct
27 Correct 48 ms 114068 KB Output is correct
28 Correct 45 ms 113996 KB Output is correct
29 Correct 43 ms 114008 KB Output is correct
30 Correct 43 ms 114016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 113960 KB Output is correct
2 Correct 42 ms 113956 KB Output is correct
3 Correct 47 ms 113992 KB Output is correct
4 Correct 43 ms 113988 KB Output is correct
5 Correct 44 ms 114060 KB Output is correct
6 Correct 51 ms 114132 KB Output is correct
7 Correct 43 ms 114120 KB Output is correct
8 Correct 45 ms 114252 KB Output is correct
9 Correct 46 ms 114312 KB Output is correct
10 Correct 50 ms 114220 KB Output is correct
11 Correct 43 ms 114124 KB Output is correct
12 Correct 44 ms 114096 KB Output is correct
13 Correct 44 ms 113980 KB Output is correct
14 Correct 51 ms 114072 KB Output is correct
15 Correct 47 ms 114116 KB Output is correct
16 Correct 53 ms 114244 KB Output is correct
17 Correct 45 ms 114116 KB Output is correct
18 Correct 46 ms 114120 KB Output is correct
19 Correct 45 ms 114176 KB Output is correct
20 Correct 44 ms 114084 KB Output is correct
21 Correct 42 ms 114144 KB Output is correct
22 Correct 43 ms 114296 KB Output is correct
23 Correct 45 ms 114172 KB Output is correct
24 Correct 44 ms 114252 KB Output is correct
25 Correct 44 ms 114040 KB Output is correct
26 Correct 47 ms 114116 KB Output is correct
27 Correct 48 ms 114068 KB Output is correct
28 Correct 45 ms 113996 KB Output is correct
29 Correct 43 ms 114008 KB Output is correct
30 Correct 43 ms 114016 KB Output is correct
31 Correct 2410 ms 159616 KB Output is correct
32 Correct 176 ms 117152 KB Output is correct
33 Correct 918 ms 128932 KB Output is correct
34 Correct 2386 ms 139456 KB Output is correct
35 Correct 1857 ms 153904 KB Output is correct
36 Correct 966 ms 136088 KB Output is correct
37 Correct 692 ms 123244 KB Output is correct
38 Correct 485 ms 122240 KB Output is correct
39 Correct 431 ms 121464 KB Output is correct
40 Correct 387 ms 121532 KB Output is correct
41 Correct 831 ms 122076 KB Output is correct
42 Correct 895 ms 122068 KB Output is correct
43 Correct 134 ms 120704 KB Output is correct
44 Correct 871 ms 121976 KB Output is correct
45 Correct 728 ms 121740 KB Output is correct
46 Correct 561 ms 121688 KB Output is correct
47 Correct 349 ms 121352 KB Output is correct
48 Correct 314 ms 121280 KB Output is correct
49 Correct 423 ms 121392 KB Output is correct
50 Correct 606 ms 121692 KB Output is correct
51 Correct 464 ms 121476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5086 ms 375576 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5080 ms 300164 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 113960 KB Output is correct
2 Correct 42 ms 113956 KB Output is correct
3 Correct 47 ms 113992 KB Output is correct
4 Correct 43 ms 113988 KB Output is correct
5 Correct 44 ms 114060 KB Output is correct
6 Correct 51 ms 114132 KB Output is correct
7 Correct 43 ms 114120 KB Output is correct
8 Correct 45 ms 114252 KB Output is correct
9 Correct 46 ms 114312 KB Output is correct
10 Correct 50 ms 114220 KB Output is correct
11 Correct 43 ms 114124 KB Output is correct
12 Correct 44 ms 114096 KB Output is correct
13 Correct 44 ms 113980 KB Output is correct
14 Correct 51 ms 114072 KB Output is correct
15 Correct 47 ms 114116 KB Output is correct
16 Correct 53 ms 114244 KB Output is correct
17 Correct 45 ms 114116 KB Output is correct
18 Correct 46 ms 114120 KB Output is correct
19 Correct 45 ms 114176 KB Output is correct
20 Correct 44 ms 114084 KB Output is correct
21 Correct 42 ms 114144 KB Output is correct
22 Correct 43 ms 114296 KB Output is correct
23 Correct 45 ms 114172 KB Output is correct
24 Correct 44 ms 114252 KB Output is correct
25 Correct 44 ms 114040 KB Output is correct
26 Correct 47 ms 114116 KB Output is correct
27 Correct 48 ms 114068 KB Output is correct
28 Correct 45 ms 113996 KB Output is correct
29 Correct 43 ms 114008 KB Output is correct
30 Correct 43 ms 114016 KB Output is correct
31 Correct 2410 ms 159616 KB Output is correct
32 Correct 176 ms 117152 KB Output is correct
33 Correct 918 ms 128932 KB Output is correct
34 Correct 2386 ms 139456 KB Output is correct
35 Correct 1857 ms 153904 KB Output is correct
36 Correct 966 ms 136088 KB Output is correct
37 Correct 692 ms 123244 KB Output is correct
38 Correct 485 ms 122240 KB Output is correct
39 Correct 431 ms 121464 KB Output is correct
40 Correct 387 ms 121532 KB Output is correct
41 Correct 831 ms 122076 KB Output is correct
42 Correct 895 ms 122068 KB Output is correct
43 Correct 134 ms 120704 KB Output is correct
44 Correct 871 ms 121976 KB Output is correct
45 Correct 728 ms 121740 KB Output is correct
46 Correct 561 ms 121688 KB Output is correct
47 Correct 349 ms 121352 KB Output is correct
48 Correct 314 ms 121280 KB Output is correct
49 Correct 423 ms 121392 KB Output is correct
50 Correct 606 ms 121692 KB Output is correct
51 Correct 464 ms 121476 KB Output is correct
52 Correct 796 ms 177704 KB Output is correct
53 Correct 851 ms 149412 KB Output is correct
54 Correct 2219 ms 185200 KB Output is correct
55 Correct 1074 ms 140856 KB Output is correct
56 Correct 959 ms 150180 KB Output is correct
57 Correct 1152 ms 127744 KB Output is correct
58 Correct 1185 ms 140956 KB Output is correct
59 Correct 1053 ms 150172 KB Output is correct
60 Correct 1070 ms 127888 KB Output is correct
61 Correct 143 ms 132232 KB Output is correct
62 Correct 816 ms 177668 KB Output is correct
63 Correct 1639 ms 173252 KB Output is correct
64 Correct 2159 ms 163876 KB Output is correct
65 Correct 2329 ms 135748 KB Output is correct
66 Correct 1148 ms 122868 KB Output is correct
67 Correct 547 ms 121260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 113960 KB Output is correct
2 Correct 42 ms 113956 KB Output is correct
3 Correct 47 ms 113992 KB Output is correct
4 Correct 43 ms 113988 KB Output is correct
5 Correct 44 ms 114060 KB Output is correct
6 Correct 51 ms 114132 KB Output is correct
7 Correct 43 ms 114120 KB Output is correct
8 Correct 45 ms 114252 KB Output is correct
9 Correct 46 ms 114312 KB Output is correct
10 Correct 50 ms 114220 KB Output is correct
11 Correct 43 ms 114124 KB Output is correct
12 Correct 44 ms 114096 KB Output is correct
13 Correct 44 ms 113980 KB Output is correct
14 Correct 51 ms 114072 KB Output is correct
15 Correct 47 ms 114116 KB Output is correct
16 Correct 53 ms 114244 KB Output is correct
17 Correct 45 ms 114116 KB Output is correct
18 Correct 46 ms 114120 KB Output is correct
19 Correct 45 ms 114176 KB Output is correct
20 Correct 44 ms 114084 KB Output is correct
21 Correct 42 ms 114144 KB Output is correct
22 Correct 43 ms 114296 KB Output is correct
23 Correct 45 ms 114172 KB Output is correct
24 Correct 44 ms 114252 KB Output is correct
25 Correct 44 ms 114040 KB Output is correct
26 Correct 47 ms 114116 KB Output is correct
27 Correct 48 ms 114068 KB Output is correct
28 Correct 45 ms 113996 KB Output is correct
29 Correct 43 ms 114008 KB Output is correct
30 Correct 43 ms 114016 KB Output is correct
31 Correct 2410 ms 159616 KB Output is correct
32 Correct 176 ms 117152 KB Output is correct
33 Correct 918 ms 128932 KB Output is correct
34 Correct 2386 ms 139456 KB Output is correct
35 Correct 1857 ms 153904 KB Output is correct
36 Correct 966 ms 136088 KB Output is correct
37 Correct 692 ms 123244 KB Output is correct
38 Correct 485 ms 122240 KB Output is correct
39 Correct 431 ms 121464 KB Output is correct
40 Correct 387 ms 121532 KB Output is correct
41 Correct 831 ms 122076 KB Output is correct
42 Correct 895 ms 122068 KB Output is correct
43 Correct 134 ms 120704 KB Output is correct
44 Correct 871 ms 121976 KB Output is correct
45 Correct 728 ms 121740 KB Output is correct
46 Correct 561 ms 121688 KB Output is correct
47 Correct 349 ms 121352 KB Output is correct
48 Correct 314 ms 121280 KB Output is correct
49 Correct 423 ms 121392 KB Output is correct
50 Correct 606 ms 121692 KB Output is correct
51 Correct 464 ms 121476 KB Output is correct
52 Execution timed out 5086 ms 375576 KB Time limit exceeded
53 Halted 0 ms 0 KB -