답안 #1099753

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

#include "bits/stdc++.h"
#include "nile.h"
using namespace std;

const int INF = 1e9;

struct DSU{
    vector <int> e;
    vector <int> c;
    vector <int> mni;
    vector<array<int, 3>> mnc;
    long long tot = 0;
    DSU(int n, vector<int> c) : e(n ,-1), c(c), mnc(n), mni(n) {
        for(int i = 0; i < n; i++){
            mni[i] = i;
            mnc[i] = {INF, INF, INF};
            mnc[i][i % 2] = c[i];
            tot += c[i];
        }
    }
    int find(int x) { return e[x]<0? x : e[x]=find(e[x]); }
    int size(int x) { return -e[find(x)]; }
    int cost(int x) {
        int idx = mnc[x][mni[x] % 2] < mnc[x][2]? (mni[x] % 2) : 2;
        return mnc[x][idx] * (size(x) % 2);
    }
    bool join(int i ,int j){
        int x = find(i) ,y = find(j);
        if(x == y){
            tot -= cost(x);
            mnc[x][2] = min(mnc[x][2], c[(i + j) / 2]);
            tot += cost(x);
            return false;
        }
        tot -= cost(x);
        tot -= cost(y);
        if(e[x] > e[y]) swap(x ,y);
        e[x] += e[y]; e[y] = x;
        for(int p = 0; p < 3; p++)
            mnc[x][p] = min(mnc[x][p], mnc[y][p]);
        mni[x] = min(mni[x], mni[y]);
        tot += cost(x);
        return true;
    }
};

vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
    int n = W.size();
    int q = E.size();

    vector<int> ordW(n);
    iota(ordW.begin() ,ordW.end() ,0);
    sort(ordW.begin() ,ordW.end() ,[&](int i ,int j){
        return W[i] < W[j];
    });
    vector <int> ordE(q);
    iota(ordE.begin() ,ordE.end() ,0);
    sort(ordE.begin() ,ordE.end() ,[&](int i ,int j){
        return E[i] < E[j];
    });
    vector<int> w(n), c(n);
    for(int i = 0; i < n; i++){
        int j = ordW[i];
        w[i] = W[j];
        c[i] = A[j] - B[j];
    }

    vector<array<int, 3>> edges;
    for(int i = 0; i < n; i++){
        if(i+1 < n) edges.push_back({w[i+1] - w[i], i, i+1});
        if(i+2 < n) edges.push_back({w[i+2] - w[i], i, i+2});
    }
    sort(edges.rbegin(), edges.rend());

    DSU d(n, c);
    vector<long long> R(q, accumulate(B.begin(), B.end(), 0LL));
    for(int i = 0; i < q; i++){
        while(!edges.empty() && edges.back()[0] <= E[ordE[i]]){
            auto [_, x, y] = edges.back(); edges.pop_back();
            d.join(x, y);
        }
        R[ordE[i]] += d.tot;
    }
    return R;
}

Compilation message

nile.cpp: In constructor 'DSU::DSU(int, std::vector<int>)':
nile.cpp:13:27: warning: 'DSU::mnc' will be initialized after [-Wreorder]
   13 |     vector<array<int, 3>> mnc;
      |                           ^~~
nile.cpp:12:18: warning:   'std::vector<int> DSU::mni' [-Wreorder]
   12 |     vector <int> mni;
      |                  ^~~
