Codeforces 1192B 全dfs序 + 线段树
题意:给你一颗树,每次会修改一条边的边权,问修改之后的树的直径是多少?
思路:来源于: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序 + 线段树的更多相关文章
- Codeforces 916E(思维+dfs序+线段树+LCA)
题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- 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 ...
- 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 ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
随机推荐
- 2014ACM-ICPC广州站题解(摘自闭幕式)
第39届ACM-ICPC亚洲区广州站题解 Ltysky摘抄自闭幕式题目分析 Problem A 满足px+qy=c的点(x,y)在一条直线上,而c的值由直线的截距确定,所以最大化c,就要在糖果(x,y ...
- 设计模式学习笔记——Bridge 桥接模式
先说一下我以前对桥接模式的理解:当每个类中都使用到了同样的属性或方法时,应该将他们单独抽象出来,变成这些类的属性和方法(避免重复造轮子),当时的感觉是和三层模型中的model有点单相似,也就是让mod ...
- eclipse jshint安装
http://www.cnblogs.com/lhb25/archive/2012/04/05/javascript-validation-with-jshint-eclipse.html
- fastjson学习笔记
先来说说什么是 JSON 吧. JSON:JavaScript对象表示法(JavaScript Object Notation).JSON 是存储和交换文本信息的语法.JSON 语法是 JavaScr ...
- you have not created a boot efi partition
rhel6.8 自定义分区,创建 /boot/efi 分区找不到”EFI System Partition“文件系统, 选其他文件系统 next时都提示:you have not created a ...
- Visual Studio使用技巧 +谷歌浏览器使用技巧总结
一.总结下visual studio常用的使用技巧,有助于提高效率: 1.给代码行打标记: ctrl + K :给行打标记:ctrl + K + N:切换标记,即使当前页关闭了,也可以适用此快捷键快 ...
- Ta还没有分享呢,过段时间再来看看吧~ 解决办法
自己摸索出来的.只能查看以前分享的奥. 找到要查看用户的id号 利用特百度搜索工具实现检索 http://www.tebaidu.com/user--1.html 将红字部分替换为刚才复制的u ...
- P4999烦(gui)人(chu)的数学作业
P4399P4999 这是一道有着三倍经验的宝藏题目 我们可以求出来1到n中,1~9分别出现了几次,设f[i]为数字i出现的次数,则\(ans=\sum{f[i]\cdot i}\) 然后就是数位dp ...
- The import org.apache.hadoop.mapreduce cannot be resolved
ubuntu@VM---ubuntu:~$ sudo apt--src.tar.gz Reading package lists... Done Building dependency tree Re ...
- Vuex模块:不开启命名空间
模块不开启命名空间时,会共享全局命名空间. { state: { 模块1: "局部状态1", 模块2: "局部状态2" }, getters: { getter ...