https://www.luogu.org/problemnew/show/P3833

树链剖分 + 线段树

为啥会RE?? 不解

#include <iostream>
#include <cstdio> using namespace std;
const int N = 1e5 + ; #define LL long long int n, T, now = , tim;
int top[N], tree[N], lst[N], rst[N], bef[N], size[N], fa[N], deep[N], son[N];
LL W[N << ], F[N << ], S[N << ];
struct Node {int v, nxt;} G[N << ];
int head[N];
LL Answer; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void Add(int u, int v) {G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;} struct Node_1 {
#define lson jd << 1
#define rson jd << 1 | 1
void Build_tree(int l, int r, int jd) {
S[jd] = r - l + ;
if(l == r) return ;
int mid = (l + r) >> ;
Build_tree(l, mid, lson);
Build_tree(mid + , r, rson);
}
void Down(int jd) {
int f = F[jd];
F[lson] += f; F[rson] += f;
W[lson] += S[lson] * f; W[rson] += S[rson] * f;
F[jd] = ;
}
void Sec_G(int l, int r, int jd, int x, int y, int yj) {
if(x <= l && r <= y) {
F[jd] += yj; W[jd] += S[jd] * yj; return ;
}
if(F[jd]) Down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, yj);
if(y > mid ) Sec_G(mid + , r, rson, x, y, yj);
W[jd] = W[lson] + W[rson];
}
void Sec_A(int l, int r, int jd, int x, int y) {
if(x <= l && r <= y) {
Answer += W[jd]; return ;
}
if(F[jd]) Down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid ) Sec_A(mid + , r, rson, x, y);
}
}Seg_tree; void Dfs_1(int u, int f_, int dep) {
fa[u] = f_; deep[u] = dep; size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != f_) {
Dfs_1(v, u, dep + );
size[u] += size[v];
if(size[son[u]] < size[v]) son[u] = v;
}
}
} void Dfs_2(int u, int tp) {
top[u] = tp;
tree[u] = ++ tim;
lst[u] = tim;
if(!son[u]) {rst[u] = tim; return ;}
Dfs_2(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa[u] && v != son[u]) Dfs_2(v, v);
}
rst[u] = tim;
} inline void Sec_G_imp(int x, int y, int w) {
int tp1 = top[x], tp2 = top[y];
while(tp1 != tp2) {
if(deep[tp1] < deep[tp2]) swap(tp1, tp2), swap(x, y);
Seg_tree.Sec_G(, n, , tree[tp1], tree[x], w);
x = fa[tp1];
tp1 = fa[x];
}
if(deep[x] < deep[y]) swap(x, y);
Seg_tree.Sec_G(, n, , tree[y], tree[x], w);
return ;
} int main() {
n = read();
for(int i = ; i < n; i ++) son[i] = -, head[i] = -;
for(int i = ; i < n; i ++) {int u = read(), v = read(); Add(u, v);}
Seg_tree.Build_tree(, n, );
Dfs_1(, , );
Dfs_2(, );
T = read();
while(T --) {
string opt; cin >> opt;
if(opt[] == 'A') {
int x = read(), y = read(), w = read();
Sec_G_imp(x, y, w);
} else {
Answer = ;
int x = read();
Seg_tree.Sec_A(, n, , lst[x], rst[x]);
cout << Answer << "\n";
}
} return ;
}

[Luogu] 魔法树的更多相关文章

  1. noip模拟赛(一)魔法树

    魔法树 (mahou.pas/c/cpp) [问题描述] 魔法使moreD在研究一棵魔法树. 魔法树顾名思义,这货是一棵树,奇葩的是魔法树上的每一条边都拥有一个魔法属性,如果不那么奇葩就不是moreD ...

  2. [Luogu] 魔板

    https://www.luogu.org/problemnew/show/P1275 #include <iostream> #include <cstdio> #inclu ...

  3. [bzoj2836] 魔法树

    俩操作:增加路径上的点的权值.查询子树的权值和. 想了想似乎只能树链剖分了..好久没写链剖+数据结构了TAT 一开始没开LL炸了一发(明明有想到的..我果然是傻逼= = #include<cst ...

  4. P3833 [SHOI2012]魔法树

    思路 树剖板子 注意给出点的编号是从零开始的 代码 #include <cstdio> #include <algorithm> #include <cstring> ...

  5. [洛谷P3833][SHOI2012]魔法树

    题目大意:给一棵树,路径加,子树求和 题解:树剖 卡点:无 C++ Code: #include <cstdio> #include <iostream> #define ma ...

  6. 【树链剖分】【dfs序】【线段树】bzoj2836 魔法树

    这道题告诉我们:树链剖分的重标号就是dfs序. #include<cstdio> #include<algorithm> using namespace std; #defin ...

  7. 树链剖分【洛谷P3833】 [SHOI2012]魔法树

    P3833 [SHOI2012]魔法树 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节 ...

  8. 树链剖分【P3833】 [SHOI2012]魔法树

    Description Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点,每个节点u的 ...

  9. [SHOI 2012] 魔法树

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2836 [算法] 树链剖分 时间复杂度 : O(NlogN ^ 2) [代码] #in ...

随机推荐

  1. 关于RESTful API

    添几篇文章: 一.What Is REST? 二.RESTful API最佳实践 三.MS Azure——API Design 这些文章里面也推荐了一些关联文章,微软那篇最详细,非常值得一读.

  2. ubuntu中安装字体雅黑和consolas

    Ubuntu的群体里偏向使用雅黑,我目前用的YaHei.Consolas 1.11 版本 (雅黑-Consolas的混合体) http://www.netmako.com/RobertLee/YaHe ...

  3. linux - 卸载python

    2019年10月15日12:05:42 [root@spider1 bin]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制 ...

  4. JS基础_对象的方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. git 操作实践

    git操作: - git是一个用于帮助用户实现版本控制的软件 #首先创建项目 1. cd到项目文件目录 2. 鼠标右键点击 Git Bash Here 3. git init #在项目文件目录生成 . ...

  6. python现状

    自从官方宣布 2020 年 1 月后不再更新维护 Python2,已经有一大批开源软件将其抛弃.今天,抛弃 Python2 的名单上又多了一个重磅软件.Python2 是 Python 官方在 200 ...

  7. Linux学习(二)-Xshell 5和Xftp 5的安装和使用

    (一)软件介绍: (1)Xshell: Xshell通过互联网可以连接到远程的服务器,然后通过模拟终端来实现对服务器的各种操作,而且这款软件可以很好的解决中文乱码问题,非常的方便快捷. (2)Xftp ...

  8. 第一章、VUE-挂载点-实例成员-数据-过滤器-文本指令-事件指令-属性指令-表单指令-01

    目录 路飞项目 vue vue 导读 vue 的优势 渐进式框架 引入 vue 实例成员 - 挂载点 el js 对象(字典)补充 实例成员 - 数据 data 实例成员 - 过滤器 filters ...

  9. Django ORM常用的字段和参数

    ORM 常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. IntegerField 一个整数类 ...

  10. vi和vim的使用

    本章内容: vi编辑器简介 vim基本使用 vim使用技巧 一.vim简介 vim是一个全屏幕纯文本编辑器,是vi编辑器的增强版. 二.vim的基本使用 1.vim的工作模式 命令模式:是主要使用快键 ...