poj 2763(树链剖分)边剖(树状数组版)
来源:互联网 发布:最新网络术语 编辑:程序博客网 时间:2024/06/05 22:47
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?
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?
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.
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.
For each message A, print an integer X, the time required to take the next child.
3 3 11 2 12 3 20 21 2 30 3
13
Description
给你一棵树 2种操作0 x 求当前点s到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000+20;#define lowbit(x) (x)&(-x)struct sad{ int to,next,id;}G[maxn<<2];int h[maxn],si;void add(int u,int v,int id){ G[si].to=v; G[si].id=id; G[si].next=h[u]; h[u]=si++;}int siz[maxn],dep[maxn];int fa[maxn],son[maxn],top[maxn];int fid[maxn];void dfs1(int u,int f,int d){ fa[u]=f; dep[u]=d; siz[u]=1; son[u]=-1; for(int i=h[u];~i;i=G[i].next){ int v=G[i].to; if(v^f){ fid[G[i].id]=v; dfs1(v,u,d+1); siz[u]+=siz[v]; if(son[u]==-1||siz[son[u]]<siz[v]) son[u]=v; } }}int p[maxn],fp[maxn],pos;void dfs2(int u,int sf){ top[u]=sf; p[u]=pos++; fp[p[u]]=u; if(son[u]==-1) return ; dfs2(son[u],sf); for(int i=h[u];~i;i=G[i].next){ int v=G[i].to; if(son[u]!=v&&fa[u]!=v) dfs2(v,v); }}int w[maxn];int c[maxn];void update(int x,int val){ while(x<pos) { c[x]+=val; x+=lowbit(x); }}int sum(int x){ int res=0; while(x>0) { res+=c[x]; x-=lowbit(x); } return res;}void init(){ memset(h,-1,sizeof(h)); memset(c,0,sizeof(c)); si=0; pos=1;}int query(int u,int v){ int res=0; int f1=top[u],f2=top[v]; while(f1!=f2) { if(dep[f1]<dep[f2]) { swap(f1,f2); swap(u,v); } res+=sum(p[u]); res-=sum(p[f1]-1); u=fa[f1]; f1=top[u]; } if(u==v) return res; if(dep[u]>dep[v]) swap(u,v); res+=sum(p[v]); res-=sum(p[son[u]]-1); return res;}int main(){ int n,q,s; while(~scanf("%d%d%d",&n,&q,&s)) { init(); for(int i=1;i<n;i++) { int u,v; scanf("%d%d%d",&u,&v,&w[i]); add(u,v,i); add(v,u,i); } dfs1(1,-1,1); dfs2(1,1); for(int i=1;i<n;i++) update(p[fid[i]],w[i]); while(q--) { int op; scanf("%d",&op); if(op==0) { int d; scanf("%d",&d); printf("%d\n",query(s,d) ); s=d; } if(op==1) { int a,b; scanf("%d%d",&a,&b); update(p[fid[a]],-w[a]); update(p[fid[a]],b); w[a]=b; } } }}
阅读全文
0 0
- poj 2763(树链剖分)边剖(树状数组版)
- poj 2481(树状数组)
- poj 2299(树状数组)
- poj 1195(树状数组)
- poj 2481(树状数组)
- poj 3067(树状数组)
- poj 2029(树状数组)
- poj 2155(树状数组)
- 树状数组 (poj 2352)
- POJ-1990(树状数组)
- poj 2352(树状数组)
- POJ 2299 (树状数组)
- 树状数组(poj 1195,poj 3321)
- POJ 2763 Housewife Wind (LCA+树状数组)
- poj 2155 Matrix(二维树状数组)
- poj 2155(二维树状数组)
- POJ-1990(维持两个树状数组)
- poj 2155 (二维树状数组)
- Android.mk语法简介
- Python3 正则表达式
- Glog日志模块
- Linux必学命令-文件处理类命令
- Travis.ci
- poj 2763(树链剖分)边剖(树状数组版)
- 决策树
- POJ 2524--Ubiquitous Religious
- URAL 1002|Phone Numbers|动态规划
- codeforces 546E. Soldier and Traveling 网络流
- CorelDraw应该怎么样才能学好描图技巧看看这个就能懂
- 试题3:二维数组中的查找
- java中的访问权限
- Spark Streaming基于状态、窗口的实时数据流