题目

直通车

很显然这是个树刨的板子,树上链查询和子树查询

注意:

1.这个点的树根为 0 而不是 1 所以注意读图时点标号 +1 就解决了

2.注意数据范围\(2^{32}\)

然后板子就能过了

node

#include <iostream>
#include <cstdio>
#define N 100005
#define int long long
#define lson rt << 1
#define rson rt << 1|1 using namespace std;
int n,m,r;
int pre[N],dep[N],fa[N],son[N],siz[N],top[N],dfn[N],dfn2[N],cnt; namespace Seg{
struct Tree{
int sum,len,lazy;
}tree[N << 1];
void push_up(int rt){
tree[rt].sum = tree[lson].sum + tree[rson].sum;
}
void build(int rt,int l,int r){
tree[rt].len = r - l + 1;
if(l == r){
tree[rt].sum = 0;
return;
}
int mid = (l + r)>>1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
}
void pushdown(int rt) {
if (tree[rt].lazy){
tree[lson].sum += tree[rt].lazy * tree[lson].len;
tree[rson].sum += tree[rt].lazy * tree[rson].len;
tree[lson].lazy += tree[rt].lazy;
tree[rson].lazy += tree[rt].lazy;
tree[rt].lazy = 0;
}
}
void update(int rt,int k,int l,int r,int L,int R){
if(l >= L && r <= R){
tree[rt].sum += k * tree[rt].len;
tree[rt].lazy += k;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if(mid >= L) update(lson, k, l, mid, L, R);
if(mid < R) update(rson, k, mid + 1, r, L, R);
push_up(rt);
}
int query(int rt,int l,int r,int L,int R){
if(l >= L&& r <= R) return tree[rt].sum;
pushdown(rt);
int mid = (l + r)>>1,ans = 0;
if(L <= mid) ans += query(lson,l,mid,L,R);
if(mid < R) ans += query(rson,mid + 1,r,L,R);
return ans;
}
}
namespace Cut {
struct edge{
int v,nxt;
}e[N << 1];
int head[N],js;
void add_edge(int u,int v){
e[++js].v = v;e[js].nxt = head[u];head[u] = js;
}
void dfs(int x,int f,int d){
dep[x] = d,fa[x] = f,siz[x] = 1;
for(int i = head[x]; i;i = e[i].nxt){
int v = e[i].v;
if(v != fa[x]){
dfs(v, x, d + 1);
siz[x] += siz[v];
if(!son[x]||siz[son[x]] < siz[v])
son[x] = v;
}
}
}
void dfs2(int x,int tp){
dfn[x] =++cnt,pre[cnt] = x,top[x] = tp;
if(son[x]) dfs2(son[x],tp);
for(int i = head[x]; i; i = e[i].nxt){
int v = e[i].v;
if(v != fa[x]&&v != son[x]) dfs2(v,v);
}
}
int query_tree(int x){
return Seg::query(1, 1, n, dfn[x],dfn[x] + siz[x] - 1);
}
void change_sum(int x,int y,int k){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
Seg::update(1, k, 1, n,dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
Seg::update(1, k, 1, n,dfn[x], dfn[y]);
}
}
signed main() {
int Q;
cin>>n;
for (int i = 1, x, y; i < n; i++) {
cin>>x>>y;
Cut::add_edge(x + 1, y + 1);
Cut::add_edge(y + 1, x + 1);
}
Cut::dfs(1, 0, 1);
Cut::dfs2(1, 1);
Seg::build(1, 1, n);
cin >> Q;
while(Q--){
int x,y,z;
char opt;
cin >> opt;
if (opt == 'A') cin>>x>>y>>z,Cut::change_sum(x + 1, y + 1, z);
if (opt == 'Q') cin>>x,printf("%lld\n", Cut::query_tree(x + 1));
}
}

题解 P3833 【[SHOI2012]魔法树】的更多相关文章

  1. 洛谷——P3833 [SHOI2012]魔法树

    P3833 [SHOI2012]魔法树 题目背景 SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的 ...

  2. 洛谷 P3833 [SHOI2012]魔法树

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

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

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

  4. P3833 [SHOI2012]魔法树

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

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

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

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

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

  7. [SHOI2012]魔法树

    题目:洛谷P3833. 题目大意:给你一棵树,有两种操作:1.给两个点和它们之间的最短路上的所有点加上一个值:2.询问以某个点为根的子树的子树和.你需要实现这个功能. 解题思路:如果只有最后才询问的话 ...

  8. 洛谷3833 [SHOI2012]魔法树

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

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

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

随机推荐

  1. springboot项目配置数据库

    在pom.xml文件中配置 <!-- mybatis整合springboot起步依赖--> <dependency> <groupId>org.mybatis.sp ...

  2. 扫盲:Kotlin 的泛型

    引子 相信总是有很多同学,总是在抱怨泛型无论怎么学习,都只是停留在一个简单使用的水平,所以一直为此而备受苦恼. Kotlin 作为一门能和 Java 相互调用的语言,自然也支持泛型,不过 Kotlin ...

  3. Docker安装系列教程

    首先准备一台Centos7版本的虚拟机,它支持docker容器技术.本案例使用centos7虚拟机安装docker容器. 一.安装 1.启动虚拟机,配置虚拟机能够访问互联网 2. 安装支持软件包,提供 ...

  4. 网易163 docker镜像

    $ sudo echo "DOCKER_OPTS=\"--registry-mirror=http://hub-mirror.c.163.com\"" > ...

  5. Thread中run和start方法的模板设计模式

    创建一个Thread需要继承Thread重写run方法或者实现Runnable接口中的run方法,其实两者都是一样因为Thread也继承了Runnable接口. 实现了run方法,但是启动确实用sta ...

  6. SpringMVC的@Validated校验注解使用方法

    validate会对参数进行校验,校验标准为validate后的类中的标准.本例中对User进行校验,User类中设置了校验标准. 在后台开发过程中,对参数的校验成为开发环境不可缺少的一个环节.比如参 ...

  7. shell脚本学习之6小时搞定(6)-重定向及其他

    shell学习之-重定向及其他 目录 shell学习之-重定向及其他 1.输出重定向 2.输入重定向 3.重定向深入讲解 4./dev/null 文件 5.awk Unix 命令默认从标准输入设备(s ...

  8. docker frps 内网穿透容器化服务

    准备 域名解析 将frp.xx.com解析到服务器ip,将泛域名 *.frp.xx.com解析到frp.xx.com即可 https证书申请 泛域名证书现在可以用acme.sh申请Let's Encr ...

  9. Gradle最佳实践

    一.Gradle相比Maven的优势 配置简洁 Maven是用pom.xml管理,引入一个jar包至少5行代码,Gradle只需要一行. 构建速度快 Gradle支持daemon方式运行,启动速度快, ...

  10. 解决Cannot find module '@angular/compiler-cli'

    前言: 今天clone之前做的一个angular项目,使用ng serve一直提示An unhandled exception occurred: Cannot find module '@angul ...