Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 17259 | Accepted: 4714 |
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
题目大意 : 有一棵树, 每条边有边权, 第一种操作将输入点 X 到 点 Y的距离, 并让 X = Y, 第二种操作, 更改编号为Z的边的边权
思路 :想明白以后发现非常简单, 利用dfs序在树上建立线段树, 维护的是每个点到根节点的距离, 访问到叶子结点再回溯, 过程中更新每个点的控制区间, 查询的答案和lca上查询距离一样, 根到U距离 + 根到V距离 - 2 * 根到LCA距离, 注意每次查询完更新X就好,建议画图想想
Accepted code
#include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
struct Edge
{
int v, w, next;
}e[MAXN << 1];
struct Tree
{
int l, r, ans, lzy;
}t[MAXN * 4];
int head[MAXN], n, m, rt, cnt;
int in[MAXN], out[MAXN], tot;
int p[MAXN][25], num[MAXN], dep[MAXN];
int le[MAXN], ri[MAXN];
void init() {
MEM(head, -1);
for (int i = 1; i <= n; i++) in[i] = out[i] = num[i] = dep[i] = 0;
MEM(p, 0);
cnt = tot = 0;
}
void Build(int rt, int l, int r) { // 建树
t[rt].l = l, t[rt].r = r;
t[rt].ans = t[rt].lzy = 0;
if (l == r) return;
int mid = (l + r) >> 1;
Build(ls, l, mid);
Build(rs, mid + 1, r);
}
void add(int from, int to, int wi) { // 存边
e[++cnt].v = to;
e[cnt].w = wi;
e[cnt].next = head[from];
head[from] = cnt;
}
void Pushdown(int rt) { // lazy往下
t[ls].lzy += t[rt].lzy; t[rs].lzy += t[rt].lzy;
t[ls].ans += t[rt].lzy, t[rs].ans += t[rt].lzy;
t[rt].lzy = 0;
}
void Update(int rt, int l, int r, int pos) { // 区间更新
if (t[rt].l >= l && t[rt].r <= r) {
t[rt].ans += pos; t[rt].lzy += pos;
return;
}
if (t[rt].lzy) Pushdown(rt);
int mid = (t[rt].l + t[rt].r) >> 1;
if (mid < l) Update(rs, l, r, pos);
else if (mid >= r) Update(ls, l, r, pos);
else {
Update(ls, l, mid, pos);
Update(rs, mid + 1, r, pos);
}
t[rt].ans = t[ls].ans + t[rs].ans;
}
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1, p[x][0] = fa;
num[x] = ++tot, in[num[x]] = tot; // 编号以及控制的左范围
for (int i = 1; (1 << i) <= dep[x]; i++)
p[x][i] = p[p[x][i - 1]][i - 1];
for (int i = head[x]; i != -1; i = e[i].next) {
int vi = e[i].v, wi = e[i].w;
if (vi == fa) continue;
dfs(vi, x);
Update(1, in[num[vi]], out[num[vi]], wi);
}
out[num[x]] = tot; // 控制的右范围
}
int LCA(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
for (int i = 20; i >= 0; i--) {
if (dep[y] - (1 << i) >= dep[x])
y = p[y][i];
}
if (x == y) return x;
for (int i = 20; i >= 0; i--) {
if (p[x][i] == p[y][i]) continue;
x = p[x][i], y = p[y][i];
}
return p[x][0];
}
int Query(int rt, int l, int r) { // 单点查询
if (t[rt].l >= l && t[rt].r <= r) return t[rt].ans;
int mid = (t[rt].l + t[rt].r) >> 1, ui = 0, vi = 0;
if (t[rt].lzy) Pushdown(rt);
if (mid >= l) ui = Query(ls, l, r);
if (mid < r) vi = Query(rs, l, r);
return ui + vi;
}
int main()
{
while (~sc("%d %d %d", &n, &m, &rt)) {
init();
for (int i = 1; i < n; i++) {
int ui, vi, wi;
sc("%d %d %d", &ui, &vi, &wi);
add(ui, vi, wi); add(vi, ui, wi);
le[i] = ui, ri[i] = vi;
}
Build(1, 1, n); dfs(1, 0);
for (int i = 0; i < m; i++) {
int op, ui, vi, wi;
sc("%d", &op);
if (op == 0) {
sc("%d", &vi);
int lca = LCA(rt, vi);
printf("%d\n", Query(1, num[rt], num[rt]) + Query(1, num[vi], num[vi]) - 2 * Query(1, num[lca], num[lca]));
rt = vi;
}
else {
sc("%d %d", &ui, &wi);
int l = le[ui], r = ri[ui];
int ans = dep[l] > dep[r] ? l : r;
int lca = LCA(l, r);
int stot = Query(1, num[l], num[l]) + Query(1, num[r], num[r]) - 2 * Query(1, num[lca], num[lca]); // 更新
Update(1, in[num[ans]], out[num[ans]], wi - stot);
}
}
}
return 0;
}