답안 #404678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404678 2021-05-14T19:56:12 Z nikatamliani Harbingers (CEOI09_harbingers) C++14
60 / 100
166 ms 30660 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 1e5+10, oo = 4e18;
vector<pair<int, int>> edges[N];
int p[N], s[N], v[N], sum[N];
ll dp[N], n;
struct line {
	int k;
	ll b;
	line(int _k, ll _b) : k(_k), b(_b) {}
	line() : line(0, 4e18) {}
	ll f(int x) {
		return (ll)k*x + b;
	}
};
struct node {
	node *L, *R;
	line l;
	node() : L(NULL), R(NULL), l() {}
} *root;
pair<line*, line> ops[600005]; 
int timer = 0;
void add_line(int l, int r, line ln, node *&t) {
	if(ln.b == oo) return;
	bool is_new = (l == r);
	if(t == NULL) t = new node(), is_new = 1;
	int m = (0LL+l+r)/2;
	bool lft = t->l.f(l) < ln.f(l);
	bool mid = t->l.f(m) < ln.f(m);
	if(lft == mid && !lft) {
		line save = t->l;
		pair<line*, line> p = {&(t->l), save};
		swap(ln, t->l);
		if(timer <= 6e5)ops[++timer] = p;
	} 
	if(lft != mid && !mid) {
		line save = t->l;
		pair<line*, line> p = {&(t->l), save};
		swap(ln, t->l);
		if(timer <= 6e5)ops[++timer] = p;
	}
	if(is_new) {
		return;
	}
	if(lft == mid) {
		add_line(m+1, r, ln, t->R);
	} else {
		add_line(l, m, ln, t->L);
	}
}
ll get_min(ll l, ll r, ll x, node *&t) {
	if(t == NULL || l > x || r < x) return oo;
	if(l == r) return t->l.f(x);
	return min({get_min(l, l+r>>1, x, t->L), get_min((l+r>>1)+1, r, x, t->R), t->l.f(x)});
}
void rollback(int T) {
	while(timer > T) {
		pair<line*, line> p = ops[timer--];
		(*p.first) = p.second;
	}
}
void dfs(int x, int p) {
	int T = timer;
	if(x == 1) {
		dp[x] = 0;
	} else {
		dp[x] = get_min(1, 1e9, v[x], root) + (ll)sum[x] * v[x] + (ll)s[x];
	}
	add_line(1, 1e9, line(-sum[x], dp[x]), root);
	for(pair<int, int> to : edges[x]) {
		if(to.first != p) {
			sum[to.first] = sum[x] + to.second;
			dfs(to.first, x);
		}
	}
	rollback(T);
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for(int i = 0; i < n-1; ++i) {
		int u, v, w; cin >> u >> v >> w;
		edges[u].push_back({v, w});
		edges[v].push_back({u, w});
	}
	for(int i = 2; i <= n; ++i) {
		cin >> s[i] >> v[i];
	}
	dfs(1, 0);
	for(int i = 2; i <= n; ++i) {
		cout << dp[i] << ' ';
	}
}

Compilation message

harbingers.cpp: In function 'long long int get_min(long long int, long long int, long long int, node*&)':
harbingers.cpp:55:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |  return min({get_min(l, l+r>>1, x, t->L), get_min((l+r>>1)+1, r, x, t->R), t->l.f(x)});
      |                         ~^~
harbingers.cpp:55:53: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |  return min({get_min(l, l+r>>1, x, t->L), get_min((l+r>>1)+1, r, x, t->R), t->l.f(x)});
      |                                                    ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 16684 KB Output is correct
2 Correct 12 ms 17100 KB Output is correct
3 Correct 68 ms 23228 KB Output is correct
4 Correct 101 ms 26452 KB Output is correct
5 Correct 121 ms 27812 KB Output is correct
6 Correct 156 ms 30364 KB Output is correct
7 Incorrect 113 ms 23236 KB Output isn't correct
8 Incorrect 166 ms 28496 KB Output isn't correct
9 Incorrect 156 ms 30660 KB Output isn't correct
10 Incorrect 150 ms 29636 KB Output isn't correct