Housewife Wind
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 10378   Accepted: 2886

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?

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 树链剖分水题。建议在POJ上用C++交,用G++可能会超时。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int N=2e5+;
const int M=N*N+;
int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; //top 最近的重链父节点
int num,s,m,n,q;
int sum[N*],tre[*N];
vector<int> v[N];
struct tree {
int x,y,val;
void read() {
scanf("%d%d%d",&x,&y,&val);
}
}e[N];
void dfs1(int u, int f, int d) {
dep[u] = d;
siz[u] = ;
son[u] = ;
fa[u] = f;
for (int i = ; i < v[u].size(); i++) {
int ff = v[u][i];
if (ff == f) continue;
dfs1(ff, u, d + );
siz[u] += siz[ff];
if (siz[son[u]] < siz[ff])
son[u] = ff;
}
}
void dfs2(int u, int tp) {
top[u] = tp;
id[u] = ++num;
if (son[u]) dfs2(son[u], tp);
for (int i = ; i < v[u].size(); i++) {
int ff = v[u][i];
if (ff == fa[u] || ff == son[u]) continue;
dfs2(ff, ff);
}
}
inline void PushPlus(int rt) {
sum[rt]=sum[rt*]+sum[rt*+];
} void Build(int l,int r,int rt) {
if(l==r) {
sum[rt]=val[l];
return;
}
int m=(l+r)>>;
Build(lson);
Build(rson);
PushPlus(rt);
//printf("rt=%d sum[rt]=%d\n",rt,sum[rt]);
} void Update(int p,int add,int l,int r,int rt) {
if(l==r) {
sum[rt]=add;
return;
}
int m=(r+l)>>;
if(p<=m)Update(p,add,lson);
else Update(p,add,rson);
PushPlus(rt);
} int Query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R)return sum[rt];
int m=(l+r)>>;
int ans=;
if(L<=m)ans+=Query(L,R,lson);
if(R>m)ans+=Query(L,R,rson);
return ans;
} int Yougth(int u, int v) {
int tp1 = top[u], tp2 = top[v];
int ans = ;
while (tp1 != tp2) {
if (dep[tp1] < dep[tp2]) {
swap(tp1, tp2);
swap(u, v);
}
ans += Query(id[tp1], id[u],,n,);
u = fa[tp1];
tp1 = top[u];
}
if (u == v) return ans;
if (dep[u] > dep[v]) swap(u, v);
ans += Query(id[son[u]], id[v],,n,);
return ans;
}
void Clear(int n) {
for(int i=; i<=n; i++)
v[i].clear();
}
int main() {
int u,vv,w;
scanf("%d%d%d",&n,&q,&s);
for(int i=; i<n; i++) {
e[i].read();
v[e[i].x].push_back(e[i].y);
v[e[i].y].push_back(e[i].x);
}
num = ;
dfs1(,,);
dfs2(,);
for (int i = ; i < n; i++) {
if (dep[e[i].x] < dep[e[i].y]) swap(e[i].x, e[i].y);
val[id[e[i].x]] = e[i].val;
}
Build(,num,);
while(q--) {
int x;
scanf("%d",&x);
if(!x){
scanf("%d",&u);
printf("%d\n",Yougth(s,u));
s=u;
}
else {
scanf("%d%d",&u,&vv);
Update(id[e[u].x],vv,,n,);
}
}
Clear(n); return ;
}
												

POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)的更多相关文章

  1. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  2. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  3. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  4. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  5. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  6. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  7. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  8. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  9. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  10. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

随机推荐

  1. CV限制符--C++

    C/C++提供多种声明变量和函数存储持续性.作用域和链接性的关键字,有些被称为存储说明符(store class specifier)或 cv 限定符(cv-qualifier),这里就一起学习一下c ...

  2. Percona-Tookit工具包之pt-summary

      Preface       As a dba,We are obliged to master several basic tools(such as vmstat,top,netstat,ios ...

  3. QA 、 QC & QM软件测试入门专业名词解释 -- 灌水走起

    灌水正式开始: 说明:我的农田,我灌水 一.QA . QC & QM: 1.QM QM 是quanlity management,中文名称是品质管理 2.QA QA是英文quality ass ...

  4. mac虚拟机上(centos系统)设置联网

    前面介绍了mac安装虚拟机VirtualBox,并在虚拟机上装上了centos,这里在说明一下联网问题. 首先打开centos系统,并输入命令$ ip addr 可得到如下: 发现找不到ip地址,这时 ...

  5. java5初学

    1.Eclipse快捷键 alt + / 代码提示,例如自动创建main方法ctrl + d 删除当前行ctrl + alt + up/down 复制当前行alt + up/down 交换行ctrl ...

  6. Elasticsearch相关度评分_score

    相关度评分 _score 的目的 是为了将当前查询的结果进行排序,比较不同查询结果的相关度评分没有太大意义. _score的计算方式 score(q,d) = # score(q,d) 是文档 d 与 ...

  7. 【bzoj1856】[Scoi2010]字符串 Catalan数

    题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgww想要知道满足 ...

  8. LeetCode -- Sum Root to Leaf NNumbers

    Related Links: Path Sum: http://www.cnblogs.com/little-YTMM/p/4529982.html Path Sum II: http://www.c ...

  9. 洛谷 P3765 总统选举 解题报告

    P3765 总统选举 题目背景 黑恶势力的反攻计划被小C成功摧毁,黑恶势力只好投降.秋之国的人民解放了,举国欢庆.此时,原秋之国总统因没能守护好国土,申请辞职,并请秋之国人民的大救星小C钦定下一任.作 ...

  10. poj 2186 强连通入门题目

    每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...