[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2836

[算法]

树链剖分

时间复杂度 : O(NlogN ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
typedef long long LL; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , tot , timer;
int head[MAXN] , size[MAXN] , son[MAXN] , fa[MAXN] , dfn[MAXN] , depth[MAXN] , top[MAXN]; template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
struct Segment_Tree
{
struct Node
{
int l , r;
LL sum , tag;
} Tree[MAXN << ];
inline void build(int index , int l , int r)
{
Tree[index].l = l;
Tree[index].r = r;
Tree[index].tag = Tree[index].sum = ;
if (l == r) return;
int mid = (l + r) >> ;
build(index << , l , mid);
build(index << | , mid + , r);
}
inline void pushdown(int index)
{
int l = Tree[index].l , r = Tree[index].r;
int mid = (l + r) >> ;
Tree[index << ].sum += (mid - l + ) * Tree[index].tag;
Tree[index << | ].sum += (r - mid) * Tree[index].tag;
Tree[index << ].tag += Tree[index].tag;
Tree[index << | ].tag += Tree[index].tag;
Tree[index].tag = ;
}
inline void update(int index)
{
Tree[index].sum = Tree[index << ].sum + Tree[index << | ].sum;
}
inline void modify(int index , int l , int r , LL value)
{
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].sum += value * (r - l + );
Tree[index].tag += value;
return;
}
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << , l , r , value);
else if (mid + <= l) modify(index << | , l , r , value);
else
{
modify(index << , l , mid , value);
modify(index << | , mid + , r , value);
}
update(index);
}
inline LL query(int index , int l , int r)
{
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].sum;
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query(index << , l , r);
else if (mid + <= l) return query(index << | , l , r);
else return query(index << , l , mid) + query(index << | , mid + , r);
}
} SGT;
inline void addedge(int u , int v)
{
++tot;
e[tot] = (edge){v , head[u]};
head[u] = tot;
}
inline void dfs1(int u)
{
son[u] = -;
size[u] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa[u]) continue;
fa[v] = u;
depth[v] = depth[u] + ;
dfs1(v);
size[u] += size[v];
if (son[u] == - || size[v] > size[son[u]]) son[u] = v;
}
}
inline void dfs2(int u , int tp)
{
dfn[u] = ++timer;
top[u] = tp;
if (son[u] != -) dfs2(son[u] , tp);
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa[u] || v == son[u]) continue;
dfs2(v , v);
}
}
inline void modify(int u , int v , LL d)
{
int tu = top[u] , tv = top[v];
while (tu != tv)
{
if (depth[tu] > depth[tv])
{
swap(u , v);
swap(tu , tv);
}
SGT.modify( , dfn[tv] , dfn[v] , d);
v = fa[tv]; tv = top[v];
}
if (depth[u] > depth[v]) swap(u , v);
SGT.modify(, dfn[u] , dfn[v] , d);
} int main()
{ scanf("%d" , &n);
for (int i = ; i < n; i++)
{
int u , v;
scanf("%d%d" , &u , &v);
addedge(u , v);
fa[v] = u;
}
dfs1();
dfs2( , );
SGT.build( , , n);
int q;
scanf("%d" , &q);
while (q--)
{
char op[];
scanf("%s" , &op);
if (op[] == 'A')
{
int u , v;
LL d;
scanf("%d%d%lld" , &u , &v , &d);
modify(u , v , d);
} else
{
int u;
scanf("%d" , &u);
printf("%lld\n" , SGT.query( , dfn[u] , dfn[u] + size[u] - ));
}
} return ;
}

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

  1. 洛谷 3833 SHOI 2012 魔法树

    [题解] 树链剖分模板题.. #include<cstdio> #include<algorithm> #include<queue> #define N 5000 ...

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

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

  3. shoi 魔法树

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

  4. [bzoj2836] 魔法树

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

  5. P3833 [SHOI2012]魔法树

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

  6. 解题:SHOI 2012 回家的路

    题面 完了,做的时候已经想不起来分层图这个东西了QAQ 对于这种“多种”路径加中转站的题,还有那种有若干次“特殊能力”的题,都可以考虑用分层图来做 显然只需要记录所有的中转站+起点终点,然后拆出横竖两 ...

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

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

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

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

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

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

随机推荐

  1. Spring整合SSM的配置文件详解

    在整合三大框架SSM , 即 Spring 和 SpingMVC和Mybatis的时候,搭建项目最初需要先配置好配置文件. 有人在刚开始学习框架的时候会纠结项目搭建的顺序,因为频繁的报错提示是会很影响 ...

  2. How to resolve 'Potential Leak' issue

    -1 down vote favorite I am using the 'analyze' tool in xcode to check for potential leakages in my a ...

  3. hdu 1679 The Unique MST (克鲁斯卡尔)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24152   Accepted: 8587 D ...

  4. BeagleBone Black Industrial系统更新设置一贴通

    前言 原创文章,转载引用务必注明链接.水平有限,欢迎指正. 本文使用markdown写成,为获得更好的阅读体验,推荐访问我的博客原文: http://www.omoikane.cn/2016/09/1 ...

  5. hadoop2.7.1 nutch2.3 二次开发windows环境

     Hadoop windows编译: 能够略过这一段,直接下载hadoo2.7.1 bin文件.我的资源里有终于生成的winutils.exe和一些native code,放在bin文件夹即可了 參 ...

  6. weex stream 方法封装

    1.封装 api.js // 配置API接口地址 const baseUrl = 'http://www.kuitao8.com/'; // 引入 弹窗组件 var modal = weex.requ ...

  7. 设计模式入门之原型模式Prototype

    //原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象 //简单来说,当进行面向接口编程时,假设须要复制这一接口对象时.因为不知道他的详细类型并且不能实例化一个接口 //这时就须要 ...

  8. async & await 的前世今生(Updated)----代码demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. 获取连接状态数的awk数组命令

    awk -n|more zhutianpeng@ztp-OptiPlex-:~/Icpp/server$ netstat -n|more 激活Internet连接 (w/o 服务器) Proto Re ...

  10. No Memory Alignment with GCC

    attribute method: #include <stdio.h> struct packed { char a; int b; } __attribute__((packed)); ...