• poj 2763(在线LCA+树状数组)


                                                           Housewife Wind

    After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.

    Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!'

    At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.

    Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?
    Input
    The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.

    The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.

    The following q lines each is one of the following two types:

    Message A: 0 u
    A kid in hut u calls Wind. She should go to hut u from her current position.
    Message B: 1 i w
    The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.
    Output
    For each message A, print an integer X, the time required to take the next child.
    Sample Input
    3 3 1
    1 2 1
    2 3 2
    0 2
    1 2 3
    0 3
    
    Sample Output
    1
    3
    
    题意很简单 k为0时:求s到u的距离 k为1时 把第i条边的权值变为w
    求a b两点的距离 用LCA 设d[i]为i到根节点的距离 l(a.b)=d[a]+d[b]-2*d[lca(a,b)];(很容易想到)
    修改权值的话 我们用L[i],R[i]分别表示在DFS中第一次经过i节点的时间戳和回溯到该点的时间戳
    节点u到根节点的距离就是[0,L[u]]的和
    对于更新每一条边时 DFS序较大的的节点为i 将l[i]的权值加上一个w R[i]+1的权值减去一个w 这样l{i]-R[i]区间内所有的顶点在求和的时候都加了w

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<map>
      5 #include<cstdlib>
      6 #include<vector>
      7 #include<set>
      8 #include<queue>
      9 #include<cstring>
     10 #include<string.h>
     11 #include<algorithm>
     12 typedef long long ll;
     13 typedef unsigned long long LL;
     14 using namespace std;
     15 const int N=200000+100;
     16 int head[N];
     17 int cnt,n;
     18 int vis[N];
     19 int d[N],dp[N][30];
     20 struct node{
     21     int to,next,w;
     22 }edge[2*N];
     23 int t1,t2;
     24 int pos[N],dep[N],f[N],G[N];
     25 int w[N];
     26 int L[N],R[N];
     27 void init(){
     28     memset(vis,0,sizeof(vis));
     29     cnt=0;
     30     memset(head,-1,sizeof(head));
     31     memset(pos,-1,sizeof(pos));
     32     memset(d,0,sizeof(d));
     33     t1=t2=0;
     34 }
     35 void add(int u,int v,int w){
     36     edge[cnt].to=v;
     37     edge[cnt].w=w;
     38     edge[cnt].next=head[u];
     39     head[u]=cnt++;
     40 }
     41 int lowbit(int x){
     42     return x&-x;
     43 }
     44 void update(int x,int y){
     45     while(x<=n){
     46         d[x]=d[x]+y;
     47         x=x+lowbit(x);
     48     }
     49 }
     50 int sum(int x){
     51     int ans=0;
     52     while(x){
     53         ans=ans+d[x];
     54         x=x-lowbit(x);
     55     }
     56     return ans;
     57 }
     58 void init_RMQ(int n){
     59     for(int i=1;i<=n;i++)dp[i][0]=i;
     60     for(int j=1;(1<<j)<=n;j++)
     61     for(int i=1;i+(1<<j)-1<=n;i++)
     62     if(dep[dp[i][j-1]]<dep[dp[i+(1<<j-1)][j-1]])dp[i][j]=dp[i][j-1];
     63     else
     64         dp[i][j]=dp[i+(1<<j-1)][j-1];
     65         //dp[i][j]=min(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
     66 }
     67 int RMQ(int l,int r){
     68     int k=0;
     69     while((1<<k+1)<=r-l+1)k++;
     70     if(dep[dp[l][k]]<dep[dp[r-(1<<k)+1][k]])return dp[l][k];
     71     else
     72         return dp[r-(1<<k)+1][k];
     73     //return min(dep[dp[l][k]],dep[dp[r-(1<<k)+1][k]]);
     74 }
     75 int lca(int u,int v){
     76     if(pos[u]>pos[v])return f[RMQ(pos[v],pos[u])];
     77     else
     78         return f[RMQ(pos[u],pos[v])];
     79 }
     80 void DFS(int x,int deep){
     81     f[t1]=x;
     82     dep[t1]=deep;
     83     pos[x]=t1++;
     84     L[x]=++t2;
     85     for(int i=head[x];i!=-1;i=edge[i].next){
     86         //cout<<4<<endl;
     87         int v=edge[i].to;
     88         if(pos[v]==-1){
     89             //cout<<3<<endl;
     90             G[edge[i].w]=v;
     91             DFS(v,deep+1);
     92             f[t1]=x;
     93             dep[t1++]=deep;
     94         }
     95 
     96     }
     97     R[x]=t2;
     98 }
     99 int main(){
    100     int q,s;
    101     while(scanf("%d%d%d",&n,&q,&s)!=EOF){
    102         init();
    103         int u,v,W;
    104         for(int i=1;i<n;i++){
    105             scanf("%d%d%d",&u,&v,&W);
    106             add(u,v,i);
    107             add(v,u,i);
    108             w[i]=W;
    109         }
    110         DFS(1,0);
    111         /*for(int i=1;i<=n;i++){
    112             cout<<L[i]<<" "<<R[i]<<endl;
    113         }
    114         cout<<endl;
    115         for(int i=1;i<=2*n-1;i++)cout<<f[i]<<" ";
    116         cout<<endl;*/
    117         init_RMQ(2*n-1);
    118         for(int i=1;i<n;i++){
    119             update(L[G[i]],w[i]);
    120             update(R[G[i]]+1,-w[i]);
    121         }
    122         u=s;
    123         while(q--){
    124             int k;
    125             scanf("%d",&k);
    126             if(k==1){
    127                 scanf("%d%d",&u,&W);
    128                 update(L[G[u]],W-w[u]);
    129                 update(R[G[u]]+1,-W+w[u]);
    130                 w[u]=W;
    131             }
    132             else{
    133                 scanf("%d",&v);
    134                 printf("%d
    ",sum(L[s])+sum(L[v])-2*sum(L[lca(s,v)]));
    135                 s=v;
    136             }
    137         }
    138     }
    139     return 0;
    140 }
  • 相关阅读:
    js 遇到问题
    table 排序 添加 删除 等操作
    json对象
    .style, .getComputedStyle(),.currentStyle区别
    3个div 宽度移入移出时变化
    运动 js
    OWASP Top 10之文件上传漏洞简析(二)
    owasp top10 之文件上传漏洞简析
    前台实现ajax 需注意的地方
    apache-Rewrite重写规则配置
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6738688.html
Copyright © 2020-2023  润新知