树链剖分后要处理的是边的权值,而不是点的权值,但是只要边权下放到点,就可以了

如图

但是问题是,求图4->5路径的权值之和, 那么就会把点3给算进去

那么就要减去,

或者干脆不加进去

有两种方法

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
const int INF = <<;
/*
12 1
1 2 1
2 3 1
3 4 1
4 5 1
5 11 1
11 12 1
1 6 1
6 7 1
7 8 1
6 9 1
6 10 1
1 9 10
*/
const int N = + ;
vector<int> g[N];
int size[N], son[N], fa[N], depth[N], top[N], w[N], num, ra[N];
LL tree[N*];
int edge[N][];
int h[N];
void dfs(int u)
{
size[u] = ;
son[u] = ;
for (int i = ; i < g[u].size(); ++i)
{
int v = g[u][i];
if (v != fa[u])
{
fa[v] = u;
depth[v] = depth[u] + ;
dfs(v);
size[u] += size[v];
if (size[v] > size[son[u]])
son[u] = v;
}
}
} void dfs2(int u, int tp)
{
w[u] = ++num;
ra[num] = u;
top[u] = tp;
if (son[u] != )
dfs2(son[u], top[u]);
for (int i = ; i < g[u].size(); ++i)
{
int v = g[u][i];
if (v != fa[u] && v != son[u])
dfs2(v, v);
} }
void pushUp(int rt)
{
tree[rt] = tree[rt << ] + tree[rt << | ];
}
void update(int l, int r, int rt, int pos, int val)
{
if (l == r)
{
tree[rt] = val;
return;
}
int mid = (l + r) >> ;
if (pos <= mid)
update(l, mid, rt << , pos, val);
else
update(mid + , r, rt << | , pos, val);
pushUp(rt);
} LL ans;
void query(int l, int r, int rt, int L, int R)
{
if (L <= l&&R >= r)
{
ans += tree[rt];
return;
}
int mid = (l + r) >> ;
if (L <= mid)
query(l, mid, rt << , L, R);
if (R > mid)
query(mid + , r, rt << | , L, R);
}
int main()
{
int n, m, a, b, c;
while (scanf("%d%d", &n, &m) != EOF)
{
memset(tree, , sizeof(tree));
for (int i = ; i <= n; ++i)
g[i].clear();
num = ;
for (int i = ; i < n; ++i)
{
scanf("%d%d%d", &edge[i][], &edge[i][], &edge[i][]);
g[edge[i][]].push_back(edge[i][]);
g[edge[i][]].push_back(edge[i][]);
}
fa[] = depth[] = ;
dfs();
dfs2(, );
for (int i = ; i < n; ++i)
{
if (depth[edge[i][]] > depth[edge[i][]])
swap(edge[i][], edge[i][]);
h[edge[i][]] = edge[i][];
update(, n, , w[edge[i][]], edge[i][]);
}
while (m--)
{
scanf("%d%d%d", &a, &b, &c);
if (a == )
{
if (depth[edge[b][]] > depth[edge[b][]])
swap(edge[b][], edge[b][]);
update(, n, , w[edge[b][]], c);
h[edge[b][]] = c;
}
else
{
ans = ;
while (top[b] != top[c])
{
if (depth[top[b]] < depth[top[c]])
swap(b, c);
query(, n, , w[top[b]], w[b]);
b = fa[top[b]];
}
//b和c一定是在一条链上
if (depth[b]>depth[c])
swap(b, c); //如果b有父亲,那么就有边下放到它,多加了这个点,要减去
query(, n, , w[b], w[c]);
if (fa[b] != )
ans -= h[b];
/*
如果b,c是在一条重链上, 那么询问w[son[b]] 到w[c]
那么就没有加到点b
否则,b,c就是指向同一点,那么w[son[b]] > w[c] , 询问是不会增加任何值的
query(1, n, 1, w[son[b]], w[c]);
*/
printf("%lld\n", ans);
}
}
}
return ;
}

FZU2082的更多相关文章

  1. FZU2082树链剖分

    简单题. #include<queue> #include<stack> #include<cmath> #include<cstdio> #inclu ...

随机推荐

  1. 函数alv下的颜色设置

    ABAP中的颜色代码是由4位字都组成的 cxyz    c:color的简写,颜色代码均以C开头 x:标准色代码,SAP中一共有7个标准色    y:反转颜色启用/关闭 1/0 z:增强颜色启用/关闭 ...

  2. web.xml中listener作用及使用

    一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...

  3. Python Errors and Exceptions

    1. python中的try{}catch{} 2. raise exception 3. try...except ... else.. 4. finally块 python中的异常处理的keywo ...

  4. Oracle dump 分析secondary key

    验证secondary key 含有主键列数据 SQL> select object_name,object_id,OBJECT_TYPE from user_objects; OBJECT_N ...

  5. 2014年Windows平台软件推荐:神器小工具(骨灰级

    原文  http://www.wtoutiao.com/a/120621.html 底层工具 “If you know how to use Process Monitor competently, ...

  6. Memcahce(MC)系列(一)Memcache介绍、使用、存储、算法、优化

    写在前面:前不久在工作中被问到关于MC一致哈希的问题,由于时隔太久差点儿忘记,特前来恶补一下MC,下面是前几年在工作中学习MC时的一些资料,来历不明,特整理一下,希望对大家的学习也能有帮助. 32 的 ...

  7. [Android学习笔记]Android调试

    Eclipse Debug 快捷键: [Ctrl + Shift + B]: 添加/取消断点 [F5]:进入方法中 [F6]:单步执行 [F7]:执行完毕此方法 [F8]:继续执行,直接跳到下一个断点 ...

  8. latex命令替换之\newcommand

    有时候我们在用latex写文档的时候不想写很长的命令,那么我们自己定义一个新的命令来替换一段代码. 举例如下: \usepackage{booktabs} \usepackage{multirow} ...

  9. symbol(s) not found for architecture i386

    此问题针对百度地图真机调试和模拟器.a文件的选取问题 "$(SRCROOT)/MobileYonyou/Third/BaiduMap_IOSSDK_v2.3.0_Lib/Release$(E ...

  10. Android万能适配器base-adapter-helper的源代码分析

    项目地址:https://github.com/JoanZapata/base-adapter-helper 1. 功能介绍 1.1. base-adapter-helper base-adapter ...