Submission #1102411

# Submission time Handle Problem Language Result Execution time Memory
1102411 2024-10-18T04:46:31 Z ro9669 Nile (IOI24_nile) C++17
100 / 100
96 ms 20852 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
//#include "nile.h"
using namespace std;

typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;

const int maxN = int(2e5)+7;
const int inf = int(1e9)+7;

int n , q;
ll dp[maxN];
vector<int> w , a , b , e , id;
vector<ll> res;

namespace sub1{
    bool check(){
        return (q <= 5 && n <= 2000);
    }

    void solve(){
        for (int d : e){
            dp[0] = 0;
            for (int i = 0 ; i < n ; i++){
                ll s = 0;
                int x = INT_MAX;
                dp[i + 1] = ll(1e18);
                for (int j = i ; j >= 0 ; j--){
                    s += b[id[j]];
                    x = min(x , a[id[j]] - b[id[j]]);
                    if (w[id[i]] - w[id[j]] <= d){
                        if ((i - j)&1){
                            dp[i + 1] = min(dp[i + 1] , dp[j] + s);
                        }
                        else{
                            dp[i + 1] = min(dp[i + 1] , dp[j] + s + 1ll * x);
                        }
                    }
                    else{
                        break;
                    }
                }
            }
            res.push_back(dp[n]);
        }
    }
}

namespace sub2{
    bool check(){
        return (q <= 5);
    }

    void solve(){
        for (int d : e){
            dp[0] = 0;
            for (int i = 0 ; i < n ; i++){
                dp[i + 1] = dp[i] + 1ll * a[id[i]];
                if (i - 1 >= 0 && w[id[i]] - w[id[i - 1]] <= d){
                    dp[i + 1] = min(dp[i + 1] , dp[i - 1] + 1ll * b[id[i]] + 1ll * b[id[i - 1]]);
                }
                if (i - 2 >= 0 && w[id[i]] - w[id[i - 2]] <= d){
                    dp[i + 1] = min(dp[i + 1] , dp[i - 2] + 1ll * b[id[i]] + 1ll * a[id[i - 1]] + 1ll * b[id[i - 2]]);
                }
            }
            res.push_back(dp[n]);
        }
    }
}

namespace sub3{
    int fa[maxN] , p[maxN];
    ll C[maxN] , S[maxN] , X[maxN][2] , Y[maxN] , cur_ans = 0;
    vector<pair<int , ii>> event;
    vector<ii> query;

    int root(int x){
        if (fa[x] < 0) return x; else return fa[x] = root(fa[x]);
    }

    void modify(int u){
        if ((-fa[u]) % 2 == 0){
            C[u] = S[u];
        }
        else{
            C[u] = S[u] + min(X[u][0] , Y[u]);
        }
    }

    void unite(int u , int v , ll w){
        u = root(u);
        v = root(v);
        if (u == v){
            Y[u] = min(Y[u] , w);
            cur_ans -= C[u];
            modify(u);
            cur_ans += C[u];
            return;
        }
        if (-fa[u] < -fa[v]) swap(u , v);
        ll tmp[2];
        if (p[u] < p[v]){
            if ((-fa[u]) % 2 == 0){
                tmp[0] = min(X[u][0] , X[v][0]);
                tmp[1] = min(X[u][1] , X[v][1]);
            }
            else{
                tmp[0] = min(X[u][0] , X[v][1]);
                tmp[1] = min(X[u][1] , X[v][0]);
            }
        }
        else{
            swap(u , v);
            if ((-fa[u]) % 2 == 0){
                tmp[0] = min(X[u][0] , X[v][0]);
                tmp[1] = min(X[u][1] , X[v][1]);
            }
            else{
                tmp[0] = min(X[u][0] , X[v][1]);
                tmp[1] = min(X[u][1] , X[v][0]);
            }
            swap(u , v);
        }
        X[u][0] = tmp[0];
        X[u][1] = tmp[1];
        p[u] = min(p[u] , p[v]);
        C[u] += C[v];
        S[u] += S[v];
        Y[u] = min({Y[u] , Y[v] , w});
        fa[u] += fa[v];
        fa[v] = u;
        cur_ans -= C[u];
        modify(u);
        cur_ans += C[u];
    }

    void solve(){
        for (int i = 0 ; i < n ; i++){
            cur_ans += 1ll * a[id[i]];
            fa[i] = -1;
            C[i] = a[id[i]];
            S[i] = b[id[i]];
            X[i][0] = a[id[i]] - b[id[i]];
            X[i][1] = Y[i] = inf;
            p[i] = i;
        }
        for (int i = 0 ; i < n - 1 ; i++){
            event.push_back({w[id[i + 1]] - w[id[i]] , {i , i + 1}});
        }
        for (int i = 0 ; i < n - 2 ; i++){
            event.push_back({w[id[i + 2]] - w[id[i]] , {i , i + 2}});
        }
        res.resize(q);
        for (int i = 0 ; i < q ; i++){
            query.push_back({e[i] , i});
        }
        sort(all(query));
        sort(all(event));
        for (int i = 0 , j = -1 ; i < q ; i++){
            while (j + 1 < sz(event) && event[j + 1].fi <= query[i].fi){
                int u = event[j + 1].se.fi;
                int v = event[j + 1].se.se;
                if (u + 1 == v){
                    unite(u , v , int(1e9));
                }
                else{
                    unite(u , v , a[id[u + 1]] - b[id[u + 1]]);
                    //cout << a[id[u + 1]] - b[id[u + 1]] << "\n";
                }
                j++;
            }
            res[query[i].se] = cur_ans;
        }
        //cout << Y[root(0)] << "\n";
    }
}

