답안 #1099752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1099752 2024-10-12T05:10:24 Z model_code 나일강 (IOI24_nile) C++17
100 / 100
73 ms 8228 KB
// correct/arshia_solution.cpp

#include "nile.h"
#include <tuple>
#include <vector>
#include <numeric>
#include <climits>
#include <algorithm>
#include <cassert>
using namespace std;

struct DSU {
    vector<int> parent, size;
    vector<int> even, odd;
    long long cost = 0;

    DSU(vector<int> &A, vector<int> &B) : parent(A.size(), -1), size(A.size(), 1), even(A.size()), odd(A.size(), INT_MAX) {
        for (int i = 0; i < A.size(); i++) {
            even[i] = A[i] - B[i];
            cost += even[i];
        }
    }
    int get_par(int x) {
        return ~parent[x] ? parent[x] = get_par(parent[x]) : x;
    }
    void merge(int v) {
        int u = get_par(v - 1);
        for (int x : {u, v})
            if (size[x] & 1)
                cost -= even[x];

        if (size[u] & 1)
            swap(even[v], odd[v]);
        odd[u] = min(odd[u], odd[v]);
        even[u] = min(even[u], even[v]);

        parent[v] = u;
        size[u] += size[v];
        if (size[u] & 1)
            cost += even[u];
    }
    void consider(int v, int w) {
        int u = get_par(v);
        if (v - u & 1) {
            if (size[u] & 1)
                cost -= even[u];
            even[u] = min(even[u], w);
            if (size[u] & 1)
                cost += even[u];
        }
        else
            odd[u] = min(odd[u], w);
    }
};

vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
    int n = W.size(), q = E.size();
    if (n == 1)
        return vector<long long>(q, A[0]);

    vector<tuple<int, int, int>> helper;
    for (int i = 0; i < n; i++)
        helper.push_back({W[i], A[i], B[i]});
    sort(helper.begin(), helper.end());
    for (int i = 0; i < n; i++)
        tie(W[i], A[i], B[i]) = helper[i];

    vector<int> diff1(n - 1);
    iota(diff1.begin(), diff1.end(), 1);
    sort(diff1.begin(), diff1.end(), [&](int i, int j) {
        return W[i] - W[i - 1] < W[j] - W[j - 1];
    });

    vector<int> diff2(n - 2);
    iota(diff2.begin(), diff2.end(), 1);
    sort(diff2.begin(), diff2.end(), [&](int i, int j) {
        return W[i + 1] - W[i - 1] < W[j + 1] - W[j - 1];
    });

    vector<int> indexes(q);
    iota(indexes.begin(), indexes.end(), 0);
    sort(indexes.begin(), indexes.end(), [&](int i, int j) {
        return E[i] < E[j];
    });

    DSU dsu(A, B);
    int p1 = 0, p2 = 0;
    vector<long long> costs(q);
    long long total = accumulate(B.begin(), B.end(), 0LL);
    for (int index : indexes) {
        while (p1 < n - 1 && W[diff1[p1]] - W[diff1[p1] - 1] <= E[index]) {
            int idx = diff1[p1++];
            dsu.merge(idx);

            for (int p : {idx, idx - 1})
                if (p && p < n - 1 && W[p + 1] - W[p - 1] <= E[index] && dsu.get_par(p + 1) == dsu.get_par(p - 1))
                    dsu.consider(p, A[p] - B[p]);
        }
        while (p2 < n - 2 && W[diff2[p2] + 1] - W[diff2[p2] - 1] <= E[index]) {
            int idx = diff2[p2++];
            if (dsu.get_par(idx + 1) == dsu.get_par(idx - 1))
                dsu.consider(idx, A[idx] - B[idx]);
        }
        costs[index] = total + dsu.cost;
    }

    return costs;
}

Compilation message

nile.cpp: In constructor 'DSU::DSU(std::vector<int>&, std::vector<int>&)':
nile.cpp:18:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         for (int i = 0; i < A.size(); i++) {
      |                         ~~^~~~~~~~~~
nile.cpp: In member function 'void DSU::consider(int, int)':
nile.cpp:44:15: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   44 |         if (v - u & 1) {
      |             ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 6072 KB Output is correct
2 Correct 29 ms 6088 KB Output is correct
3 Correct 29 ms 6100 KB Output is correct
4 Correct 29 ms 6076 KB Output is correct
5 Correct 29 ms 6096 KB Output is correct
6 Correct 29 ms 6088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 6096 KB Output is correct
2 Correct 42 ms 6264 KB Output is correct
3 Correct 43 ms 6100 KB Output is correct
4 Correct 43 ms 6088 KB Output is correct
5 Correct 39 ms 6068 KB Output is correct
6 Correct 43 ms 6092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 29 ms 6072 KB Output is correct
8 Correct 29 ms 6088 KB Output is correct
9 Correct 29 ms 6100 KB Output is correct
10 Correct 29 ms 6076 KB Output is correct
11 Correct 29 ms 6096 KB Output is correct
12 Correct 29 ms 6088 KB Output is correct
13 Correct 29 ms 6096 KB Output is correct
14 Correct 42 ms 6264 KB Output is correct
15 Correct 43 ms 6100 KB Output is correct
16 Correct 43 ms 6088 KB Output is correct
17 Correct 39 ms 6068 KB Output is correct
18 Correct 43 ms 6092 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 35 ms 6100 KB Output is correct
26 Correct 38 ms 6392 KB Output is correct
27 Correct 58 ms 6092 KB Output is correct
28 Correct 55 ms 6288 KB Output is correct
29 Correct 41 ms 6092 KB Output is correct
30 Correct 49 ms 6136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 6096 KB Output is correct
2 Correct 42 ms 6264 KB Output is correct
3 Correct 43 ms 6100 KB Output is correct
4 Correct 43 ms 6088 KB Output is correct
5 Correct 39 ms 6068 KB Output is correct
6 Correct 43 ms 6092 KB Output is correct
7 Correct 56 ms 8136 KB Output is correct
8 Correct 52 ms 8072 KB Output is correct
9 Correct 62 ms 8148 KB Output is correct
10 Correct 63 ms 8124 KB Output is correct
11 Correct 65 ms 8068 KB Output is correct
12 Correct 69 ms 8208 KB Output is correct
13 Correct 63 ms 8136 KB Output is correct
14 Correct 51 ms 8140 KB Output is correct
15 Correct 64 ms 8136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 29 ms 6072 KB Output is correct
9 Correct 29 ms 6088 KB Output is correct
10 Correct 29 ms 6100 KB Output is correct
11 Correct 29 ms 6076 KB Output is correct
12 Correct 29 ms 6096 KB Output is correct
13 Correct 29 ms 6088 KB Output is correct
14 Correct 29 ms 6096 KB Output is correct
15 Correct 42 ms 6264 KB Output is correct
16 Correct 43 ms 6100 KB Output is correct
17 Correct 43 ms 6088 KB Output is correct
18 Correct 39 ms 6068 KB Output is correct
19 Correct 43 ms 6092 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 35 ms 6100 KB Output is correct
27 Correct 38 ms 6392 KB Output is correct
28 Correct 58 ms 6092 KB Output is correct
29 Correct 55 ms 6288 KB Output is correct
30 Correct 41 ms 6092 KB Output is correct
31 Correct 49 ms 6136 KB Output is correct
32 Correct 56 ms 8136 KB Output is correct
33 Correct 52 ms 8072 KB Output is correct
34 Correct 62 ms 8148 KB Output is correct
35 Correct 63 ms 8124 KB Output is correct
36 Correct 65 ms 8068 KB Output is correct
37 Correct 69 ms 8208 KB Output is correct
38 Correct 63 ms 8136 KB Output is correct
39 Correct 51 ms 8140 KB Output is correct
40 Correct 64 ms 8136 KB Output is correct
41 Correct 59 ms 8224 KB Output is correct
42 Correct 61 ms 8136 KB Output is correct
43 Correct 71 ms 8140 KB Output is correct
44 Correct 71 ms 8004 KB Output is correct
45 Correct 71 ms 8140 KB Output is correct
46 Correct 71 ms 8228 KB Output is correct
47 Correct 73 ms 8140 KB Output is correct
48 Correct 54 ms 8136 KB Output is correct
49 Correct 66 ms 8136 KB Output is correct