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. javaSE 笔记一

    java 环境变量配置 步骤:   右键[计算机]图标 –>[属性]–>[高级系统设置]–>[环境变量]   在"系统变量"里找到"Path" ...

  2. Simple Library Management System HDU - 1497(图书管理系统)

    Problem Description After AC all the hardest problems in the world , the ACboy 8006 now has nothing ...

  3. # 双值Hash

    双值Hash 简单介绍 Hash的应用:Hash其实就像一个加密过程,很多加密算法都会用到Hash,像GitHub中生成的token值也是Hash的结果. Hash冲突:简单来说就是不同的数映射到了同 ...

  4. php.ini中allow_url_fopen和allow_url_include的设置

    all_url_include在php 5.2以后添加,安全方便的设置(php的默认设置)为:allow_url_fopen=on;all_url_include=off;allow_url_fope ...

  5. Javascript中new的作用

    关于js中new关键字的理解,先来看个例子:像这样创建实例时使用new与不使用new有什么区别????function ParasiticPerson(name, age, job) {    var ...

  6. vue 导航守卫

    1.全局守卫(在所有路由展示前触发)//在main.js中 router.beforeEach((to, from, next) => {      to 即将要进入的到的路由,值为路由    ...

  7. AOP底层实现原理,动态代理如何动态

    代理 指定另外一个主体代替原来的某个主体去执行某个事物 代理执行的人 需要代理的人 需要代理的事情是一定要做的 但是被代理的人没有时间或自己做的不专业 静态代理: 父母朋友帮忙物色找对象 代理人掌握需 ...

  8. liunx pyinotify的安装和使用

    介绍此功能是检测目录的操作的事件 1.安装 在百度云盘下载或者在gits上下载安装包 链接:https://pan.baidu.com/s/1Lqt872YEgEo_bNPEnEJMaw 提取码:bj ...

  9. ORACLE数据库 自动备份 定时计划任务 windows

    疑问为什么没有输入oracle 的数据库安装目录就能直接备份呢,可能是因为oracle默认安装c盘,在docs命令直接能操作吧,不信可以使用sqlplus试试. 一共分三步: 一.建立一个.bat 批 ...

  10. 8.SpringMVC注解式开发-HelloWorld

    第一个注解式开发程序 SpringMVC 是 Spring 框架的一部分,所以它和Spring结合的是非常紧密的 使用 @Controller 注解,既可以将处理器交给Spring容器去管理,又可以说 ...