Submission #705964

# Submission time Handle Problem Language Result Execution time Memory
705964 2023-03-05T17:28:19 Z mychecksedad Harbingers (CEOI09_harbingers) C++17
0 / 100
1000 ms 44744 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;
  ll w;
};  

struct Line{
  bool type = false;
  double x = 0;
  ll m, c;
  bool operator < (const Line &other) const{
    if(type != other.type) return x < other.x;
    return m < other.m;
  }
};  


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

double intersection(const set<Line>::iterator &a, const set<Line>::iterator &b){
  return (double) (a->c - b->c) / (b->m - a->m);
}
double intersection(const set<Line>::iterator &a, const Line &b){
  return (double) (a->c - b.c) / (b.m - a->m);
}

void calcX(const set<Line>::iterator &a){
  if(cht.size() > 1){
    Line L = *a;
    L.x = intersection(prev(a), a);
    cht.insert(a, L);
  }
}

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);
    }
  }
}

void dfs(int v, int p){
  for(auto U: g[v]){
    int u = U.to;
    if(u != p){
      vector<Line> popp;

      Line j = *prev(cht.upper_bound({1, (double) s[v], 0, 0}));
      dp[u] = j.c + j.m * c[u] + s[u] + c[u] * dist[u];

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

      while(cht.size() > 1 && intersection(prev(prev(cht.end())), prev(cht.end())) <= intersection(prev(cht.end()), L)){
        popp.pb(*prev(cht.end()));
        cht.erase(prev(cht.end()));
      }
      auto it = cht.insert(L).first;
      calcX(it);
      auto val = *it;

      dfs(u, v);

      cht.erase(val);

      for(auto &popped: popp) cht.insert(popped);
    }
  }
}

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.insert({0, 0, 0, 0});
  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 'int main()':
harbingers.cpp:108:14: warning: unused variable 'aa' [-Wunused-variable]
  108 |   int T = 1, aa;
      |              ^~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Incorrect 4 ms 3668 KB Output isn't correct
3 Incorrect 51 ms 19072 KB Output isn't correct
4 Incorrect 79 ms 27164 KB Output isn't correct
5 Runtime error 113 ms 35364 KB Memory limit exceeded
6 Runtime error 150 ms 44744 KB Memory limit exceeded
7 Incorrect 69 ms 17428 KB Output isn't correct
8 Incorrect 128 ms 24856 KB Output isn't correct
9 Execution timed out 1071 ms 29512 KB Time limit exceeded
10 Incorrect 146 ms 27308 KB Output isn't correct