zoukankan      html  css  js  c++  java
  • codeforces#329div2 D. Happy Tree Party 树链剖分

    D. Happy Tree Party
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 biusing 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.

    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 uivi and xi (1 ≤ ui, vi ≤ nui ≠ 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:

    • ai bi yi corresponds to a guest, who chooses the operation of the first type.
    • 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 ≤ n1 ≤ 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 test(s)
    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
    output
    2
    4
    20
    3
    input
    5 4
    1 2 7
    1 3 3
    3 4 2
    3 5 5
    1 4 2 100
    1 5 4 1
    2 2 2
    1 1 3 4
    output
    2
    0
    2
    由于a/b/c=a/(b*c),所以直接维护乘积即可,注意爆longlong。
    这里由于溢出可以溢回正数,所以不能用判断是否负数来判溢出,直接在中间过程设一个double型的tmp判断是否大于limit就可以了。。
    #include<bits/stdc++.h>
    #define REP(i,a,b) for(int i=a;i<=b;i++)
    #define MS0(a) memset(a,0,sizeof(a))
    #define PB push_back
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    using namespace std;
    
    typedef long long ll;
    const int maxn=1000100;
    const ll INF=(1LL<<60);
    
    struct Tree
    {
        int u,v;
        ll w;
    };Tree tree[maxn];
    int fa[maxn],dep[maxn],top[maxn],son[maxn],siz[maxn];
    int id[maxn],num;
    int n,m;
    vector<int> G[maxn];
    ll val[maxn];
    int op,u,v;ll y;
    int p;ll c;
    ll w;
    struct SegTree
    {
        int l,r;
        ll val;
        void debug()
        {
            printf("%2d %2d %2d
    ",l,r,val);
        }
    };SegTree T[maxn<<2];
    const ll limit=1e18+28;
    
    void dfs(int u,int f,int d)
    {
        fa[u]=f;
        dep[u]=d;
        siz[u]=1;
        son[u]=0;
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(v==f) continue;
            dfs(v,u,d+1);
            siz[u]+=siz[v];
            if(siz[v]>siz[son[u]]) son[u]=v;
        }
    }
    
    void dfs1(int u,int tp)
    {
        top[u]=tp;
        id[u]=++num;
        if(son[u]) dfs1(son[u],tp);
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(v==son[u]||v==fa[u]) continue;
            dfs1(v,v);
        }
    }
    
    void push_up(int rt)
    {
        double tmp=T[rt<<1].val*1.0*T[rt<<1|1].val;
        if(tmp>limit) T[rt].val=limit+1;
        else T[rt].val=T[rt<<1].val*T[rt<<1|1].val;
    }
    
    void build(int l,int r,int rt)
    {
        T[rt].l=l;T[rt].r=r;
        if(l==r){
            T[rt].val=val[l];
            return;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        push_up(rt);
    }
    
    ll query(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R) return T[rt].val;
        ll res=1;
        int m=(l+r)>>1;
        if(L<=m){
            ll a=res,b=query(L,R,lson);
            double tmp=a*1.0*b;
            if(tmp>limit) res=limit+1;
            else res=a*b;
        }
        if(R>m){
            ll a=res,b=query(L,R,rson);
            double tmp=a*1.0*b;
            if(tmp>limit) res=limit+1;
            else res=a*b;
        }
        return res;
    }
    
    void update(int p,ll c,int l,int r,int rt)
    {
        if(l==r){
            T[rt].val=c;
            return;
        }
        int m=(l+r)>>1;
        if(p<=m) update(p,c,lson);
        else update(p,c,rson);
        push_up(rt);
    }
    
    ll cha(int u,int v)
    {
        ll res=1;
        while(top[u]!=top[v]){
            if(dep[top[u]]<dep[top[v]]) swap(u,v);
            if(top[u]!=u){
                ll a=res,b=query(id[top[u]]+1,id[u],1,num,1);
                double tmp=a*1.0*b;
                if(tmp>limit) res=limit+1;
                else res=a*b;
            }
            ll a=res,b=query(id[top[u]],id[top[u]],1,num,1);
            double tmp=a*1.0*b;
            if(tmp>limit) res=limit+1;
            else res=a*b;
            u=fa[top[u]];
        }
        if(dep[u]>dep[v]) swap(u,v);
        if(u!=v){
            ll a=res,b=query(id[u]+1,id[v],1,num,1);
            double tmp=a*1.0*b;
            if(tmp>limit) res=limit+1;
            else res=a*b;
        }
        return res;
    }
    
    ll change(int u,ll c)
    {
        update(id[u],c,1,num,1);
    }
    
    int main()
    {
        freopen("in.txt","r",stdin);
        while(cin>>n>>m){
            REP(i,0,n) G[i].clear();
            MS0(son);
            REP(i,1,n-1){
                scanf("%d%d%I64d",&u,&v,&w);
                tree[i]={u,v,w};
                G[u].PB(v);G[v].PB(u);
            }
            num=0;
            dfs(1,0,1);
            dfs1(1,1);
            val[1]=1;
            REP(i,1,n-1){
                if(dep[tree[i].v]<dep[tree[i].u]) swap(tree[i].u,tree[i].v);
                val[id[tree[i].v]]=tree[i].w;
            }
            build(1,num,1);
            while(m--){
                scanf("%d",&op);
                if(op==1){
                    scanf("%d%d%I64d",&u,&v,&y);
                    printf("%I64d
    ",y/cha(u,v));
    
                }
                else{
                    scanf("%d%I64d",&p,&c);
                    change(tree[p].v,c);
                }
            }
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    Java基础知识(四)使用多线程插入数据
    Java基础知识(三)重写equals和hashCode
    Java基础知识(二)基本数据类型转换
    Java基础知识(一)基本数据类型默认值
    C# DES加密,KEY和IV不同设置的写法
    [异常记录(三)] 从 bcp 客户端收到一个对 colid 12 无效的列长度
    ADO.NET 使用DELETE语句批量删除操作,提示超时,删除失败,几种优化解决思路
    [异常记录(二)] 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。
    [解决]JS失效,提示HTML1114: (UNICODE 字节顺序标记)的代码页 utf-8 覆盖(META 标记)的冲突的代码页 utf-8
    SQL SERVER 2012/ 2014 分页,用 OFFSET,FETCH NEXT改写ROW_NUMBER的用法
  • 原文地址:https://www.cnblogs.com/--560/p/4939625.html
Copyright © 2011-2022 走看看