# BZOJ 3672: [Noi2014]购票( 树链剖分 + 线段树 + 凸包 )

s弄成前缀和(到根), dp(i) = min(dp(j) + (s(i)-s(j))*p(i)+q(i)). 链的情况大家都会做...就是用栈维护个下凸包, 插入时暴力弹栈, 查询时就在凸包上二分/三分. 扩展到树上的话, 就先树链剖分, 然后就变成链上的情况了, 线段树每个结点处理出对应的区间的凸包. 对于x, 用Root到fa[x]这段路径来更新x. 我们知道1段路径会剖成 ≤ log N 段, 然后每段(区间)只会影响log N个线段树结点, 加上每次O(log N)三分/二分, 时间复杂度是O(N log^3 N). 常数很小, 可以过. 空间复杂度是O(N log N)

-------------------------------------------------------------------------

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>

using namespace std;

typedef long long ll;

const int maxn = 200009;
const double eps = 1e-9;

int N, L, R, Top, n, T;
int fa[maxn], p[maxn], seq[maxn * 20];
int top[maxn], sz[maxn], dep[maxn], ch[maxn], Id[maxn], _Id[maxn];
ll q[maxn], d[maxn], len[maxn], ans[maxn];

template<class T>
inline void Min(T &x, T t) {
if(t < x) x = t;
}

template<class T>
char c = getchar(); x = 0;
while(!isdigit(c)) c = getchar();
for(; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x;
}

struct edge {
int t;
edge* n;
} E[maxn], *Pt = E, *H[maxn];

inline void AddEdge(int u, int v) {
Pt->t = v, Pt->n = H[u], H[u] = Pt++;
}

void dfs(int x) {
sz[x] = 1, ch[x] = -1;
if(~fa[x]) d[x] += d[fa[x]];
for(edge* e = H[x]; e; e = e->n) {
dep[e->t] = dep[x] + 1;
dfs(e->t);
sz[x] += sz[e->t];
if(!~ch[x] || sz[e->t] > sz[ch[x]]) ch[x] = e->t;
}
}

void DFS(int x) {
Id[x] = ++n;
top[_Id[n] = x] = Top;
if(~ch[x]) DFS(ch[x]);
for(edge* e = H[x]; e; e = e->n)
if(e->t != ch[x]) DFS(Top = e->t);
}

void Init() {
for(int i = 1; i < N; i++) {
}
fa[0] = -1;
dfs(dep[0] = 0);
DFS(n = Top = 0);
}

bool Cmp(const int &l, const int &r) {
return dep[l] < dep[r];
}

struct Node {
Node *lc, *rc;
int l, r;
} pool[maxn << 1], *pt = pool, *Root;

void Build(Node* t, int l, int r) {
t->l = n, t->r = n - 1;
n += r - l + 1;
if(l != r) {
int m = (l + r) >> 1;
Build(t->lc = pt++, l, m);
Build(t->rc = pt++, m + 1, r);
}
}

inline ll calc(int x) {
return ans[seq[x]] - d[seq[x]] * p[T];
}

void Query(Node* t, int l, int r) {
if(L <= l && r <= R && d[T] - d[_Id[r]] > len[T]) return;
if(L <= l && r <= R && d[T] - d[_Id[l]] <= len[T]) {
l = t->l, r = t->r;
while(l <= r) {
int lth = (r - l) / 3;
int m1 = l + lth, m2 = r - lth;
ll c1 = calc(m1), c2 = calc(m2);
if(c1 < c2)
r = m2 - 1, Min(ans[T], c1);
else
l = m1 + 1, Min(ans[T], c2);
}
} else {
int m = (l + r) >> 1;
if(L <= m) Query(t->lc, l, m);
if(m < R) Query(t->rc, m + 1, r);
}
}

void QUERY(int x, int y) {
for(; top[x] != top[y]; x = fa[top[x]]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
L = Id[top[x]], R = Id[x];
Query(Root, 1, N);
}
if(dep[x] < dep[y]) swap(x, y);
L = Id[y], R = Id[x];
Query(Root, 1, N);
}

inline bool chk(int a, int b, int c) {
if(d[b] == d[c]) return false;
return (double) (ans[b] - ans[a]) / (d[b] - d[a]) - (double) (ans[c] - ans[b]) / (d[c] - d[b]) > eps;
}

void Modify(Node* t, int l, int r) {
while(t->r > t->l && chk(seq[t->r - 1], seq[t->r], T)) t->r--;
while(t->r >= t->l && d[seq[t->r]] == d[T] && ans[seq[t->r]] >= ans[T]) t->r--;
if(t->r < t->l || d[seq[t->r]] != d[T]) seq[++t->r] = T;
if(l != r) {
int m = (l + r) >> 1;
Id[T] <= m ? Modify(t->lc, l, m) : Modify(t->rc, m + 1, r);
}
}

void Work() {
n = 0;
Build(Root = pt++, 1, N);
ans[T = 0] = 0;
Modify(Root, 1, N);
for(T = 1; T < N; T++) {
ans[T] = 1LL << 62;
QUERY(0, fa[T]);
ans[T] += d[T] * p[T] + q[T];
Modify(Root, 1, N);
}
for(int i = 1; i < N; i++)
printf("%lld\n", ans[i]);
}

int main() {
Init();
Work();
return 0;
}

-------------------------------------------------------------------------

## 3672: [Noi2014]购票

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 669  Solved: 315
[Submit][Status][Discuss]

## Description

今年夏天，NOI在SZ市迎来了她30周岁的生日。来自全国 n 个城市的OIer们都会从各地出发，到SZ市参加这次盛会。
全国的城市构成了一棵以SZ市为根的有根树，每个城市与它的父亲用道路连接。为了方便起见，我们将全国的 n 个城市用 1 到 n 的整数编号。其中SZ市的编号为 1。对于除SZ市之外的任意一个城市 v，我们给出了它在这棵树上的父亲城市 fv  以及到父亲城市道路的长度 sv

7 3
1 2 20 0 3
1 5 10 100 5
2 4 10 10 10
2 9 1 100 10
3 5 20 100 10
4 4 20 0 10

40
150
70
149
300
150

n=2×10^5

## Source

posted @ 2016-02-12 14:48  JSZX11556  阅读(459)  评论(0编辑  收藏