vector<long long> calculate_costs(vector<int> _w , vector<int> _a , vector<int> _b , vector<int> _e){
    w = _w; a = _a; b = _b; e = _e;
    n = sz(w) , q = sz(e);
    id.resize(n);
    for (int i = 0 ; i < n ; i++) id[i] = i;
    sort(all(id) , [](int x , int y){
         return w[x] < w[y];
    });
//    if (sub1::check()){
//        sub1::solve();
//        return res;
//    }
//    if (sub2::check()){
//        sub2::solve();
//        return res;
//    }
    sub3::solve();
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 18368 KB Output is correct
2 Correct 41 ms 18376 KB Output is correct
3 Correct 37 ms 18384 KB Output is correct
4 Correct 37 ms 18384 KB Output is correct
5 Correct 39 ms 18368 KB Output is correct
6 Correct 37 ms 18376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 18368 KB Output is correct
2 Correct 43 ms 18376 KB Output is correct
3 Correct 54 ms 18380 KB Output is correct
4 Correct 52 ms 18468 KB Output is correct
5 Correct 51 ms 18340 KB Output is correct
6 Correct 62 ms 18376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 3 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 3 ms 8784 KB Output is correct
11 Correct 3 ms 8784 KB Output is correct
12 Correct 3 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 38 ms 18368 KB Output is correct
8 Correct 41 ms 18376 KB Output is correct
9 Correct 37 ms 18384 KB Output is correct
10 Correct 37 ms 18384 KB Output is correct
11 Correct 39 ms 18368 KB Output is correct
12 Correct 37 ms 18376 KB Output is correct
13 Correct 42 ms 18368 KB Output is correct
14 Correct 43 ms 18376 KB Output is correct
15 Correct 54 ms 18380 KB Output is correct
16 Correct 52 ms 18468 KB Output is correct
17 Correct 51 ms 18340 KB Output is correct
18 Correct 62 ms 18376 KB Output is correct
19 Correct 3 ms 8784 KB Output is correct
20 Correct 3 ms 8784 KB Output is correct
21 Correct 3 ms 8784 KB Output is correct
22 Correct 3 ms 8784 KB Output is correct
23 Correct 3 ms 8784 KB Output is correct
24 Correct 3 ms 8952 KB Output is correct
25 Correct 51 ms 18368 KB Output is correct
26 Correct 52 ms 18376 KB Output is correct
27 Correct 58 ms 18376 KB Output is correct
28 Correct 67 ms 18380 KB Output is correct
29 Correct 62 ms 18368 KB Output is correct
30 Correct 65 ms 18404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 18368 KB Output is correct
2 Correct 43 ms 18376 KB Output is correct
3 Correct 54 ms 18380 KB Output is correct
4 Correct 52 ms 18468 KB Output is correct
5 Correct 51 ms 18340 KB Output is correct
6 Correct 62 ms 18376 KB Output is correct
7 Correct 68 ms 20668 KB Output is correct
8 Correct 70 ms 20668 KB Output is correct
9 Correct 76 ms 20852 KB Output is correct
10 Correct 92 ms 20688 KB Output is correct
11 Correct 74 ms 20676 KB Output is correct
12 Correct 73 ms 20676 KB Output is correct
13 Correct 75 ms 20688 KB Output is correct
14 Correct 69 ms 20668 KB Output is correct
15 Correct 83 ms 20676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8528 KB Output is correct
2 Correct 3 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 3 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 38 ms 18368 KB Output is correct
9 Correct 41 ms 18376 KB Output is correct
10 Correct 37 ms 18384 KB Output is correct
11 Correct 37 ms 18384 KB Output is correct
12 Correct 39 ms 18368 KB Output is correct
13 Correct 37 ms 18376 KB Output is correct
14 Correct 42 ms 18368 KB Output is correct
15 Correct 43 ms 18376 KB Output is correct
16 Correct 54 ms 18380 KB Output is correct
17 Correct 52 ms 18468 KB Output is correct
18 Correct 51 ms 18340 KB Output is correct
19 Correct 62 ms 18376 KB Output is correct
20 Correct 3 ms 8784 KB Output is correct
21 Correct 3 ms 8784 KB Output is correct
22 Correct 3 ms 8784 KB Output is correct
23 Correct 3 ms 8784 KB Output is correct
24 Correct 3 ms 8784 KB Output is correct
25 Correct 3 ms 8952 KB Output is correct
26 Correct 51 ms 18368 KB Output is correct
27 Correct 52 ms 18376 KB Output is correct
28 Correct 58 ms 18376 KB Output is correct
29 Correct 67 ms 18380 KB Output is correct
30 Correct 62 ms 18368 KB Output is correct
31 Correct 65 ms 18404 KB Output is correct
32 Correct 68 ms 20668 KB Output is correct
33 Correct 70 ms 20668 KB Output is correct
34 Correct 76 ms 20852 KB Output is correct
35 Correct 92 ms 20688 KB Output is correct
36 Correct 74 ms 20676 KB Output is correct
37 Correct 73 ms 20676 KB Output is correct
38 Correct 75 ms 20688 KB Output is correct
39 Correct 69 ms 20668 KB Output is correct
40 Correct 83 ms 20676 KB Output is correct
41 Correct 77 ms 20684 KB Output is correct
42 Correct 76 ms 20668 KB Output is correct
43 Correct 82 ms 20676 KB Output is correct
44 Correct 77 ms 20668 KB Output is correct
45 Correct 83 ms 20668 KB Output is correct
46 Correct 77 ms 20668 KB Output is correct
47 Correct 78 ms 20672 KB Output is correct
48 Correct 77 ms 20684 KB Output is correct
49 Correct 96 ms 20668 KB Output is correct