# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112719 | InvMOD | Harbingers (CEOI09_harbingers) | C++14 | 150 ms | 65536 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 1e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
struct LichaoOptMem{
struct Line{
ll a,b;
Line(ll a = 0, ll b = LLONG_MAX): a(a), b(b) {}
ll f(ll x){return a*x + b;}
ll slope(){return a;}
};
vector<int> compress; int global_version = 0;
vector<vector<pair<Line, int>>> st; int trsz;
LichaoOptMem(int n = 0, vector<int> comp = vector<int>()): trsz(n){
compress = comp;
st.assign(n+1, vector<pair<Line,int>>(1, make_pair(Line(), 0)));
return;
}
void update(int l, int r, Line curLine){
if(l > r) return;
if(l == r){
Line version = st[l].back().first;
version = (version.f(compress[l]) < curLine.f(compress[l]) ? version : curLine);
return st[l].push_back(make_pair(version, global_version)), void();
}
else{
int mid = l+r>>1;
Line version = st[mid].back().first;
if(version.f(compress[mid]) > curLine.f(compress[mid])) swap(curLine, version);
if(version.slope() < curLine.slope()) update(l, mid-1, curLine);
if(version.slope() > curLine.slope()) update(mid+1, r, curLine);
st[mid].push_back(make_pair(version, global_version));
}
return;
}
bool roll_back(vector<pair<Line,int>>& tr){
if(tr.back().second == global_version){
tr.pop_back();
return true;
}
return false;
}
void update_rmv(int l, int r){
if(l > r) return;
if(l == r){
return roll_back(st[l]), void();
}
else{
int mid = l+r>>1;
if(roll_back(st[mid])){
update_rmv(l, mid-1);
update_rmv(mid+1, r);
}
}
return;
}
ll query(int l, int r, ll x){
if(l > r) return LLONG_MAX;
int mid = l+r>>1;
ll cur = st[mid].back().first.f(x);
if(x == compress[mid]) return cur;
if(x < compress[mid]) return min(cur, query(l, mid-1, x));
if(x > compress[mid]) return min(cur, query(mid+1, r, x));
return -1;
}
ll query(ll x){
return query(0, trsz, x);
}
void addLine(ll a, ll b){return ++global_version, update(0, trsz, Line(a, b)), void();}
void rmvLine(){return update_rmv(0, trsz), --global_version, void();}
};
int n, wait_time[N], speed[N];
ll dp[N];
vector<pair<int,int>> adj[N];
void dfs(int x, int p, ll cur_dist, LichaoOptMem& lichao){
if(x > 1) dp[x] = lichao.query(speed[x]) + (1ll * cur_dist * speed[x]) + (1ll * wait_time[x]);
lichao.addLine(-cur_dist, dp[x]);
for(pair<int,int> e : adj[x]){
int v = e.first, w = e.second;
if(v != p){
dfs(v, x, cur_dist + 1ll*w, lichao);
}
}
lichao.rmvLine();
return;
}
void solve()
{
int n; cin >> n;
FOR(i, 2, n){
int u,v,w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
vector<int> compress;
FOR(i, 2, n){
cin >> wait_time[i] >> speed[i];
compress.push_back(speed[i]);
}
LichaoOptMem lichao(sz(compress), compress);
dfs(1, -1, 0, lichao);
FOR(i, 2, n) cout << dp[i] <<" \n"[i == n];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |