Submission #706056

# Submission time Handle Problem Language Result Execution time Memory
706056 2023-03-05T19:22:47 Z mychecksedad Harbingers (CEOI09_harbingers) C++17
90 / 100
100 ms 20200 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e5+100, M = 1e5+10, K = 20;

struct Edge{
  int to, w;
};  

struct Line{
  ll m, c;
  ll eval(ll x){
    return m*x + c;
  }
};  


int n, dist[N], chtsz = 0;
vector<Edge> g[N];
ll s[N], c[N], dp[N];
Line cht[N];

float intersection(const Line &a, const Line &b){
  return (float) (a.c - b.c) / (b.m - a.m);
}



void pre(int v, int p){
  for(auto U: g[v]){
    int u = U.to;
    ll w = U.w;
    if(u != p){
      dist[u] = dist[v] + w;
      pre(u, v);
    }
  }
}

ll search(ll val){
  int l = 0, r = chtsz - 2, ans = 0;
  while(l <= r){
    int m = l+r>>1;
    if(intersection(cht[m], cht[m + 1]) < val){
      l = m + 1;
      ans = m + 1;
    }else{
      ans = m;
      r = m - 1;
    }
  }
  return ans;
}

ll rem(Line L){
  if(chtsz < 2 || intersection(L, cht[chtsz - 1]) >= intersection(cht[chtsz - 1], cht[chtsz - 2]))
    return chtsz;

  int l = 1, r = chtsz - 1, ans = 1;

  while(l <= r){
    int m = l+r>>1;
    if(intersection(L, cht[m]) >= intersection(cht[m], cht[m - 1])){
      ans = m + 1;
      l = m + 1;
    }else{
      ans = m;
      r = m - 1;
    }
  }

  return ans;
}

void dfs(int v, int p){
  for(auto U: g[v]){
    int u = U.to;
    if(u != p){
      int prev_sz, prev_pos;
      Line prev_line;


      int j = search(c[u]);
      dp[u] = cht[j].c + cht[j].m * c[u] + s[u] + c[u] * dist[u];

      Line L{-dist[u], dp[u]};

      prev_sz = chtsz;
      prev_pos = chtsz = rem(L);
      prev_line = cht[chtsz];

      cht[chtsz++] = L;

      dfs(u, v);

      chtsz = prev_sz;
      cht[prev_pos] = prev_line;
    }
  }
}

void solve(){
  cin >> n;
  for(int i = 0; i < n - 1; ++i){
    int u, v, w; cin >> u >> v >> w;
    g[u].pb({v, w});
    g[v].pb({u, w});
  }
  for(int i = 2; i <= n; ++i) cin >> s[i] >> c[i];

  dist[1] = 0;
  pre(1, 0);

  dp[1] = 0;
  cht[0] = {0, 0};
  chtsz = 1;
  dfs(1, 0);  
  for(int i = 2; i <= n; ++i) cout << dp[i] << ' ';
}


int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int T = 1, aa;
  // cin >> T;aa=T;
  while(T--){
    // cout << "Case #" << aa-T << ": ";
    solve();
    cout << '\n';
  }
  return 0;
 
}

Compilation message

harbingers.cpp: In function 'll search(ll)':
harbingers.cpp:49:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |     int m = l+r>>1;
      |             ~^~
harbingers.cpp: In function 'll rem(Line)':
harbingers.cpp:68:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   68 |     int m = l+r>>1;
      |             ~^~
harbingers.cpp: In function 'int main()':
harbingers.cpp:130:14: warning: unused variable 'aa' [-Wunused-variable]
  130 |   int T = 1, aa;
      |              ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 3 ms 3028 KB Output is correct
3 Incorrect 35 ms 10044 KB Output isn't correct
4 Correct 68 ms 13760 KB Output is correct
5 Correct 76 ms 16928 KB Output is correct
6 Correct 100 ms 20200 KB Output is correct
7 Correct 47 ms 10388 KB Output is correct
8 Correct 95 ms 14992 KB Output is correct
9 Correct 99 ms 17368 KB Output is correct
10 Correct 78 ms 16164 KB Output is correct