codeforces 592D 树链剖分

题意:给一棵树,边上有权值,有两种操作

1 a b f 输出f div(a、b两点间的路径权值和)  向下取整

2 a f  把第a条边的权值修改为f~~~


一开始就想到是树链剖分,但是被数据范围给吓到了,开了一个long double,,但是long double卡效率啊~~结果TLE

后来改为long long,特判一下如果大于1e18就直接赋值为1e18~~~避免溢出  AC了~~   不能用long double声明变量啊~~~~


结果我去看别人的代码,有更加简单的方法~~

由于边的大小只会越来越小,一直小到1,可以利用这一点做文章~~

我们可以暴力往上搜索,且一路合并路径为1的边,(即更改fa[u]的值)

如son->father->grandfather这一条路径~~

当处理到son这个节点时,如果father->grandfather这条边的大小为1的话,那么可以直接把fa[son]=grandfather,跳过father~~~直接处理grandfather这个点~~~


#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define exp 1e-8
#define fi first
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
#define cnt_one(i) __builtin_popcount(i)
#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;
void bug(string m="here"){cout< inline void READ(__ll &m){__ll x=0,f=1;char ch=getchar();while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}m=x*f;}
templateinline void read(__ll &m){READ(m);}
templateinline void read(__ll &m,__ll &a){READ(m);READ(a);}
templateinline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
templateinline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
templateinline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}
template < class T > inline  void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}
template < class T > inline  void outln(T a){out(a);puts("");}
template < class T > inline  void out(T a,T b){out(a);putchar(' ');out(b);}
template < class T > inline  void outln(T a,T b){out(a);putchar(' ');outln(b);}
template < class T > inline  void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}
template < class T > inline  void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }

const int cnt_edge=400010;  //修改啊
const int cnt_v=200010;
int head[cnt_v],cnt_e;
struct EDGE{int u,v,next;int cost;}edge[cnt_edge];
void addedge(int u,int v,int cost=0)
{edge[cnt_e].u=u;edge[cnt_e].v=v;edge[cnt_e].cost=cost;edge[cnt_e].next=head[u];head[u]=cnt_e++;}
void init(){cnt_e=0;memset(head,-1,sizeof(head));}
#define erg(i,u) for(int i=head[u];i!=-1;i=edge[i].next)
int tot;
int lr[cnt_v][2];
ll cost[cnt_v];
int idx[cnt_v];
int fa[cnt_v];
void dfs(int u,int pre)
{
    fa[u]=pre;
    lr[u][0]=tot;
    erg(i,u)
    {
        int v=edge[i].v;
        if(v==pre)continue;
        idx[v]=edge[i].cost;//v这个点  对应的是第cost条边~~~~
        dfs(v,u);
    }
    lr[u][1]=tot++;
}
int bingo(int k)//进行路径压缩,即修改fa的值~~
{
    return cost[idx[k]]==1?fa[k]=bingo(fa[k]):k;
}
void solve(int a,int b,ll &c)
{
    if(!a||!c)return;
    if(lr[a][0]<=lr[b][0]&&lr[b][1]<=lr[a][1])return;
    c/=cost[idx[a]];
    solve(fa[a]=bingo(fa[a]),b,c);
}
int main()
{
    init();
    int n,m;
    read(n,m);
    for1(i,n-1)
    {
        int a,b;
        read(a,b);read(cost[i]);
        addedge(a,b,i);
        addedge(b,a,i);
    }
    tot=1;
    dfs(1,0);
    int opt,a,b;ll ff;
    while(m--)
    {
        read(opt);
        if(opt==1)
        {
            read(a,b);
            read(ff);
            solve(a,b,ff);
            solve(b,a,ff);
            outln(ff);
        }
        else
        {
            read(a);read(ff);
            cost[a]=ff;
        }
    }
}


树链剖分的代码:

#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define exp 1e-8
#define fi first
#define ll long long
#define INF 1e18
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for0(a,b) for(int a=0;a<=b;a++)//0---(b-1)
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
#define cnt_one(i) __builtin_popcount(i)
#define stl(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;
void bug(string m="here"){cout< inline void READ(__ll &m){__ll x=0,f=1;char ch=getchar();while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}m=x*f;}
templateinline void read(__ll &m){READ(m);}
templateinline void read(__ll &m,__ll &a){READ(m);READ(a);}
templateinline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
templateinline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
templateinline void read(__ll &m,__ll &a,__ll &b,__ll &c,__ll &d){READ(m);READ(a);READ(b);READ(c);read(d);}
template < class T > inline  void out(T a){if(a<0){putchar('-');a=-a;}if(a>9)out(a/10);putchar(a%10+'0');}
template < class T > inline  void outln(T a){out(a);puts("");}
template < class T > inline  void out(T a,T b){out(a);putchar(' ');out(b);}
template < class T > inline  void outln(T a,T b){out(a);putchar(' ');outln(b);}
template < class T > inline  void out(T a,T b,T c){out(a);putchar(' ');out(b);putchar(' ');out(c);}
template < class T > inline  void outln(T a,T b,T c){out(a);putchar(' ');outln(b);putchar(' ');outln(b);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }

