题目链接:##

点我

题目分析:##

树剖。将边权下放到下方点上(为什么要选深度更深的点?一个父亲可能对应多个儿子,但一个儿子只有一个父亲,即可以保证每个点只保存一条边权)成为经典点权+树剖裸题

注意链计算时不能把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] 月下”毛景“树的更多相关文章

  1. 洛谷P4315 月下“毛景树”

    题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬毛毛虫爬到了一颗小小的"毛景树&quo ...

  2. 洛谷P4315 月下“毛景树”(树剖+线段树)

    传送门 woc这该死的码农题…… 把每一条边转化为它连接的两点中深度较深的那一个,然后就可以用树剖+线段树对路径进行修改了 然后顺便注意在上面这种转化之后,树剖的时候不能搞$LCA$ 然后是几个注意点 ...

  3. P4315 月下“毛景树”

    P4315 月下"毛景树" 题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬 ...

  4. P4315 月下“毛景树”(树链剖分)

    P4315 月下"毛景树"(树链剖分) 题面 简述: 边权转点权(在dfs1处转换) 把一条边权赋值在深度更深的上 需要实现对单边权的染色 , 路径边权的染色 , 路径边权的增加 ...

  5. P4315 月下“毛景树” (树链剖分+边剖分+区间覆盖+区间加+区间最大值)

    题目链接:https://www.luogu.org/problem/P4315 题目大意: 有N个节点和N-1条树枝,但节点上是没有毛毛果的,毛毛果都是长在树枝上的.但是这棵“毛景树”有着神奇的魔力 ...

  6. P4315 月下“毛景树”[树剖]

    题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬毛毛虫爬到了一颗小小的"毛景树&quo ...

  7. BZOJ 1984: 月下“毛景树” [树链剖分 边权]

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1728  Solved: 531[Submit][Status][Discu ...

  8. 【BZOJ1984】月下“毛景树” 树链剖分+线段树

    [BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...

  9. 【BZOJ-1984】月下“毛景树” 树链剖分

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1314  Solved: 416[Submit][Status][Discu ...

随机推荐

  1. 在CI框架中的配置整合amfphp

    之前做的项目用到CI框架和amfphp的整合,主要用于php与flex的交互,在此做一下记录: 一. 安装CI框架: 1.  搭建PHP运行环境,本人在WIN7下用WAMP作测试,安装目录:d:/wa ...

  2. struts2的结果类型

    1.从struts-default.xml入手,得到结果类型列表以及对应的处理类: <result-types> <!-- 转发到action --> <result-t ...

  3. Java与国际化

    i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是"国际化"的简称. Java使用java.util.ResourceBu ...

  4. ACM学习历程—BestCoder 2015百度之星资格赛1001 大搬家(递推 && 组合数学)

    Problem Description 近期B厂组织了一次大搬家,所有人都要按照指示换到指定的座位上.指示的内容是坐在位置i 上的人要搬到位置j 上.现在B厂有N 个人,一对一到N 个位置上.搬家之后 ...

  5. Stamps

    链接 分析:dp[i][j]表示前i个数,组成j,最少需要多少个.dp[i][j]=min(dp[i-1][j],dp[i-1][j-k*v[i]]+k),则可以转化为完全背包问题,同样的方法进行降维 ...

  6. java计算两个时间相差(天、小时、分钟、秒)

    public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...

  7. Spring boot 学习八 Springboot的filter

    一:  传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...

  8. Lagom学习 (三)

    lagom代码中有大量的Lambda表达式,首先补习一下lambda表达式和函数式接口的相关知识. 一: 函数式接口: 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口: 这种类型的接口,使得 ...

  9. DataWindow.Net V2.5原始文件下载

    DW2.5 原始安装程序下载 http://download.sybase.com/eval/datawindowNET_25_eval/CD50090-55-0250-01.zip

  10. 再学IHanlder 类----------------关于Asp.net与iis原理网上看博客收获写一个验证码用一般处理程序记的好长时间前就写过不过现在再看有点不一样的感觉

    建一个web网站 新建一般处理程序直接贴代码: using System;using System.Collections.Generic;using System.Linq;using System ...