• Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分


    D. Happy Tree Party

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/593/problem/D

    Description

    Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every edge of the tree i, some number xi was written on it. In case you forget, a tree is a connected non-directed graph without cycles. After the present was granted, m guests consecutively come to Bogdan's party. When the i-th guest comes, he performs exactly one of the two possible operations:

    1. Chooses some number yi, and two vertecies ai and bi. After that, he moves along the edges of the tree from vertex ai to vertex bi using the shortest path (of course, such a path is unique in the tree). Every time he moves along some edge j, he replaces his current number yi by , that is, by the result of integer division yi div xj.
    2. Chooses some edge pi and replaces the value written in it xpi by some positive integer ci < xpi.

    As Bogdan cares about his guests, he decided to ease the process. Write a program that performs all the operations requested by guests and outputs the resulting value yi for each i of the first type.


    Under two situations the player could score one point.

    ⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

    ⋅2. Ignoring the buoys and relying on dogfighting to get point. If you and your opponent meet in the same position, you can try to fight with your opponent to score one point. For the proposal of game balance, two players are not allowed to fight before buoy #2 is touched by anybody.

    There are three types of players.

    Speeder: As a player specializing in high speed movement, he/she tries to avoid dogfighting while attempting to gain points by touching buoys.
    Fighter: As a player specializing in dogfighting, he/she always tries to fight with the opponent to score points. Since a fighter is slower than a speeder, it's difficult for him/her to score points by touching buoys when the opponent is a speeder.
    All-Rounder: A balanced player between Fighter and Speeder.

    There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
    Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

    Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting. Since Asuka is slower than Shion, she decides to fight with Shion for only one time during the match. It is also assumed that if Asuka and Shion touch the buoy in the same time, the point will be given to Asuka and Asuka could also fight with Shion at the buoy. We assume that in such scenario, the dogfighting must happen after the buoy is touched by Asuka or Shion.

    The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

    Input

    The first line of the input contains integers, n and m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ 200 000) — the number of vertecies in the tree granted to Bogdan by his mom and the number of guests that came to the party respectively.

    Next n - 1 lines contain the description of the edges. The i-th of these lines contains three integers ui, vi and xi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ xi ≤ 1018), denoting an edge that connects vertecies ui and vi, with the number xi initially written on it.

    The following m lines describe operations, requested by Bogdan's guests. Each description contains three or four integers and has one of the two possible forms:

    • 1 ai bi yi corresponds to a guest, who chooses the operation of the first type.
    • 2 pi ci corresponds to a guests, who chooses the operation of the second type.

    It is guaranteed that all the queries are correct, namely 1 ≤ ai, bi ≤ n, 1 ≤ pi ≤ n - 1, 1 ≤ yi ≤ 1018 and 1 ≤ ci < xpi, where xpi represents a number written on edge pi at this particular moment of time that is not necessarily equal to the initial value xpi, as some decreases may have already been applied to it. The edges are numbered from 1 to n - 1 in the order they appear in the input.

    Output

    For each guest who chooses the operation of the first type, print the result of processing the value yi through the path from ai to bi.

    Sample Input

    6 6
    1 2 1
    1 3 7
    1 4 4
    2 5 5
    2 6 2
    1 4 6 17
    2 3 2
    1 4 6 17
    1 5 5 20
    2 4 1
    1 5 1 3
     

    Sample Output

    2
    4
    20
    3

    HINT

    题意

     给你一颗树,然后有两个操作

    1 x y z,假设x到y的链上边权分别为x1,x2,x3....,那么输出 [[[z/x1]/x2]/x3]

    2 x y,把第x条边的边权改为y

    题解:

    有一个结论 [[[z/x1]/x2]/x3] = [z/(x1*x2*x3)]

    那么我们只要维护区间乘积就好了,树链剖分裸题

    但是区间乘积很容易爆LL,那么我们就用一个log来判断,如果爆了ll,那就直接把他置为1e18

    然后答案直接输出0就好了

    代码

    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <stdio.h>
    #include <math.h>
    using namespace std;
    const int N=800010;
    const int INF=1<<30;
    
    int n,tim;
    
    long long num[N];
    int siz[N],top[N],son[N];
    int dep[N],tid[N],Rank[N],fa[N];
    int head[N],to[2*N],Next[2*N],w[2*N],edge;
    int flag = 0;
    struct Edge
    {
        int u,v;
        long long c;
    };
    Edge tmp[2*N];
    
    void Init()
    {
        memset(head,-1,sizeof(head));
        memset(son,-1,sizeof(son));
        tim=0;
        edge=0;
    }
    
    void addedge(int u,int v,int c)
    {
        to[edge]=v,w[edge]=c,Next[edge]=head[u],head[u]=edge++;
        to[edge]=u,w[edge]=c,Next[edge]=head[v],head[v]=edge++;
    }
    
    //树链剖分部分
    void dfs1(int u,int father,int d)
    {
        dep[u]=d;
        fa[u]=father;
        siz[u]=1;
        for(int i=head[u]; ~i; i=Next[i])
        {
            int v=to[i];
            if(v!=father)
            {
                dfs1(v,u,d+1);
                siz[u]+=siz[v];
                if(son[u]==-1||siz[v]>siz[son[u]])
                    son[u]=v;
            }
        }
    }
    
    void dfs2(int u,int tp)
    {
        top[u]=tp;
        tid[u]=++tim;
        Rank[tid[u]]=u;
        if(son[u]==-1) return;
        dfs2(son[u],tp);
        for(int i=head[u]; ~i; i=Next[i])
        {
            int v=to[i];
            if(v!=son[u]&&v!=fa[u])
                dfs2(v,v);
        }
    }
    
    //线段树部分
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    const long long kkk = 1e18+28;
    long long MAX[4*N];
    
    void PushUP(int rt)
    {
        if(MAX[rt<<1]==0)
            MAX[rt]=1;
        if(MAX[rt<<1|1]==0)
            MAX[rt<<1|1]=1;
        double ppp1 = MAX[rt<<1];
        double ppp2 = MAX[rt<<1|1];
        if(log(ppp1) + log(ppp2) > log(kkk * 1.0))
            MAX[rt]=kkk+5;
        else
            MAX[rt]=MAX[rt<<1]*MAX[rt<<1|1];
    }
    
    void Build(int l,int r,int rt)
    {
        if(l==r)
        {
            MAX[rt]=num[l];
            return;
        }
        int mid=(l+r)>>1;
        Build(lson);
        Build(rson);
        PushUP(rt);
    }
    
    void Update(int l,int r,int rt,int p,long long val)
    {
        if(l==r)
        {
            MAX[rt]=val;
            return;
        }
        int mid=(l+r)>>1;
        if(p<=mid)
            Update(lson,p,val);
        else
            Update(rson,p,val);
        PushUP(rt);
    }
    
    long long Query(int l,int r,int rt,int L,int R)
    {
        if(L<=l&&R>=r)
            return MAX[rt];
        int mid=(l+r)>>1;
        long long ans=1;
        if(L<=mid)
        {
            long long kkkk = Query(lson,L,R);
            if(kkkk==-1)
                flag = 1;
            if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
                ans = kkk+203;
            else
                ans*=kkkk;
        }
        if(R>mid)
        {
            long long kkkk = Query(rson,L,R);
            if(kkkk==-1)
                flag = 1;
            if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
                ans = kkk+203;
            else
                ans*=kkkk;
        }
        return ans;
    }
    
    void Change(int x,long long val)
    {
        if(dep[tmp[x].u]>dep[tmp[x].v])
            Update(2,n,1,tid[tmp[x].u],val);
        else
            Update(2,n,1,tid[tmp[x].v],val);
    }
    
    long long query(int x,int y)
    {
        long long ans=1;
        while(top[x]!=top[y])
        {
            if(dep[top[x]]<dep[top[y]]) swap(x,y);
            long long kkkk = Query(2,n,1,tid[top[x]],tid[x]);
            if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
                ans = kkk+203;
            else
                ans*=kkkk;
            x=fa[top[x]];
        }
        if(dep[x]>dep[y]) swap(x,y);
        if(x!=y)
        {
            long long kkkk =Query(2,n,1,tid[x]+1,tid[y]);
            if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
                ans = kkk+203;
            else
                ans*=kkkk;
        }
        return ans;
    }
    int MM;
    int main()
    {
        char oper[15];
        int a,b;
        long long c;
        Init();
        scanf("%d%d",&n,&MM);
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%lld",&a,&b,&c);
            tmp[i].u=a;tmp[i].v=b;tmp[i].c=c;
            addedge(a,b,c);
        }
        dfs1(1,1,1);
        dfs2(1,1);
        //用边的孩子节点来表示该边
        for(int i=1;i<n;i++)
        {
            if(dep[tmp[i].u]>dep[tmp[i].v])
                num[tid[tmp[i].u]]=tmp[i].c;
            else
                num[tid[tmp[i].v]]=tmp[i].c;
        }
        Build(2,n,1);
        while(MM--)
        {
            scanf("%s",oper);
            if(oper[0]=='1')
            {
                long long ppp;
                scanf("%d%d",&a,&b);
                scanf("%lld",&ppp);
                flag = 0;
                long long dddd = query(a,b);
                printf("%lld
    ",ppp/dddd);
            }
            else
            {
                long long ppp;
                scanf("%d%lld",&a,&ppp);
                Change(a,ppp);
            }
        }
        return 0;
    }
  • 相关阅读:
    failed to create pid file /var/run/rsyncd.pid: File exists报错
    Ansible系列之roles使用说明
    设置build.gradle打包时自动加时间
    Oracle客户端连接数据库配置
    Gradle实现自动打包,签名,自定义apk文件名
    linux定时任务执行没结果,手动执行有结果问题总结
    实现TableLayout布局下循环取出TableRow控件中的文字内容到list集合
    ImageView的src和background的区别
    关于Merge的整理--Merge的使用方法和注意事项的Demo
    关于Merge的整理--AndroidScreenSlidePager开源库中用到的
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4938486.html
Copyright © 2020-2023  润新知