题意:给你一颗树,每次会修改一条边的边权,问修改之后的树的直径是多少?

思路:来源于:https://www.cnblogs.com/TinyWong/p/11260601.html

得到树的全序dfs序之后,我们考虑用线段树维护x - 2 * y + z。维护方法和2017, 2016那道题差不多,对于每个区间维护:x, -y, z, x - 2 * y, -2 * y + z, x - 2 * y + z6个部分的最大值,然后区间合并。

代码:

#include <bits/stdc++.h>
#define ls (o << 1)
#define rs (o << 1 | 1)
#define LL long long
#define INF 1e18
using namespace std;
const int maxn = 100010;
int lp[maxn], rp[maxn], mp[maxn * 2], tot;
vector<pair<int, long long> > G[maxn];
LL d[maxn];
void add(LL x, LL y, LL z) {
G[x].push_back(make_pair(y, z));
G[y].push_back(make_pair(x, z));
}
struct edge {
int u, v;
LL w;
};
edge a[maxn];
struct Seg {
LL v[6], lz;
//0: a
//1: b
//2: c
//3: a + 2 * b
//4: 2 * c + b
//5: a + b + 2 * c
};
Seg tr[maxn * 8];
void pushup(int o) {
//0:
tr[o].v[0] = max(tr[ls].v[0], tr[rs].v[0]);
//1:
tr[o].v[1] = max(tr[ls].v[1], tr[rs].v[1]);
//2:
tr[o].v[2] = max(tr[ls].v[2], tr[rs].v[2]);
//3:
tr[o].v[3] = max(tr[ls].v[3], tr[rs].v[3]);
tr[o].v[3] = max(tr[o].v[3], tr[ls].v[0] + 2ll * tr[rs].v[1]);
//4:
tr[o].v[4] = max(tr[ls].v[4], tr[rs].v[4]);
tr[o].v[4] = max(tr[o].v[4], 2ll * tr[ls].v[1] + tr[rs].v[2]);
//: 5
tr[o].v[5] = max(tr[ls].v[5], tr[rs].v[5]);
tr[o].v[5] = max(tr[o].v[5], tr[ls].v[3] + tr[rs].v[2]);
tr[o].v[5] = max(tr[o].v[5], tr[ls].v[0] + tr[rs].v[4]);
}
void maintain(int o, LL x) {
tr[o].lz += x;
tr[o].v[0] += x;
tr[o].v[1] -= x;
tr[o].v[2] += x;
tr[o].v[3] -= x;
tr[o].v[4] -= x;
}
void pushdown(int o) {
if(tr[o].lz) {
maintain(ls, tr[o].lz);
maintain(rs, tr[o].lz);
tr[o].lz = 0;
}
}
void build(int o, int l, int r) {
tr[o].lz = 0;
if(l == r) {
tr[o].v[0] = tr[o].v[2] = d[mp[l]];
tr[o].v[4] = tr[o].v[3] = tr[o].v[1] = -d[mp[l]];
tr[o].v[5] = 0;
return;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
pushup(o);
}
void update(int o, int l, int r, int ql, int qr, LL val) {
if(l >= ql && r <= qr) {
maintain(o, val);
return;
}
int mid = (l + r) >> 1;
pushdown(o);
if(ql <= mid) update(ls, l, mid, ql, qr, val);
if(qr > mid) update(rs, mid + 1, r, ql, qr, val);
pushup(o);
}
void dfs(int x, int fa, LL dis) {
mp[++tot] = x;
lp[x] = tot;
rp[x] = tot;
d[x] = dis;
for (auto y : G[x]) {
if(y.first == fa) continue;
dfs(y.first, x, dis + y.second);
mp[++tot] = x;
rp[x] = tot;
}
}
int main() {
int n, m;
LL w, x, y, z;
scanf("%d%d%lld", &n, &m, &w);
for (int i = 1; i < n; i++) {
scanf("%lld%lld%lld", &x, &y, &z);
add(x, y, z);
a[i].u = x, a[i].v = y, a[i].w = z;
}
dfs(1, 0, 0);
build(1, 1, tot);
LL ans = 0;
while(m--) {
scanf("%lld%lld", &x, &y);
x = (x + ans) % (n - 1) + 1;
y = (y + ans) % w;
int p;
if(lp[a[x].u] < lp[a[x].v]) p = a[x].v;
else p = a[x].u;
update(1, 1, tot, lp[p], rp[p], y - a[x].w);
a[x].w = y;
ans = tr[1].v[5];
printf("%lld\n", ans);
}
}

  

Codeforces 1192B 全dfs序 + 线段树的更多相关文章

  1. Codeforces 916E(思维+dfs序+线段树+LCA)

    题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...

  2. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  3. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  4. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  5. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  7. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  8. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

  9. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

随机推荐

  1. idea创建项目和依赖

    创建项目和依赖 项目搭建完成后打开web.xml可看到如图使用的是servlet2.3,但版本太老,servlet2.3 jsp  的el表达式不工作,所以我们需要切换新版本. 切换新版本方法:打开t ...

  2. 基于python实现自动化办公学习笔记四

    PPT(1)写PPT import win32comimport win32com.client def makeppt(path): ppt = win32com.client.Dispatch(& ...

  3. SQL Server系列之 删除大量数据

    一.写在前面 - 想说爱你不容易 为了升级数据库至SQL Server 2008 R2,拿了一台现有的PC做测试,数据库从正式库Restore(3个数据库大小夸张地达到100G+),而机器内存只有可怜 ...

  4. mkdir: 无法创建目录"kk": 只读文件系统

    创建文件提示 root@hap1:/test>mkdir kk mkdir: 无法创建目录"kk": 只读文件系统 root@hap1:/test>mount .... ...

  5. Package manager has died异常PackageInfo 引发 Crash

    Android 获取 PackageInfo 引发 Crash 填坑 一般 Android 通过PackageInfo这个类来获取应用安装包信息,比如应用内包含的所有Activity名称.应用版本号之 ...

  6. Krypton Suite of .NET WinForms Controls

    The Krypton Suite of .NET WinForms controls are now freely available for use in personal or commeric ...

  7. jvm jstack log分析工具,在线分析

    http://spotify.github.io/threaddump-analyzer Spotify提供的Web版在线分析工具,可以将锁或条件相关联的线程聚合到一起.

  8. MyBatis Generator 生成的example 使用 and or 简单混合查询

    MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...

  9. js:获取单选组radio中的被选择的数据

    现在有一name为sex的单选组,代表的是选择性别,要求获取radio中被选择的选项值 <div class="sexDiv"> 用户性别: <input cla ...

  10. python基础-7.2文件内置属性__doc__ __file__ __package__ __cached__ __name__

    __doc__ #py文件开头的注释文字 __file__ #当前py文件的路径 __package__ #包含文件所在包,用 . 分隔,当前文件为None,导入的其他文件:指定文件所在包,用.分隔. ...