zoukankan      html  css  js  c++  java
  • BZOJ3672: [Noi2014]购票

    Description

     今年夏天,NOI在SZ市迎来了她30周岁的生日。来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会。
           全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路连接。为了方便起见,我们将全国的 n 个城市用 1 到 n 的整数编号。其中SZ市的编号为 1。对于除SZ市之外的任意一个城市 v,我们给出了它在这棵树上的父亲城市 fv  以及到父亲城市道路的长度 sv
    从城市 v 前往SZ市的方法为:选择城市 v 的一个祖先 a,支付购票的费用,乘坐交通工具到达 a。再选择城市 a 的一个祖先 b,支付费用并到达 b。以此类推,直至到达SZ市。
    对于任意一个城市 v,我们会给出一个交通工具的距离限制 lv。对于城市 v 的祖先 a,只有当它们之间所有道路的总长度不超过 lv  时,从城市 v 才可以通过一次购票到达城市 a,否则不能通过一次购票到达。对于每个城市 v,我们还会给出两个非负整数 pv,qv  作为票价参数。若城市 v 到城市 a 所有道路的总长度为 d,那么从城市 v 到城市 a 购买的票价为 dpv+qv
    每个城市的OIer都希望自己到达SZ市时,用于购票的总资金最少。你的任务就是,告诉每个城市的OIer他们所花的最少资金是多少。
     

    Input

    第 1 行包含2个非负整数 n,t,分别表示城市的个数和数据类型(其意义将在后面提到)。输入文件的第 2 到 n 行,每行描述一个除SZ之外的城市。其中第 v 行包含 5 个非负整数 f_v,s_v,p_v,q_v,l_v,分别表示城市 v 的父亲城市,它到父亲城市道路的长度,票价的两个参数和距离限制。请注意:输入不包含编号为 1 的SZ市,第 2 行到第 n 行分别描述的是城市 2 到城市 n。

    Output

    输出包含 n-1 行,每行包含一个整数。其中第 v 行表示从城市 v+1 出发,到达SZ市最少的购票费用。同样请注意:输出不包含编号为 1 的SZ市。

    Sample Input

    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

    Sample Output


    40
    150
    70
    149
    300
    150

    HINT

    对于所有测试数据,保证 0≤pv≤106,0≤qv≤1012,1≤fv<v;保证 0<sv≤lv≤2×1011,且任意城市到SZ市的总路程长度不超过 2×1011


    输入的 t 表示数据类型,0≤t<4,其中:


    当 t=0 或 2 时,对输入的所有城市 v,都有 fv=v-1,即所有城市构成一个以SZ市为终点的链;


    当 t=0 或 1 时,对输入的所有城市 v,都有 lv=2×1011,即没有移动的距离限制,每个城市都能到达它的所有祖先;


    当 t=3 时,数据没有特殊性质。


    n=2×10^5

     
    狠下心来怒写了有根树分治。
    思想就是先处理带根的子树,转化成序列问题,再分治其它子树。
    然后使劲写就行了。
    #include<cstdio>
    #include<cctype>
    #include<queue>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #define rep(i,s,t) for(int i=s;i<=t;i++)
    #define dwn(i,s,t) for(int i=s;i>=t;i--)
    #define ren for(int i=first[x];i;i=next[i])
    using namespace std;
    typedef long long ll;
    const int BufferSize=1<<16;
    char buffer[BufferSize],*head,*tail;
    inline char Getchar() {
        if(head==tail) {
            int l=fread(buffer,1,BufferSize,stdin);
            tail=(head=buffer)+l;
        }
        return *head++;
    }
    inline ll read() {
        ll x=0,f=1;char c=Getchar();
        for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
        return x*f;
    }
    const int maxn=200010;
    const ll inf=1ll<<60;
    int n,e,fa[maxn],first[maxn],next[maxn<<1],to[maxn<<1];
    ll p[maxn],q[maxn],l[maxn],dep[maxn],f[maxn];
    void AddEdge(int u,int v) {
        to[++e]=v;next[e]=first[u];first[u]=e;
        to[++e]=u;next[e]=first[v];first[v]=e;
    }
    int vis[maxn],g[maxn],s[maxn],size,root;
    void getroot(int x,int f2) {
        s[x]=1;int maxs=0;
        ren if(!vis[to[i]]&&to[i]!=f2) {
            getroot(to[i],x);
            s[x]+=s[to[i]];
            maxs=max(maxs,s[to[i]]);
        }
        g[x]=max(size-s[x],maxs);
        if(g[root]>g[x]) root=x;
    }
    int cnt,A[maxn],Q[maxn];
    void dfs(int x,int f2) {
        A[++cnt]=x;
        ren if(!vis[to[i]]&&to[i]!=f2) dfs(to[i],x);
    }
    void relax(int x,int y) {f[x]=min(f[x],f[y]+(dep[x]-dep[y])*p[x]+q[x]);}
    double slop(int x,int y) {return !x?1e30:(double)(f[x]-f[y])/(dep[x]-dep[y]);}
    int cmp(int x,int y) {return dep[x]-l[x]>dep[y]-l[y];}
    void solve(int x) {
        root=0;getroot(x,0);
        int now=root,rt=root;vis[now]=1;
        if(!vis[x]) size=s[x]-s[now],solve(x);cnt=0;
        for(int i=first[now];i;i=next[i]) if(!vis[to[i]]) dfs(to[i],now);
        sort(A+1,A+cnt+1,cmp);
        for(int i=now;i!=fa[x]&&dep[i]>=dep[now]-l[now];i=fa[i]) relax(now,i);
        for(int i=1,r=0;i<=cnt;i++) {
            for(;now!=fa[x]&&dep[now]>=dep[A[i]]-l[A[i]];now=fa[now]) {
                while(r>1&&slop(now,Q[r])>=slop(Q[r],Q[r-1])) r--;Q[++r]=now;
            }
            if(r) {
                int L=1,R=r;
                while(L<R) {
                    int mid=L+R+1>>1;
                    if(slop(Q[mid-1],Q[mid])>=(double)p[A[i]]) L=mid;
                    else R=mid-1;
                }
                relax(A[i],Q[L]);
            }
        }
        for(int i=first[rt];i;i=next[i]) if(!vis[to[i]]) size=s[to[i]],solve(to[i]);
    }
    int main() {
        n=size=g[0]=read();read();
        rep(i,2,n) f[i]=inf,fa[i]=read(),dep[i]=dep[fa[i]]+read(),AddEdge(fa[i],i),p[i]=read(),q[i]=read(),l[i]=read();
        solve(1);
        rep(i,2,n) printf("%lld
    ",f[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    【题解】P3565 [POI2014]HOT-Hotels
    【学习笔记】$gcd$ 与扩展 $gcd$
    【题解】P3810 【模板】三维偏序(陌上花开)- $CDQ$
    【题解】P3374 【模板】树状数组 1
    【考试总结】2020 上半年 汇总
    【题解】P4570 [BJWC2011]元素
    【题解】P2480 [SDOI2010]古代猪文
    【考试总结】小奇模拟赛
    【题解】P3349 [ZJOI2016]小星星
    搞懂ZooKeeper的Watcher之源码分析及特性总结
  • 原文地址:https://www.cnblogs.com/wzj-is-a-juruo/p/5128161.html
Copyright © 2011-2022 走看看