Submission #930896

# Submission time Handle Problem Language Result Execution time Memory
930896 2024-02-20T15:44:46 Z parlimoos Factories (JOI14_factories) C++14
100 / 100
6719 ms 156888 KB
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include "factories.h"
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'

const ll INF = (1ll * 1e18);

int n;
vector<arr(2)> tr[500000];
int p[500000] , id[500000] , r[500000] , cmp[500000];
ll h[500000] , seg[2][2000001];
int seginf[2000001][2] , is[500000];
int bck[30000000][2] , bckcnt = 0;

void buildSeg(int l = 0 , int r = n - 1 , int i = 1){
    seginf[i][0] = l , seginf[i][1] = r , seg[0][i] = INF , seg[1][i] = INF;
    if(r > l){
        int c = (r + l - 1) >> 1;
        buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
    }else is[l] = i;
}
void updF(int l , int r , int i , ll val){
    if(seginf[i][0] >= l and seginf[i][1] <= r) seg[0][i] = min(seg[0][i] , val) , bck[bckcnt][0] = 0 , bck[bckcnt++][1] = i;
    else if(seginf[i][1] >= l and seginf[i][0] <= r) updF(l , r , i << 1 , val) , updF(l , r , (i << 1) | 1 , val);
}
ll getF(int i){
    ll res = seg[0][i];
    while(i > 1) i /= 2 , res = min(res , seg[0][i]);
    return res;
}
void updPt(int i , ll val){
    while(i >= 1) seg[1][i] = min(seg[1][i] , val) , bck[bckcnt][0] = 1 , bck[bckcnt++][1] = i , i /= 2;
}
ll getPt(int l , int r , int i){
    if(seginf[i][0] >= l and seginf[i][1] <= r) return seg[1][i];
    if(seginf[i][1] >= l and seginf[i][0] <= r) return min(getPt(l , r , i << 1) , getPt(l , r , (i << 1) | 1));
    return INF;
}
void ersSeg(){
    for(int i = 0 ; i < bckcnt ; i++) seg[bck[i][0]][bck[i][1]] = INF;
    bckcnt = 0;
}
void dfs1(int v = 0){
    if(v == 0) p[0] = -1;
    cmp[v] = 1;
    for(auto d : tr[v]){
        int u = d[0] , c = d[1];
        if(u == p[v]) continue;
        p[u] = v , h[u] = h[v] + (1ll * c);
        dfs1(u);
        cmp[v] += cmp[u];
    }
}
int timer = 0;
void dfs2(int v = 0){
    id[v] = timer++;
    int u = -1;
    for(auto el : tr[v]){
        if(el[0] == p[v]) continue;
        if(u == -1 or cmp[u] < cmp[el[0]]) u = el[0];
    }
    if(u == -1) return;
    r[u] = r[v] , dfs2(u);
    for(auto el : tr[v]){
        if(el[0] == p[v] or el[0] == u) continue;
        r[el[0]] = el[0] , dfs2(el[0]);
    }
}
void Init(int N , int *A , int *B , int *D){
    n = N;
    for(int i = 0 ; i < n - 1 ; i++) tr[A[i]].pb({B[i] , D[i]}) , tr[B[i]].pb({A[i] , D[i]});
    dfs1() , dfs2() , buildSeg();
}
long long Query(int S , int *X , int T , int *Y){
    for(int i = 0 ; i < T ; i++){
        int v = Y[i] , cur = v;
        while(cur != -1){
            updF(id[r[cur]] , id[cur] , 1 , h[v]);
            updPt(is[id[cur]] , h[v] - h[cur] - h[cur]);
            cur = p[r[cur]];
        }
    }
    ll res = INF;
    for(int i = 0 ; i < S ; i++){
        int v = X[i] , cur = v;
        while(cur != -1){
            res = min(res , h[v] + getF(is[id[cur]]) - h[cur] - h[cur]);
            res = min(res , h[v] + getPt(id[r[cur]] , id[cur] , 1));
            cur = p[r[cur]];
        }
    }
    ersSeg();
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 27 ms 43612 KB Output is correct
2 Correct 992 ms 59628 KB Output is correct
3 Correct 850 ms 57436 KB Output is correct
4 Correct 888 ms 59724 KB Output is correct
5 Correct 430 ms 59856 KB Output is correct
6 Correct 558 ms 59728 KB Output is correct
7 Correct 840 ms 57684 KB Output is correct
8 Correct 789 ms 59584 KB Output is correct
9 Correct 433 ms 59736 KB Output is correct
10 Correct 558 ms 59988 KB Output is correct
11 Correct 825 ms 57940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 43608 KB Output is correct
2 Correct 3806 ms 113132 KB Output is correct
3 Correct 2586 ms 116480 KB Output is correct
4 Correct 1631 ms 113728 KB Output is correct
5 Correct 1762 ms 144320 KB Output is correct
6 Correct 2915 ms 116920 KB Output is correct
7 Correct 1395 ms 70740 KB Output is correct
8 Correct 898 ms 70688 KB Output is correct
9 Correct 821 ms 74324 KB Output is correct
10 Correct 1491 ms 71732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 43612 KB Output is correct
2 Correct 992 ms 59628 KB Output is correct
3 Correct 850 ms 57436 KB Output is correct
4 Correct 888 ms 59724 KB Output is correct
5 Correct 430 ms 59856 KB Output is correct
6 Correct 558 ms 59728 KB Output is correct
7 Correct 840 ms 57684 KB Output is correct
8 Correct 789 ms 59584 KB Output is correct
9 Correct 433 ms 59736 KB Output is correct
10 Correct 558 ms 59988 KB Output is correct
11 Correct 825 ms 57940 KB Output is correct
12 Correct 9 ms 43608 KB Output is correct
13 Correct 3806 ms 113132 KB Output is correct
14 Correct 2586 ms 116480 KB Output is correct
15 Correct 1631 ms 113728 KB Output is correct
16 Correct 1762 ms 144320 KB Output is correct
17 Correct 2915 ms 116920 KB Output is correct
18 Correct 1395 ms 70740 KB Output is correct
19 Correct 898 ms 70688 KB Output is correct
20 Correct 821 ms 74324 KB Output is correct
21 Correct 1491 ms 71732 KB Output is correct
22 Correct 6719 ms 156888 KB Output is correct
23 Correct 6085 ms 120192 KB Output is correct
24 Correct 4777 ms 144336 KB Output is correct
25 Correct 5113 ms 146584 KB Output is correct
26 Correct 5110 ms 122196 KB Output is correct
27 Correct 2780 ms 156076 KB Output is correct
28 Correct 2730 ms 137348 KB Output is correct
29 Correct 4913 ms 121672 KB Output is correct
30 Correct 5117 ms 119080 KB Output is correct
31 Correct 4895 ms 119564 KB Output is correct
32 Correct 870 ms 89936 KB Output is correct
33 Correct 1024 ms 87752 KB Output is correct
34 Correct 2041 ms 71664 KB Output is correct
35 Correct 2023 ms 71672 KB Output is correct
36 Correct 1408 ms 72532 KB Output is correct
37 Correct 1403 ms 72296 KB Output is correct