[Luogu] 魔法树
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] 魔法树的更多相关文章
- noip模拟赛(一)魔法树
魔法树 (mahou.pas/c/cpp) [问题描述] 魔法使moreD在研究一棵魔法树. 魔法树顾名思义,这货是一棵树,奇葩的是魔法树上的每一条边都拥有一个魔法属性,如果不那么奇葩就不是moreD ...
- [Luogu] 魔板
https://www.luogu.org/problemnew/show/P1275 #include <iostream> #include <cstdio> #inclu ...
- [bzoj2836] 魔法树
俩操作:增加路径上的点的权值.查询子树的权值和. 想了想似乎只能树链剖分了..好久没写链剖+数据结构了TAT 一开始没开LL炸了一发(明明有想到的..我果然是傻逼= = #include<cst ...
- P3833 [SHOI2012]魔法树
思路 树剖板子 注意给出点的编号是从零开始的 代码 #include <cstdio> #include <algorithm> #include <cstring> ...
- [洛谷P3833][SHOI2012]魔法树
题目大意:给一棵树,路径加,子树求和 题解:树剖 卡点:无 C++ Code: #include <cstdio> #include <iostream> #define ma ...
- 【树链剖分】【dfs序】【线段树】bzoj2836 魔法树
这道题告诉我们:树链剖分的重标号就是dfs序. #include<cstdio> #include<algorithm> using namespace std; #defin ...
- 树链剖分【洛谷P3833】 [SHOI2012]魔法树
P3833 [SHOI2012]魔法树 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节 ...
- 树链剖分【P3833】 [SHOI2012]魔法树
Description Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点,每个节点u的 ...
- [SHOI 2012] 魔法树
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2836 [算法] 树链剖分 时间复杂度 : O(NlogN ^ 2) [代码] #in ...
随机推荐
- 深度学习之卷积神经网络CNN及tensorflow代码实现示例
深度学习之卷积神经网络CNN及tensorflow代码实现示例 2017年05月01日 13:28:21 cxmscb 阅读数 151413更多 分类专栏: 机器学习 深度学习 机器学习 版权声明 ...
- MySQL SELECT语法(四)UNION语法详解
源自MySQL 5.7 官方手册:13.2.9.3 UNION Syntax 一.UNION语法 UNION用于将多个SELECT语句的结果合并到一个结果集中. SELECT ... UNION [A ...
- hdu 2066 Dijstra 堆优化
嗯 有广搜的意思 #include<cstdio> #include<iostream> #include<queue> #include<vector> ...
- vue阻止右键默认行为
vue阻止右键默认行为 <!--不阻止右键菜单(浏览器行为),右键执行函数show--> <input type="button" value="按钮& ...
- js摄像头
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- Node.js学习(2)-使用模板引擎art-template
node 安装cnpm i -S art-template 加载require('art-template') template.render接收的是字符串
- OpenCl入门——实现简单卷积
现在的卷积实现无非是那么几种:直接卷积.im2col+gemm.局部gemm.wingrod.FFT.如果直接卷积的话,其实kernel函数是比较好实现.以下代码参考至<OpenCL Progr ...
- 不显示Zetero导出的文献库中的部分内容
不显示Zetero导出的文献库中的部分内容 Zetero作为文献管理软件,收集到的参考文献的相关信息(域fields)比较齐全.文章或书籍的引用中仅仅只用到了其中的一部分,如作者.发表年.题名.期刊( ...
- 第三章 Lambda表达式
第三章 Lambda表达式 3.1 函数式编程思想概述 在数学中,函数就是有输入量.输出量的一套计算方案,也就是“拿什么东西做什么事情”.相对而言,面向对象过分强调“必须通过对象的形式来做事情”,而函 ...
- vs code 开发小程序会用到的插件
主要介绍一下几个vscode插件,在vscode中搜索插件关键字点击安装即可. 1) vscode weapp api, 语法结构api; 2) minapp-vscode 3) vscode wx ...