const int cnt_edge=400010;  //Ð޸İ¡
const int cnt_v=200010;
int head[cnt_v],cnt_e;
struct EDGE{int u,v,next,cost;}edge[cnt_edge];
void addedge(int u,int v,int cost=0)
{edge[cnt_e].u=u;edge[cnt_e].v=v;edge[cnt_e].cost=cost;edge[cnt_e].next=head[u];head[u]=cnt_e++;}
void init(){cnt_e=0;memset(head,-1,sizeof(head));}
#define erg(i,u) for(int i=head[u];i!=-1;i=edge[i].next)

int top[cnt_v];
int fa[cnt_v];
int deep[cnt_v];
int num[cnt_v];
int p[cnt_v];
int son[cnt_v];
int pos;
int eee[cnt_v][2];
ll cost[cnt_v];

void INIT()
{
    init();
    pos = 0;
    memset(son,-1,sizeof(son));
}
void dfs1(int u,int pre,int d)
{
    deep[u]=d;
    fa[u]=pre;
    erg(i,u)
    {
        int v=edge[i].v;
        if(v==pre)continue;
        dfs1(v,u,d+1);
        num[u]+=num[v];
        if(son[u]==-1||num[v]>num[son[u]])
            son[u]=v;
    }
}
void getpos(int u,int sp)
{
    top[u]=sp;
    p[u]=++pos;
    if(son[u]==-1)return;
    getpos(son[u],sp);
    erg(i,u)
    {
        int v=edge[i].v;
        if(v!=son[u]&&v!=fa[u])
            getpos(v,v);
    }
}
long double sum[cnt_v*3];
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
void build(int l,int r,int rt)
{
    sum[rt]=0;
    if(l==r)return;
    int m=l+r>>1;
    build(lson);
    build(rson);
}
void modify(int l,int r,int rt,int pos,long double val)
{
    if(l==r)
    {
        sum[rt]=val;
        return;
    }
    int m=l+r>>1;
    if(pos<=m)modify(lson,pos,val);
    else modify(rson,pos,val);
    if((long double)sum[rt<<1]*sum[rt<<1|1]>=INF)
        sum[rt]=INF;
    else
        sum[rt]=sum[rt<<1]*sum[rt<<1|1];
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)return sum[rt];
    int m=l+r>>1;
    long double ret=1.0;
    if(L<=m)ret*=(long double)query(L,R,lson);
    if(R>m)ret*=(long double)query(L,R,rson);
    return ret>INF?INF:(ll)ret;
}
ll find(int u,int v)
{
    int f1=top[u],f2=top[v];
    long double ret=1.0;
    while(f1!=f2)
    {
        if(deep[f1]=INF)return INF;
        u=fa[f1];f1=top[u];
    }
    if(u==v)return ret;
    if(deep[u]>deep[v])swap(u,v);
    ret*=(long double)query(p[son[u]],p[v],1,pos,1);
    return ret>=INF?INF:ret;
}
int main()
{
    int n,m;
    INIT();
    read(n,m);
    for1(i,n-1)
    {
        read(eee[i][0],eee[i][1]);
        read(cost[i]);
        addedge(eee[i][0],eee[i][1]);
        addedge(eee[i][1],eee[i][0]);
    }
    dfs1(1,0,0);
    getpos(1,1);
    build(1,pos,1);
    for(int i=1;ideep[eee[i][1]])
            swap(eee[i][0],eee[i][1]);
        modify(1,pos,1,p[eee[i][1]],cost[i]);
    }
    int opt,a,b;
    ll  ff,gg;
    while(m--)
    {
        read(opt);
        if(opt==1)
        {
            read(a,b);
            read(ff);
            if(a==b)
            {
                outln(ff);
                continue;
            }
            gg=find(a,b);
            ff/=gg;
            outln(ff);
        }
        else
        {
            read(a);
            read(ff);
            modify(1,pos,1,p[eee[a][1]],ff);
        }
    }
}



你可能感兴趣的:(树链剖分,codeforces)