# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112817 | InvMOD | Harbingers (CEOI09_harbingers) | C++14 | 54 ms | 9424 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>
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define sz(v) (int)(v).size()
using namespace std;
using ll = long long;
const int N = 1e5;
struct LichaoOptMem{
struct Line{
int a; ll b;
Line(int a = 0, ll b = LLONG_MAX): a(a), b(b) {}
ll f(ll x){return 1ll*a*x + b;}
ll slope(){return a;}
bool operator != (const Line& x) const{
return (a != x.a || b != x.b);
}
};
vector<bool> rekt;
vector<int> compress; int global_version = -1;
vector<vector<pair<Line, int>>> st; int trsz;
void init(vector<int>& comp){
trsz = sz(comp)-1;
st.resize(trsz+1);
compress = comp;
rekt.resize(trsz);
return;
}
void update(int l, int r, Line curLine){
if(l > r) return;
if(l == r){
if(!sz(st[l]) || st[l].back().first.f(compress[l]) > curLine.f(compress[l]))
return st[l].push_back(make_pair(curLine, global_version)), void();
}
else{
int mid = l+r>>1;
while(sz(st[mid]) && rekt[st[mid].back().second]) st[mid].pop_back();
if(!sz(st[mid])) return st[mid].push_back(make_pair(curLine, global_version)), void();
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);
if(version != st[mid].back().first) st[mid].push_back(make_pair(version, global_version));
}
return;
}
ll query(int l, int r, ll x){
if(l > r) return LLONG_MAX;
int mid = l+r>>1;
while(sz(st[mid]) && rekt[st[mid].back().second]) st[mid].pop_back();
ll cur = (st[mid].size() ? st[mid].back().first.f(x) : LLONG_MAX);
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);
}
int addLine(int a, ll b){return ++global_version, update(0, trsz, Line(a, b)), global_version;}
void rmvLine(int version){return rekt[version] = true, void();}
};
int speed[N];
ll dp[N];
basic_string<pair<int,int>> adj[N];
LichaoOptMem lichao;
void dfs(int x, int p, ll cur_dist){
if(x) dp[x] += lichao.query(speed[x]) + (1ll * cur_dist * speed[x]);
int rem_version = 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.rmvLine(rem_version);
return;
}
void solve()
{
int n; cin >> n;
FOR(i, 1, n-1){
int u,v,w; cin >> u >> v >> w;
u--, v--;
adj[u].push_back(make_pair(v, w));
}
vector<int> compress;
FOR(i, 1, n-1){
cin >> dp[i] >> speed[i];
compress.push_back(speed[i]);
}
sort(compress.begin(), compress.end());
lichao.init(compress);
dfs(0, -1, 0);
FOR(i, 1, n-1) cout << dp[i] <<" \n"[i == n-1];
}
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... |