Submission #1111239

# Submission time Handle Problem Language Result Execution time Memory
1111239 2024-11-11T19:39:34 Z popabogdannnn Nile (IOI24_nile) C++17
100 / 100
94 ms 15568 KB
#include <bits/stdc++.h>

using namespace std;

struct Artifact {
    int W, C;
};

struct Edge {
    int a, b, c;
};

struct DSU {
    struct Info {
        int cost;
        int le, ri;
        int min_alone[2];
        int min_over[2];
        Info() = default;
        Info(int pos, int val) {
            le = ri = pos;
            min_alone[0] = min_alone[1] = INT_MAX;
            min_over[0] = min_over[1] = INT_MAX;
            min_alone[pos % 2] = val;
            cost = val;
        }
    };

    vector<int>T;
    vector<Info> info;

    long long init(vector<Artifact> artifacts) {
        int N = artifacts.size();
        long long ret = 0;
        T.resize(N);
        info.resize(N);
        iota(T.begin(), T.end(), 0);
        for(int i = 0; i < N; i++) {
            info[i] = Info(i, artifacts[i].C);
            ret += artifacts[i].C;
        }
        return ret;
    };

    int find(int a) {
        if(a == T[a]) {
            return a;
        }
        return T[a] = find(T[a]);
    }

    int unite(int a, int b, int c) {
        if(b == a + 2) {
            int p = (a + 1) % 2;
            a = find(a);
            b = find(b);
            assert(a == b);
            info[a].min_over[p] = min(info[a].min_over[p], c);
            int ret = -info[a].cost;
            if((info[a].ri - info[a].le + 1) % 2 == 1) {
                info[a].cost = min(info[a].cost, info[a].min_over[1 - info[a].le % 2]);
            }
            return ret + info[a].cost;
        }
        a = find(a);
        b = find(b);

        int ret = -info[a].cost - info[b].cost;
        info[b].le = min(info[b].le, info[a].le);
        info[b].ri = max(info[b].ri, info[a].ri);
        info[b].min_alone[0] = min(info[b].min_alone[0], info[a].min_alone[0]);
        info[b].min_alone[1] = min(info[b].min_alone[1], info[a].min_alone[1]);
        info[b].min_over[0] = min(info[b].min_over[0], info[a].min_over[0]);
        info[b].min_over[1] = min(info[b].min_over[1], info[a].min_over[1]);
        T[a] = b;
        
        if((info[b].ri - info[b].le + 1) % 2 == 1) {
            info[b].cost = min(info[b].min_alone[info[b].le % 2], info[b].min_over[1 - info[b].le % 2]);
        }
        else {
            info[b].cost = 0;
        }
        return ret + info[b].cost;
    }

};

std::vector<long long> calculate_costs(std::vector<int> W, std::vector<int> A, std::vector<int> B, std::vector<int> E) {
    int N = W.size(), Q = E.size();    
    vector<Artifact> artifacts(N);
    vector<pair <int, int>> queries(Q);
    vector<long long> ret(Q);
    vector<Edge> edges;

    long long ans = 0;
    for(int i = 0; i < N; i++) {
        artifacts[i] = {W[i], A[i] - B[i]};
        ans += B[i];
    }
    for(int i = 0; i < Q; i++) {
        queries[i] = {E[i], i};
    }
    sort(queries.begin(), queries.end());

    sort(artifacts.begin(), artifacts.end(), [](Artifact a, Artifact b) {
        return a.W < b.W;
    });

    for(int i = 0; i + 1 < N; i++) {
        edges.push_back({i, i + 1, artifacts[i + 1].W - artifacts[i].W});
        if(i + 2 < N) {
            edges.push_back({i, i + 2, artifacts[i + 2].W - artifacts[i].W});
        }
    }
    
    sort(edges.begin(), edges.end(), [](Edge a, Edge b) {
        return a.c < b.c || (a.c == b.c && (a.b - a.a) < (b.b - b.a));
    });
    DSU dsu;
    ans += dsu.init(artifacts);
    
    int j = 0;
    for(int i = 0; i < Q; i++) {
        while(j < edges.size() && edges[j].c <= queries[i].first) {
            ans += dsu.unite(edges[j].a, edges[j].b, artifacts[edges[j].a + 1].C);
            j++;
        }
        ret[queries[i].second] = ans;
    }
    return ret;
}

Compilation message

nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:124:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |         while(j < edges.size() && edges[j].c <= queries[i].first) {
      |               ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 11916 KB Output is correct
2 Correct 40 ms 9636 KB Output is correct
3 Correct 37 ms 9660 KB Output is correct
4 Correct 38 ms 9664 KB Output is correct
5 Correct 44 ms 11976 KB Output is correct
6 Correct 39 ms 9824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 9668 KB Output is correct
2 Correct 44 ms 10668 KB Output is correct
3 Correct 48 ms 9660 KB Output is correct
4 Correct 50 ms 9660 KB Output is correct
5 Correct 44 ms 9660 KB Output is correct
6 Correct 54 ms 11196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 2 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 41 ms 11916 KB Output is correct
8 Correct 40 ms 9636 KB Output is correct
9 Correct 37 ms 9660 KB Output is correct
10 Correct 38 ms 9664 KB Output is correct
11 Correct 44 ms 11976 KB Output is correct
12 Correct 39 ms 9824 KB Output is correct
13 Correct 38 ms 9668 KB Output is correct
14 Correct 44 ms 10668 KB Output is correct
15 Correct 48 ms 9660 KB Output is correct
16 Correct 50 ms 9660 KB Output is correct
17 Correct 44 ms 9660 KB Output is correct
18 Correct 54 ms 11196 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 760 KB Output is correct
25 Correct 51 ms 12488 KB Output is correct
26 Correct 50 ms 12740 KB Output is correct
27 Correct 62 ms 12524 KB Output is correct
28 Correct 57 ms 12712 KB Output is correct
29 Correct 50 ms 12476 KB Output is correct
30 Correct 60 ms 12476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 9668 KB Output is correct
2 Correct 44 ms 10668 KB Output is correct
3 Correct 48 ms 9660 KB Output is correct
4 Correct 50 ms 9660 KB Output is correct
5 Correct 44 ms 9660 KB Output is correct
6 Correct 54 ms 11196 KB Output is correct
7 Correct 66 ms 13128 KB Output is correct
8 Correct 64 ms 14116 KB Output is correct
9 Correct 71 ms 14012 KB Output is correct
10 Correct 69 ms 14116 KB Output is correct
11 Correct 69 ms 13992 KB Output is correct
12 Correct 69 ms 14012 KB Output is correct
13 Correct 68 ms 14016 KB Output is correct
14 Correct 64 ms 13748 KB Output is correct
15 Correct 76 ms 14012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 1 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 41 ms 11916 KB Output is correct
9 Correct 40 ms 9636 KB Output is correct
10 Correct 37 ms 9660 KB Output is correct
11 Correct 38 ms 9664 KB Output is correct
12 Correct 44 ms 11976 KB Output is correct
13 Correct 39 ms 9824 KB Output is correct
14 Correct 38 ms 9668 KB Output is correct
15 Correct 44 ms 10668 KB Output is correct
16 Correct 48 ms 9660 KB Output is correct
17 Correct 50 ms 9660 KB Output is correct
18 Correct 44 ms 9660 KB Output is correct
19 Correct 54 ms 11196 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 2 ms 760 KB Output is correct
26 Correct 51 ms 12488 KB Output is correct
27 Correct 50 ms 12740 KB Output is correct
28 Correct 62 ms 12524 KB Output is correct
29 Correct 57 ms 12712 KB Output is correct
30 Correct 50 ms 12476 KB Output is correct
31 Correct 60 ms 12476 KB Output is correct
32 Correct 66 ms 13128 KB Output is correct
33 Correct 64 ms 14116 KB Output is correct
34 Correct 71 ms 14012 KB Output is correct
35 Correct 69 ms 14116 KB Output is correct
36 Correct 69 ms 13992 KB Output is correct
37 Correct 69 ms 14012 KB Output is correct
38 Correct 68 ms 14016 KB Output is correct
39 Correct 64 ms 13748 KB Output is correct
40 Correct 76 ms 14012 KB Output is correct
41 Correct 70 ms 15400 KB Output is correct
42 Correct 70 ms 15556 KB Output is correct
43 Correct 94 ms 15548 KB Output is correct
44 Correct 80 ms 15548 KB Output is correct
45 Correct 83 ms 15568 KB Output is correct
46 Correct 79 ms 15396 KB Output is correct
47 Correct 83 ms 15548 KB Output is correct
48 Correct 71 ms 15292 KB Output is correct
49 Correct 82 ms 15552 KB Output is correct