POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 10378 | Accepted: 2886 |
Description
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
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
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(树链剖分)(线段树单点修改)的更多相关文章
- POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )
POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- HDU4897 (树链剖分+线段树)
Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...
- 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 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- HDU 2460 Network(双连通+树链剖分+线段树)
HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- 【Matrix Factorization】林轩田机器学习技法
在NNet这个系列中讲了Matrix Factorization感觉上怪怪的,但是听完第一小节课程就明白了. 林首先介绍了机器学习里面比较困难的一种问题:categorical features 这种 ...
- loadrunner 欺骗ip设置
工具准备:loadrunner12,windows 10 ip欺骗=ip wizard 前提条件:本机IP地址为固定地址,不是自动获取的地址 方法: 1.管理员身份打开cmd 2.输入命令:confi ...
- 替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child's parent first.
在将一个fragment替换到一个frameLayout的时候报错: code: transaction.replace(R.id.fragment_container, fragment2); 错误 ...
- Makefile编写记录
近期学习 Linux 需要使用 Makefile,网上搜罗了很多这方面的资料,所里在这里做一个整理. 1.静态模式 看一个例子: objects = foo.o bar.o all: $(object ...
- 【bzoj3685】普通van Emde Boas树 权值zkw线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6809743.html 题目描述 设计数据结构支持:1 x 若x不存在,插入x2 x 若x存在,删除x3 输 ...
- CLion 的 Debug 模式是怎么回事
我对这个问题产生兴趣的起因是 Codeoforces Avito Challenges 2018 的 E 题,我想到了正解,但写得太慢,最后一刻才提交.有个地方写错,结果是 Runtime error ...
- [bzoj] 1176 Mokia || CDQ分治
原题 给出W×W的矩阵(S没有用,题目有误),给出无限次操作,每次操作的含义为: 输入1:你需要把(x,y)(第x行第y列)的格子权值增加a 输入2:你需要求出以左下角为(x1,y1),右上角为(x2 ...
- let与const区别
let 1. let有变量提升,但是有约束 2. 会形成暂时性死区(TDZ) 3. 同一个块级作用域内不允许声明相同变量 4. 块级变量 5. let声明的全局变量不是全局对象的属性,var会 6. ...
- ubuntu安装出现"删除initramfs-tools时出错",subprocess installed post-installation script returned error exit status 1
昨日准备重装ubuntu,增大了系统容量,因为前面用到boot分区不到100M,于是这里分区如下 /boot 100M / 30G /home 50G 然后安装快结束时就出现如下图问题 开始以为是镜像 ...
- HDU 1054树形DP入门
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...