POJ 2763 Housewife Wind
Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 5798 | Accepted: 1455 |
Description
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?
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.
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
树链剖分 ,边上有权 ,单点更新,区间查询
#include <cstdio> #include <queue> #include <algorithm> #include <iostream> #include <cstring> using namespace std; #define root 1,n,1 #define lr rt<<1 #define rr rt<<1|1 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; const int N= 100100; int n,q,s; int eh[N],et[N<<2],nxt[N<<2],w[N<<2],tot=0; int pos,p[N],top[N],rnk[N],dep[N],fa[N],son[N],siz[N]; struct node { int u,v,w; }e[N<<2]; void init() { memset(eh,-1,sizeof(eh)); memset(son,-1,sizeof(son)); tot=0; pos=0; } void addedge(int u,int v){ et[tot]=v;nxt[tot]=eh[u];eh[u]=tot++; et[tot]=u;nxt[tot]=eh[v];eh[v]=tot++; } void dfs1(int u,int father,int d) { dep[u]=d; fa[u]=father; siz[u]=1; for(int i=eh[u];~i;i=nxt[i]){ int v=et[i]; if(v != father){ dfs1(v,u,d+1); siz[u] += siz[v]; if( son[u]==-1 || siz[v] > siz[son[u]]) son[u]=v; } } } void dfs2(int u,int tp) { top[u]=tp; p[u]= ++pos; rnk[ p[u] ]= u; if( son[u] == -1 )return ; dfs2(son[u], tp ); for(int i=eh[u]; ~i ;i=nxt[i]){ int v=et[i]; if(v != son[u] && v!=fa[u] ){ dfs2(v,v); } } } int sum[ N<<2 ]; void Up(int rt) { sum[rt]=sum[lr]+sum[rr]; } void build(int l,int r,int rt) { if(l == r ){ sum[rt]=0; return ; } int m=(l+r)>>1; build(lson); build(rson); Up(rt); } void update(int l,int r,int rt,int x,int v) { if( l == r ){ sum[rt]=v; return ; } int m=(l+r)>>1; if(x<=m) update(lson,x,v); if(x>m) update(rson,x,v); Up(rt); } int query(int l,int r,int rt,int L,int R) { int res=0; if( L<=l&&r<=R ){ return sum[rt]; } int m=(l+r)>>1; if(L <= m) res += query(lson,L,R); if(R > m) res += query(rson,L,R); return res; } int Q(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 += query(root,p[f1],p[u]); u=fa[f1]; f1=top[u]; } if(u==v)return res; if(dep[u]>dep[v])swap(u,v); res += query(root,p[son[u]],p[v]); return res; } int run() { int u,v,w; int op; while(~scanf("%d%d%d",&n,&q,&s)){ init(); for(int i= 0;i<n-1;++i){ scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); addedge(e[i].u,e[i].v); } dfs1(1,0,0); dfs2(1,1); build(root); for(int i=0;i<n-1;++i) { if(dep[e[i].u] > dep[e[i].v]) swap(e[i].u,e[i].v); update(root,p[e[i].v],e[i].w); } while(q--) { scanf("%d",&op); if( !op ) { scanf("%d",&u); printf("%d\n",Q(s,u)); s=u; } else { scanf("%d%d",&v,&w); update(root,p[ e[v-1].v ],w); } } } return 0; } int main() { //freopen("in.txt","r",stdin); ios::sync_with_stdio(0); return run(); }
only strive for your goal , can you make your dream come true ?
标签:
树链剖分
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
· 《HelloGitHub》第 108 期
· 一个基于 .NET 开源免费的异地组网和内网穿透工具