POJ 2763 Housewife Wind(DFS序+LCA+树状数组)
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 11419 | Accepted: 3140 |
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
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 2763
给定一棵树,两种操作,一种是询问当前点到v点的最短距离,并且输出答案之后当前点就变成v点了;第二种是把某一条边的长度变为w。
先考虑只有询问的情况下怎么做,显然是LCA裸题,设dis[u]代表u点到根节点的距离,则有:$dis(u, v) = dis[u]+dis[v]-2*dis[LCA(u,v)]$,然后考虑第二种操作,可以发现当你改变一条边的长度(权值)的时候,会对这条边下的整颗子树造成影响,比如u——v的一条边,且u比v更靠近根节点的话,那么造成的影响是v所在整颗子树的影响,即子树是深度较深的点所连接的。由于是整颗子树,明显可以用DFS序来转换后用树状数组或者线段树来维护这个DFS序列即dis数组,操作就是区间更新,单点查询——改变边长度的时候显然整个子树序列区间长度均改变了$w'-w$。好像这题一般写法是树链剖分,蒟蒻我不会……就用树状数组好了,树状数组怎么区间更新?左端点处+val,右端点+1处-val即可。
ZZ地WA了一下午发现是把D数组当作深度数组来用了……
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,sum) memset(arr,sum,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct edge
{
int to, nxt, d;
edge() {}
edge(int _to, int _nxt, int _d): to(_to), nxt(_nxt), d(_d) {}
};
edge E[N << 1];
int head[N], tot;
int ver[N << 1], D[N << 1], F[N], sz, dp[N << 1][19];
int W[N], A[N], B[N];
int L[N], R[N], T[N], ts;
bitset<N>vis; void init()
{
CLR(head, -1);
tot = 0;
sz = 0;
ts = 0;
CLR(T, 0);
vis.reset();
}
void update(int k, int v)
{
while (k < N)
{
T[k] += v;
k += (k & -k);
}
}
int query(int k)
{
int ret = 0;
while (k)
{
ret += T[k];
k -= (k & -k);
}
return ret;
}
inline void add(int s, int t, int d)
{
E[tot] = edge(t, head[s], d);
head[s] = tot++;
}
void dfs(int u, int d, int sumdis)
{
ver[++sz] = u;
D[sz] = d;
F[u] = sz; L[u] = ++ts;
update(L[u], sumdis);
update(L[u] + 1, -sumdis);
vis[u] = 1;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!vis[v])
{
dfs(v, d + 1, sumdis + E[i].d);
ver[++sz] = u;
D[sz] = d;
}
}
R[u] = ts;
}
void RMQinit(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
dp[i][0] = i;
for (j = 1; l + (1 << j) - 1 <= r; ++j)
{
for (i = l; i + (1 << j) - 1 <= r; ++i)
{
int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];
dp[i][j] = D[a] < D[b] ? a : b;
}
}
}
int LCA(int u, int v)
{
int l = F[u], r = F[v];
if (l > r)
swap(l, r);
int len = r - l + 1;
int k = 0;
while (1 << (k + 1) <= len)
++k;
int a = dp[l][k], b = dp[r - (1 << k) + 1][k];
return D[a] < D[b] ? ver[a] : ver[b];
}
int main(void)
{
int n, q, s, i, a, b, w;
while (~scanf("%d%d%d", &n, &q, &s))
{
init();
for (i = 1; i <= n - 1; ++i)
{
scanf("%d%d%d", &a, &b, &w);
A[i] = a;
B[i] = b;
W[i] = w;
add(a, b, w);
add(b, a, w);
}
dfs(s, 1, 0);
RMQinit(1, sz);
while (q--)
{
int ops;
scanf("%d", &ops);
if (ops == 0)
{
int v;
scanf("%d", &v);
printf("%d\n", query(L[s]) + query(L[v]) - 2 * query(L[LCA(s, v)]));
s = v;
}
else
{
int k, neww;
scanf("%d%d", &k, &neww);
int u = A[k], v = B[k];
if (L[u] > L[v])
swap(u, v);
update(L[v], neww - W[k]);
update(R[v] + 1, -(neww - W[k]));
W[k] = neww;
}
}
}
return 0;
}
POJ 2763 Housewife Wind(DFS序+LCA+树状数组)的更多相关文章
- BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...
- HDU 6203 ping ping ping(dfs序+LCA+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意: n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连 ...
- HDU 3966 dfs序+LCA+树状数组
题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...
- BZOJ 2819: Nim dfs序维护树状数组,倍增
1.随机选两个堆v,u,询问若在v到u间的路径上的石子堆中玩Nim游戏,是否有必胜策略,如果有,vfleaking将会考虑将这些石子堆作为初始局面之一,用来坑玩家.2.把堆v中的石子数变为k. 分析: ...
- 【Tyvj2133&BZOJ1146】网络管理Network(树套树,DFS序,树状数组,主席树,树上差分)
题意:有一棵N个点的树,每个点有一个点权a[i],要求在线实现以下操作: 1:将X号点的点权修改为Y 2:查询X到Y的路径上第K大的点权 n,q<=80000 a[i]<=10^8 思路: ...
- 【BZOJ1103】大都市meg(DFS序,树状数组)
题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线: A:将连接x,y的边标记 W:查询从1到x的路径上有多少条边未被标记 n<=2*10^5 ...
- 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)
4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...
- 【POJ3321】Apple Tree(DFS序,树状数组)
题意:给一棵n个节点的树,每个节点开始有一个苹果,m次操作 1.将某个结点的苹果数异或 1 2.查询一棵子树内的苹果数 n,m<=100000 思路:最近一段时间在思考树上统计问题的算法 发 ...
- BZOJ 1103: [POI2007]大都市meg(dfs序,树状数组)
本来还想链剖的,结果才发现能直接树状数组的= = 记录遍历到达点与退出点的时间,然后一开始每个到达时间+1,退出时间-1,置为公路就-1,+1,询问直接点1到该点到达时间求和就行了- - CODE: ...
随机推荐
- 深入理解计算机系统_3e 第十一章家庭作业 CS:APP3e chapter 11 homework
注:tiny.c csapp.c csapp.h等示例代码均可在Code Examples获取 11.6 A. 书上写的示例代码已经完成了大部分工作:doit函数中的printf("%s&q ...
- VC-基础-WebBrowser控件中弹出新网页窗口
用webbrowser控件浏览网页时,常弹出新的网页窗口,若不做任何控制的话,会在默认浏览器(一般是IE)中打开,这样就在新的窗口打开了,原程序就很难控制了,且存在webbrowser控件和IE的se ...
- 解决Jquery中使用each循环时,循环外的js依旧会执行
今天在改项目bug时,发现一个问题,我获取一个div中所有的input,并取值时,判断某一条件,但是循环外的js依然可以执行. $(".tab-reg-next input").e ...
- C# StreamWriter对像
用FileWriter来随机读取文件是个好主意,而用StreamWriter可以直接把字符串写入文件中,它处理重要的转换和向FileStream对像写入工作.创建StreamWriter有很多方法: ...
- java 程序设计第三次作业内容
第一题:输出结果是什么? System.out.println("5+5="+5+5); 第二题:输出结果是什么? int a=3,b; b=a++; sop("a=&q ...
- 网络流_spfa最小费用最大流
最大流: 不断搜索增广路,寻找最小的容量-流量,得到最大流量,但最大流量在有花费时不一定是最小花费. 最小费用最大流 算法思想: 采用贪心的思想,每次找到一条从源点到达汇点的花费最小的路径,增加流量, ...
- 数据库引擎InnoDB和myisam的区别和联系
1.ENGINE=InnoDB 数据库存储引擎,DEFAULT 默认,CHARSET=utf8 数据库字符编码 2.数据库的存储引擎, mysql中engine=innodb和engine=myisa ...
- .net core IdentityServer4 使用query参数
基本用法请参考官方文档:https://identityserver4.readthedocs.io/en/latest/index.html 这里不对具体用法进行说明,一般情况下,Startup添加 ...
- node实现一个简单的聊天室(认识一下socket)
边学边理解node的高深,今天写了一个聊天室的demo,很简单,认识一下socket node服务端代码 var express = require('express'); var app = exp ...
- Linux下启动、停止xampp命令
启动xampp: /opt/lampp/./lampp start 停止xampp: /opt/lampp/./lampp stop 卸载xampp: rm -rf /opt/lampp