[洛谷P4315] 月下”毛景“树
题目链接:##
题目分析:##
树剖。将边权下放到下方点上(为什么要选深度更深的点?一个父亲可能对应多个儿子,但一个儿子只有一个父亲,即可以保证每个点只保存一条边权)成为经典点权+树剖裸题
注意链计算时不能把LCA算进去,其余细节较多,具体见代码。
代码:##
#include<bits/stdc++.h>
#define N (100000 + 5)
#define inf (1000000000+7)
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c;
c = getchar();
while(!isdigit(c)) {
if (c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) {
cnt = cnt * 10 + c - '0';
c = getchar();
}
return cnt * f;
}
int nxt[N * 2], first[N * 2], to[N * 2], w[N * 2], a[N], tot;
int father[N], top[N], siz[N], dep[N], son[N], num[N], id[N], idx;
int n, u, v, W, k;
char opr[20];
struct node{
int l, r;
long long cov, add, gmax;
#define l(p) tree[p].l
#define r(p) tree[p].r
#define cov(p) tree[p].cov
#define add(p) tree[p].add
#define gmax(p) tree[p].gmax
} tree[N * 4];
struct edge{
int u; int v;
}e[N * 2];
void Add(int x, int y, int z) {
nxt[++tot] = first[x];
first[x] = tot;
to[tot] = y;
w[tot] = z;
}
void dfs1(int cur, int fa) {
father[cur] = fa; siz[cur] = 1; dep[cur] = dep[fa] + 1;
for (register int i = first[cur]; i; i = nxt[i]) {
int v = to[i];
if(v != fa) {
a[v] = w[i];
dfs1(v, cur);
siz[cur] += siz[v];
if(siz[son[cur]] < siz[v]) son[cur] = v;
}
}
}
void dfs2(int cur, int tp) {
top[cur] = tp; num[cur] = ++idx; id[idx] = cur;
if (son[cur]) dfs2(son[cur], tp);
for (register int i = first[cur]; i; i = nxt[i]) {
int v = to[i];
if (!num[v]) dfs2(v, v);
}
}
void pushup(int p) {
gmax(p) = max(gmax(p << 1), gmax(p << 1 | 1));
}
void pushcover(int p, int v){cov(p) = v; add(p) = 0; gmax(p) = v;}
void pushadd(int p,int v){ add(p)+=v; gmax(p)+=v;}
void pushdown(int p) {
if (cov(p) >= 0) {
pushcover(p<<1, cov(p)); pushcover(p<<1|1, cov(p)); cov(p) = -1;
}
if (add(p)) {
pushadd(p<<1, add(p)); pushadd(p<<1|1, add(p)); add(p) = 0;
}
}
void build_tree(int p, int l, int r) {
l(p) = l; r(p) = r; cov(p) = -1;
if(l == r) {
gmax(p) = a[id[l]];
// cout<<l<<" "<<r<<" "<<gmax(p)<<endl;
return;
}
int mid = (l + r) >> 1;
build_tree(p << 1, l, mid);
build_tree(p << 1 | 1, mid + 1, r);
pushup(p);
}
void debug(int u,int l,int r){
if(l==r){
cout<<id[l]<<" "<<gmax(u)<<endl;return;
}int mid=(l+r)>>1;
debug(u*2,l,mid);debug(u*2+1,mid+1,r);
}
void Cover(int p, int l, int r, int d) {
if (l > r) return;
if (l <= l(p) && r >= r(p)) {
pushcover(p, d);
return;
}
pushdown(p);
int mid = (l(p) + r(p)) >> 1;
if (l <= mid) Cover(p << 1, l, r, d);
if (r > mid) Cover(p << 1 | 1, l, r, d);
pushup(p);
}
void Modify(int p, int l, int r, int d) {
if (l > r) return;
if (l <= l(p) && r >= r(p)) {
// if(cov(p) >= 0) {
// cov(p) += d;
// gmax(p) += d;
// } else {
// pushadd(p, d);
// }
pushadd(p, d);
return;
}
pushdown(p);
int mid = (l(p) + r(p)) >> 1;
if (l <= mid) Modify(p << 1, l, r, d);
if (r > mid) Modify(p << 1 | 1, l, r, d);
pushup(p);
}
long long query(int p, int l, int r) {
// cout<<p<<" "<<l(p)<<" "<<r(p)<<endl;
if (l <= l(p) && r >= r(p)) return gmax(p);
int mid = (l(p) + r(p)) >> 1;
pushdown(p);
//cout<<p<<endl;
long long val = -inf;
if (l <= mid) val = max (val, query(p << 1, l, r));
if (r > mid) val = max(val, query(p << 1 | 1, l, r));
return val;
}
void chain_cover(int u, int v, int d) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
Cover(1, num[top[u]], num[u], d);
u = father[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
// if (u == v) Cover(1, num[v], num[u], d);
Cover(1, num[v] + 1, num[u], d);
}
void chain_modify(int u, int v, int d) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
Modify(1, num[top[u]], num[u], d);
u = father[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
// if (u == v) Modify(1, num[v], num[u], d);
Modify(1, num[v] + 1, num[u], d);
}
long long chain_query(int u, int v) {
long long ans = -inf;
while (top[u] != top[v]) {//cout<<u<<endl;
// cout<<top[u]<<" "<<top[v]<<endl;
if (dep[top[u]] < dep[top[v]]) swap(u, v);
// cout<<top[u]<<" "<<top[v]<<endl;
// cout<<num[top[u]]<<" "<<num[u]<<endl;
// cout<<query(1,num[top[u]],num[u])<<"###"<<endl;
ans = max(ans, query(1, num[top[u]], num[u]));//cout<<"#";
u = father[top[u]];
// cout<<ans<<endl;
}
if (dep[u] < dep[v]) swap(u, v);
ans = max(ans, query(1, num[v] + 1, num[u]));
return ans;
}
void solve() {
n = read();
for (register int i = 1; i < n; i++) {
u = read(); v = read(); W = read();
e[i].u = u; e[i].v = v;
Add(u, v, W); Add(v, u, W);
}
dfs1(1, 0); dfs2(1, 1);
// for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
// for(int i=1;i<=n;i++)cout<<id[i]<<" ";cout<<endl;
build_tree(1, 1, n);
// debug(1,1,n);
// cout<<"#"<<endl;
for (;;) {
scanf("%s", opr + 1);
if (opr[1] == 'M') {
u = read(); v = read();
printf("%lld\n", chain_query(u, v));
}
if (opr[1] == 'C') {
if (opr[2] == 'o') {
u = read(); v = read(); W = read();
chain_cover(u, v, W);
}
if (opr[2] == 'h') {
u = read(); W = read();
if (dep[e[u].u] < dep[e[u].v]) swap(e[u].u, e[u].v);
// cout<<"nmsl"<<e[u].u<<" "<<e[u].v<<endl;
// cout<<"##"<<num[e[u].u]<<"WWW"<<W<<endl;
// chain_cover(num[e[u].u], num[e[u].v], W);
Cover(1, num[e[u].u], num[e[u].u], W);
}
}
if (opr[1] == 'A') {
u = read(); v = read(); W = read();
chain_modify(u, v, W);
}
if (opr[1] == 'S') break;
// debug(1,1,n);cout<<endl;
}
return;
}
int main() {
// freopen("input.in","r",stdin);
solve();
return 0;
}
[洛谷P4315] 月下”毛景“树的更多相关文章
- 洛谷P4315 月下“毛景树”
题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬毛毛虫爬到了一颗小小的"毛景树&quo ...
- 洛谷P4315 月下“毛景树”(树剖+线段树)
传送门 woc这该死的码农题…… 把每一条边转化为它连接的两点中深度较深的那一个,然后就可以用树剖+线段树对路径进行修改了 然后顺便注意在上面这种转化之后,树剖的时候不能搞$LCA$ 然后是几个注意点 ...
- P4315 月下“毛景树”
P4315 月下"毛景树" 题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬 ...
- P4315 月下“毛景树”(树链剖分)
P4315 月下"毛景树"(树链剖分) 题面 简述: 边权转点权(在dfs1处转换) 把一条边权赋值在深度更深的上 需要实现对单边权的染色 , 路径边权的染色 , 路径边权的增加 ...
- P4315 月下“毛景树” (树链剖分+边剖分+区间覆盖+区间加+区间最大值)
题目链接:https://www.luogu.org/problem/P4315 题目大意: 有N个节点和N-1条树枝,但节点上是没有毛毛果的,毛毛果都是长在树枝上的.但是这棵“毛景树”有着神奇的魔力 ...
- P4315 月下“毛景树”[树剖]
题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬毛毛虫爬到了一颗小小的"毛景树&quo ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- 【BZOJ1984】月下“毛景树” 树链剖分+线段树
[BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...
- 【BZOJ-1984】月下“毛景树” 树链剖分
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1314 Solved: 416[Submit][Status][Discu ...
随机推荐
- 淘宝双十一页面(Flexible)demo
下面的代码是看了大漠 使用Flexible实现手淘H5页面的终端适配 做的一个demo. <!DOCTYPE html> <html lang="en" ng-a ...
- 高并发压力下导致数据库bug
环境信息: linux 6.1 + oracle11.2.0.3 RAC 问题现象: 学校晚上6点选课,人数大概有3000,7点时,数据库报错如下(数据库到6点多还是可以连接的),数据库hu ...
- lucene内置的评分函数
For multiterm queries, Lucene takes the Boolean model, TF/IDF, and the vector space model and combin ...
- Tips:PowerDesigner16.5 图表显示Code以及 Columns新增Commet显示
- 集训Day3
被疯狂造谣+请家长 但生活还得继续 ...今天的题口胡一下吧明天码 PKUSC2018 D1T1 对于x:若x不翻,则x的一半到x的数都不能翻 若x翻,则x到2x都得翻 剩下随便安排 排列组合一下 P ...
- python快速上手教程
python版本 python目前的版本分为2.7和3.5,两种版本的代码目前无法兼容,查看python版本号: python --version 基本数据类型 数字类型 整型和浮点型数据和其它编程语 ...
- iview之select选择框选中内容后有空格的问题
导致原因: option组件格式化造成的.此处</Option>在另一行,只要和输出内容一行,就不会有空格了. <Select :label-in-value="true& ...
- Process 启动参数问题
c#在有些情况下需要在启动另一个程序时传递参数,这里存在两个问题. 1.如果在参数里面含有空格,那么传递过去就会变成一个字符数组,这种情况是不满足情况的,解决方案是在传递参数时将空格用一些特殊字符替换 ...
- java 原始类与封装类 的区别
int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...
- 写守护进程时碰到open函数的参数,没记住
今天写一个最简单的守护进程, 要成为一个守护进程,其实很简单了.主要步骤就4步: 1,创建进程. 2,父进程退出. 3,成为会话的头领进程. 4,将工作目录改成根目录,并把标准输入输出重定向到空设备. ...