Submission #1100081

# Submission time Handle Problem Language Result Execution time Memory
1100081 2024-10-12T16:17:20 Z fve5 Nile (IOI24_nile) C++17
100 / 100
84 ms 14052 KB
#include "nile.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long i64;

struct CC {
    i64 sum;
    int best_even, best_odd, best;
    int par, size, l, r;

    void skip(int diff) { best = min(best, diff); }

    i64 get_best() const { return min({best, l & 1 ? best_odd : best_even}); }
    i64 get_cost() const { return sum + (size & 1) * get_best(); }
};

struct DSU {
    vector<CC> arr;
    vector<int> A, B;
    i64 cost;

    int find(int u) {
        if (arr[u].par == u) return u;
        return arr[u].par = find(arr[u].par);
    }

    void join(int i) {
        int u = find(i);
        int v = find(i + 1);
        
        if (u == v) return;
        if (arr[u].size < arr[v].size) swap(u, v);

        cost -= arr[u].get_cost();
        cost -= arr[v].get_cost();

        arr[u].size += arr[v].size;
        arr[u].r = max(arr[u].r, arr[v].r);
        arr[u].l = min(arr[u].l, arr[v].l);
        arr[v].par = u;

        arr[u].sum += arr[v].sum;
        arr[u].best = min(arr[u].best, arr[v].best);
        arr[u].best_even = min(arr[u].best_even, arr[v].best_even);
        arr[u].best_odd = min(arr[u].best_odd, arr[v].best_odd);

        cost += arr[u].get_cost();
    }

    void skip(int i) {
        int u = find(i);

        cost -= arr[u].get_cost();
        arr[u].skip(A[i] - B[i]);
        cost += arr[u].get_cost();
    }

    DSU(int N, const vector<int> &A, const vector<int> &B) : arr(N), A(A), B(B), cost(0) {
        for (int i = 0; i < N; i++) {
            arr[i] = {B[i], i & 1 ? (int)2e9 : A[i] - B[i], i & 1 ? A[i] - B[i] : (int)2e9, (int)2e9, i, 1, i, i};
            cost += arr[i].get_cost();
        }
    }
};

enum EventType { JOIN, SKIP, QUERY };
struct Event {
    EventType type;
    int time, position;
};

bool operator<(const Event &a, const Event &b) { return tie(a.time, a.type) < tie(b.time, b.type); }

vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
    const int N = W.size();
    const int Q = E.size();

    vector<int> order(N);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int u, int v) { return W[u] < W[v]; });
    sort(W.begin(), W.end());

    vector<int> sA(N), sB(N);

    for (int i = 0; i < N; i++) {
        sA[i] = A[order[i]];
        sB[i] = B[order[i]];
    }

    vector<Event> events;
    for (int i = 0; i < Q; i++)
        events.push_back({QUERY, E[i], i});
    for (int i = 0; i < N - 1; i++)
        events.push_back({JOIN, W[i + 1] - W[i], i});
    for (int i = 1; i < N - 1; i++)
        events.push_back({SKIP, W[i + 1] - W[i - 1], i});
    
    DSU dsu(N, sA, sB);
    vector<i64> ans(E.size());
    sort(events.begin(), events.end());

    for (auto [type, _, idx]: events) {
        switch (type) {
        case JOIN:
            dsu.join(idx);
            break;
        case SKIP:
            dsu.skip(idx);
            break;
        case QUERY:
            ans[idx] = dsu.cost;
            break;
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 11212 KB Output is correct
2 Correct 32 ms 10960 KB Output is correct
3 Correct 32 ms 10980 KB Output is correct
4 Correct 31 ms 10960 KB Output is correct
5 Correct 31 ms 10960 KB Output is correct
6 Correct 35 ms 11484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 11208 KB Output is correct
2 Correct 39 ms 11468 KB Output is correct
3 Correct 47 ms 11208 KB Output is correct
4 Correct 47 ms 10960 KB Output is correct
5 Correct 42 ms 10960 KB Output is correct
6 Correct 50 ms 10968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 1 ms 612 KB Output is correct
9 Correct 2 ms 612 KB Output is correct
10 Correct 1 ms 612 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 31 ms 11212 KB Output is correct
8 Correct 32 ms 10960 KB Output is correct
9 Correct 32 ms 10980 KB Output is correct
10 Correct 31 ms 10960 KB Output is correct
11 Correct 31 ms 10960 KB Output is correct
12 Correct 35 ms 11484 KB Output is correct
13 Correct 41 ms 11208 KB Output is correct
14 Correct 39 ms 11468 KB Output is correct
15 Correct 47 ms 11208 KB Output is correct
16 Correct 47 ms 10960 KB Output is correct
17 Correct 42 ms 10960 KB Output is correct
18 Correct 50 ms 10968 KB Output is correct
19 Correct 2 ms 856 KB Output is correct
20 Correct 1 ms 612 KB Output is correct
21 Correct 2 ms 612 KB Output is correct
22 Correct 1 ms 612 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 45 ms 10952 KB Output is correct
26 Correct 46 ms 11204 KB Output is correct
27 Correct 53 ms 10948 KB Output is correct
28 Correct 52 ms 11008 KB Output is correct
29 Correct 48 ms 11464 KB Output is correct
30 Correct 55 ms 11008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 11208 KB Output is correct
2 Correct 39 ms 11468 KB Output is correct
3 Correct 47 ms 11208 KB Output is correct
4 Correct 47 ms 10960 KB Output is correct
5 Correct 42 ms 10960 KB Output is correct
6 Correct 50 ms 10968 KB Output is correct
7 Correct 63 ms 13764 KB Output is correct
8 Correct 63 ms 13508 KB Output is correct
9 Correct 74 ms 13504 KB Output is correct
10 Correct 72 ms 13748 KB Output is correct
11 Correct 74 ms 13504 KB Output is correct
12 Correct 70 ms 13760 KB Output is correct
13 Correct 74 ms 14020 KB Output is correct
14 Correct 66 ms 13760 KB Output is correct
15 Correct 78 ms 13508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 31 ms 11212 KB Output is correct
9 Correct 32 ms 10960 KB Output is correct
10 Correct 32 ms 10980 KB Output is correct
11 Correct 31 ms 10960 KB Output is correct
12 Correct 31 ms 10960 KB Output is correct
13 Correct 35 ms 11484 KB Output is correct
14 Correct 41 ms 11208 KB Output is correct
15 Correct 39 ms 11468 KB Output is correct
16 Correct 47 ms 11208 KB Output is correct
17 Correct 47 ms 10960 KB Output is correct
18 Correct 42 ms 10960 KB Output is correct
19 Correct 50 ms 10968 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
21 Correct 1 ms 612 KB Output is correct
22 Correct 2 ms 612 KB Output is correct
23 Correct 1 ms 612 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 45 ms 10952 KB Output is correct
27 Correct 46 ms 11204 KB Output is correct
28 Correct 53 ms 10948 KB Output is correct
29 Correct 52 ms 11008 KB Output is correct
30 Correct 48 ms 11464 KB Output is correct
31 Correct 55 ms 11008 KB Output is correct
32 Correct 63 ms 13764 KB Output is correct
33 Correct 63 ms 13508 KB Output is correct
34 Correct 74 ms 13504 KB Output is correct
35 Correct 72 ms 13748 KB Output is correct
36 Correct 74 ms 13504 KB Output is correct
37 Correct 70 ms 13760 KB Output is correct
38 Correct 74 ms 14020 KB Output is correct
39 Correct 66 ms 13760 KB Output is correct
40 Correct 78 ms 13508 KB Output is correct
41 Correct 74 ms 14020 KB Output is correct
42 Correct 77 ms 13752 KB Output is correct
43 Correct 84 ms 13508 KB Output is correct
44 Correct 81 ms 13764 KB Output is correct
45 Correct 83 ms 14016 KB Output is correct
46 Correct 81 ms 13752 KB Output is correct
47 Correct 84 ms 14052 KB Output is correct
48 Correct 72 ms 13508 KB Output is correct
49 Correct 83 ms 13508 KB Output is correct