nile.cpp:15:5: warning:   when initialized here [-Wreorder]
   15 |     DSU(int n, vector<int> c) : e(n ,-1), c(c), mnc(n), mni(n) {
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 9420 KB Output is correct
2 Correct 46 ms 8900 KB Output is correct
3 Correct 43 ms 9668 KB Output is correct
4 Correct 42 ms 8908 KB Output is correct
5 Correct 42 ms 9012 KB Output is correct
6 Correct 42 ms 8908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 9016 KB Output is correct
2 Correct 42 ms 8900 KB Output is correct
3 Correct 54 ms 9672 KB Output is correct
4 Correct 53 ms 8896 KB Output is correct
5 Correct 55 ms 8900 KB Output is correct
6 Correct 54 ms 9416 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 604 KB Output is correct
8 Correct 1 ms 412 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 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 45 ms 9420 KB Output is correct
8 Correct 46 ms 8900 KB Output is correct
9 Correct 43 ms 9668 KB Output is correct
10 Correct 42 ms 8908 KB Output is correct
11 Correct 42 ms 9012 KB Output is correct
12 Correct 42 ms 8908 KB Output is correct
13 Correct 44 ms 9016 KB Output is correct
14 Correct 42 ms 8900 KB Output is correct
15 Correct 54 ms 9672 KB Output is correct
16 Correct 53 ms 8896 KB Output is correct
17 Correct 55 ms 8900 KB Output is correct
18 Correct 54 ms 9416 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 412 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 46 ms 8836 KB Output is correct
26 Correct 47 ms 8900 KB Output is correct
27 Correct 54 ms 8900 KB Output is correct
28 Correct 55 ms 9184 KB Output is correct
29 Correct 55 ms 8992 KB Output is correct
30 Correct 59 ms 9308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 9016 KB Output is correct
2 Correct 42 ms 8900 KB Output is correct
3 Correct 54 ms 9672 KB Output is correct
4 Correct 53 ms 8896 KB Output is correct
5 Correct 55 ms 8900 KB Output is correct
6 Correct 54 ms 9416 KB Output is correct
7 Correct 61 ms 10828 KB Output is correct
8 Correct 60 ms 11464 KB Output is correct
9 Correct 71 ms 10948 KB Output is correct
10 Correct 70 ms 10948 KB Output is correct
11 Correct 77 ms 10948 KB Output is correct
12 Correct 71 ms 11568 KB Output is correct
13 Correct 72 ms 10948 KB Output is correct
14 Correct 67 ms 10832 KB Output is correct
15 Correct 75 ms 10948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 45 ms 9420 KB Output is correct
9 Correct 46 ms 8900 KB Output is correct
10 Correct 43 ms 9668 KB Output is correct
11 Correct 42 ms 8908 KB Output is correct
12 Correct 42 ms 9012 KB Output is correct
13 Correct 42 ms 8908 KB Output is correct
14 Correct 44 ms 9016 KB Output is correct
15 Correct 42 ms 8900 KB Output is correct
16 Correct 54 ms 9672 KB Output is correct
17 Correct 53 ms 8896 KB Output is correct
18 Correct 55 ms 8900 KB Output is correct
19 Correct 54 ms 9416 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 412 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 46 ms 8836 KB Output is correct
27 Correct 47 ms 8900 KB Output is correct
28 Correct 54 ms 8900 KB Output is correct
29 Correct 55 ms 9184 KB Output is correct
30 Correct 55 ms 8992 KB Output is correct
31 Correct 59 ms 9308 KB Output is correct
32 Correct 61 ms 10828 KB Output is correct
33 Correct 60 ms 11464 KB Output is correct
34 Correct 71 ms 10948 KB Output is correct
35 Correct 70 ms 10948 KB Output is correct
36 Correct 77 ms 10948 KB Output is correct
37 Correct 71 ms 11568 KB Output is correct
38 Correct 72 ms 10948 KB Output is correct
39 Correct 67 ms 10832 KB Output is correct
40 Correct 75 ms 10948 KB Output is correct
41 Correct 71 ms 10948 KB Output is correct
42 Correct 72 ms 10940 KB Output is correct
43 Correct 82 ms 11260 KB Output is correct
44 Correct 83 ms 10948 KB Output is correct
45 Correct 85 ms 10952 KB Output is correct
46 Correct 84 ms 10952 KB Output is correct
47 Correct 85 ms 10948 KB Output is correct
48 Correct 73 ms 10880 KB Output is correct
49 Correct 86 ms 11508 KB Output is correct