[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 ...
随机推荐
- mysql innodb数据库损坏导致无法启动
生产环境中的mysql突然启动不了,查了原因是innodb库错误,以前就遇到过这个问题,稀里糊涂的没解决,结果导致大量数据丢失.这些又遇到这个问题,果断把那个有问题的数据库移动了别的地方,启动了mys ...
- Zynq-7000 MiZ701 SOC硬件使用手册
一.整体概述 4 二.应用领域及人群 4 三.硬件配置 4 BANK资源分配 6 四.MiZ701开发板功能描述 7 4.1 全编程SOC(All Programmable SoC) 7 4.2 内存 ...
- k8s-traefik默认80端口
vim traefik.yaml kind: Deployment apiVersion: extensions/v1beta1 metadata: name: traefik-ingress-con ...
- win10下面opencv安装
记得以前是安装好的,但是用了conda更新所有包以后,cv2不好用了,试验了很多方法都不管用,最后只能卸载opencv然后重新安装了. 如果电脑上安装了很多版本的python,比如我就安装了pytho ...
- 微信小程序转发事件
和生命周期是同级,在.js文件里面设置 // 分享按钮 onShareAppMessage: function () { return { title: '前端伪大叔', path: "/p ...
- el表达式获取url中携带的参数
使用JSTL时,URL会被隐含的对象param包裹起来,使用param.变量名,直接获取值 <body>hello:${param.name}</body> 在使用jquery ...
- vue报类似警告Computed property "isLoading" was assigned to but it has no setter
一.原因:一个计算属性,当计算传入的是一个函数,或者传入的是一个对象,而没有设置 setter,也就是 set 属性,当你尝试直接该改变这个这个计算属性的值,都会报这个警告,vuex还会出现通过com ...
- PHP 求两个日期之间相差的天数、月数
<?php /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day ...
- Delphi 数组特性
- redis写入性能测试
import timeit import redis def clock(func): def clocked(*args, **kwargs): t0 = timeit.default_timer( ...