[树链剖分] CF593D. Happy Tree Party

题目链接:D. Happy Tree Party

树链剖分是从下面两篇博客学的。

思想从这里看的(不能再清晰了):http://blog.anudeep2011.com/heavy-light-decomposition/

代码从这列看的(区间操作的非LCA思想太赞了):http://blog.sina.com.cn/s/blog_6974c8b20100zc61.html

然后是这道题目,正好学习了HLD,拿来练手了。

题意:给你一棵树,和边权值(<=1e18)。

操作1,从a走到b,经过一条边就用一正数c整除以边权值,输出最后的c。

操作2,把第a条边(输入顺序)的值改为b(b小于原来的值)。

c范围最大1e18,longlong可以存,然后a到b的路径可以先求出一路上的乘积,最后用c除就可以了。

然后问题就是乘积可能有点大,又不想写高精度乘除,结果随便用long double试了试边界值(1e9 * 1e9, (1e9-1) * 1e9, (1+1e9) * 1e9),在本地跑判断是否大于1e18,结果没发现精度问题,于是就写了=.=,用一个flag标志当前节点的乘积是不是大于1e18了,说起来long double到底是什么精度啊。。

#include<bits/stdc++.h>
#define ll long long int;
using namespace std;
const int N = 200055;
bool islarge(ll a, ll b){
    long double t1 = a, t2 = b;
    if(t1 * t2 > 1e18) return true;
    return false;
}
struct eg{
    int v, nex;
    ll w;
    eg(){}
    eg(int _v, ll _w, int _nex){ v = _v, w = _w, nex = _nex; }
}edg[N<<2];
ll sav[N][3];
int fir[N], ecnt = 0;
void add(int a, int b, ll c){
    edg[ecnt] = eg(b, c, fir[a]), fir[a] = ecnt++;
    edg[ecnt] = eg(a, c, fir[b]), fir[b] = ecnt++;
}
int fa[N], siz[N], son[N], dep[N];
void dfs(int rt){
    siz[rt] = 1, son[rt] = 0;
    for(int k = fir[rt]; k != -1; k = edg[k].nex){
        if(edg[k].v != fa[rt]){
            fa[edg[k].v] = rt;
            dep[edg[k].v] = dep[rt] + 1;
            dfs(edg[k].v);
            siz[rt] += siz[edg[k].v];
            if(siz[edg[k].v] > siz[son[rt]]) son[rt] = edg[k].v;
        }
    }
}
int w[N] = {0}, top[N] = {0}, wcnt = 0;
void dfs2(int rt, int tp){
    w[rt] = ++wcnt, top[rt] = tp;
    if(son[rt]) dfs2(son[rt], tp);
    for(int k = fir[rt]; k != -1; k = edg[k].nex){
        if(edg[k].v != son[rt] && edg[k].v != fa[rt]){
            dfs2(edg[k].v, edg[k].v);
        }
    }
}
ll tree[N << 2];
bool mflag[N << 2] = {0};
void update(int rt, int l, int r, int pos, ll val){
    if(l == r){
        tree[rt] = val;
        return;
    }
    int mid = (l+r) >> 1;
    if(pos <= mid) update(rt<<1, l, mid, pos, val);
    else update(rt<<1|1, mid+1, r, pos, val);
    if(mflag[rt<<1] | mflag[rt<<1|1]) mflag[rt] = 1;
    else{
        if(islarge(tree[rt<<1], tree[rt<<1|1])) mflag[rt] = 1;
        else tree[rt] = tree[rt<<1] * tree[rt<<1|1], mflag[rt] = 0;
    }
}
ll query(int rt, int l, int r, int ql, int qr){
    if(ql <= l && qr >= r) {
        return mflag[rt]? -1 : tree[rt];
    }
    int mid = (l+r) >> 1;
    ll tmp1 = 1, tmp2 = 1;
    if(ql <= mid) tmp1 = query(rt<<1, l, mid, ql, qr);
    if(qr > mid) tmp2 = query(rt<<1|1, mid+1, r, ql, qr);
    if(tmp1 < 0 || tmp2 < 0) return -1;
    if(islarge(tmp1, tmp2)) return -1;
    return tmp1*tmp2;
}
ll getmul(int va, int vb){
    int f1 = top[va], f2 = top[vb];
    ll res = 1, tmp;
    while(f1 != f2){
        if(dep[f1] < dep[f2]){
            swap(f1, f2);
            swap(va, vb);
        }
        tmp = query(1, 1, wcnt, w[f1], w[va]);
        if(tmp < 0) return -1;
        if(islarge(tmp, res)) return -1;
        res *= tmp;
        va = fa[f1], f1 = top[va];
    }
    if(va == vb) return res;
    if(dep[va] > dep[vb]) swap(va, vb);
    tmp = query(1, 1, wcnt, w[son[va]], w[vb]);
    if(tmp < 0 ) return -1;
    if(islarge(tmp, res)) return -1;
    return res * tmp;
}
int main(){
    int n, q, a, b, d;
    ll c;
    scanf("%d %d", &n, &q);
    memset(fir, -1, sizeof(fir));
    wcnt = ecnt = fa[1] = dep[1] = 0;
    for(int i = 0; i < N; ++i) tree[i] = 1;
    for(int i = 1; i < n; ++i){
        scanf("%d %d %lld", &a, &b, &c);
        sav[i][0] = a, sav[i][1] = b, sav[i][2] = c;
        add(a, b, c);
    }
    dfs(1);
    dfs2(1, 1);
    for(int i = 1; i < n; ++i){
        if(dep[sav[i][0]] > dep[sav[i][1]]) swap(sav[i][0], sav[i][1]);
        update(1, 1, wcnt, w[sav[i][1]], sav[i][2]);
    }
    while(q--){
        scanf("%d", &d);
        if(d == 1){
            scanf("%d %d %lld", &a, &b, &c);
            ll t = getmul(a, b);
            if(t < 0) puts("0");
            else printf("%lld\n", c/t);
        }
        else{
            scanf("%d %lld", &a, &c);
            sav[a][2] = c;
            update(1, 1, wcnt, w[sav[a][1]], c);
        }
    }
}



你可能感兴趣的:(数据结构,ACM,codeforces,树链剖分)