HDU5044---Tree 树链剖分
大致题意:add1 u v u到v路径上所有点的权值加上k,add2 u 到v路径上所有边的权值加上k
最后输出所有点的权值,边的权值。。树链剖分预处理然后来个线性O(n)的操作。刚开始用线段树tle了.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+;
struct
{
int to,next;
} e[maxn<<];
int head[maxn],edge;
void add(int x,int y)
{
e[edge].to = y;
e[edge].next = head[x];
head[x] = edge++;
} int son[maxn],fa[maxn],siz[maxn],dep[maxn];
void dfs1(int root)
{
siz[root] = ;
son[root] = ;
for (int i = head[root]; i > ; i = e[i].next)
{
if (fa[root] != e[i].to)
{
dep[e[i].to] = dep[root] + ;
fa[e[i].to] = root;
dfs1(e[i].to);
if (siz[son[root]] < siz[e[i].to])
son[root] = e[i].to;
siz[root] += siz[e[i].to];
}
}
}
int top[maxn],pos[maxn],fp[maxn],tot;
void dfs2(int root,int f)
{
top[root] = f;
pos[root] = tot++;
fp[pos[root]] = root;
if (son[root]>)
dfs2(son[root],top[root]);
for (int i = head[root]; i > ; i = e[i].next)
if (fa[root] != e[i].to && e[i].to != son[root])
dfs2(e[i].to,e[i].to);
}
ll addv[][maxn<<];
int k;
void pre_update(int ua,int ub,int cho)
{
int f1 = top[ua];
int f2 = top[ub];
while (f1 != f2)
{
if (dep[f1] < dep[f2])
swap(f1,f2),swap(ua,ub);
addv[cho][pos[f1]] += k;
addv[cho][pos[ua]+] -= k;
ua = fa[f1];
f1 = top[ua];
}
if (dep[ua] > dep[ub])
swap(ua,ub);
if (cho == )
addv[cho][pos[ua]] += k,addv[cho][pos[ub]+] -= k;
if (cho == )
addv[cho][pos[son[ua]]] += k,addv[cho][pos[ub]+] -= k;
}
int n,m,d[maxn][],link[maxn];
void init()
{
scanf ("%d%d",&n,&m);
int root = ;
dep[root] = fa[root] = ;
edge = tot = ;
memset(head,,sizeof(head));
memset(siz,,sizeof(siz));
memset(addv,,sizeof(addv));
for (int i = ; i < n; i++)
{
int u,v;
scanf ("%d%d",&u,&v);
d[i][] = u;
d[i][] = v;
add(u,v),add(v,u);
}
dfs1(root);
dfs2(root,root);
for (int i = ; i < n; i++)
{
if (dep[d[i][]] < dep[d[i][]])
swap(d[i][],d[i][]);
link[d[i][]] = i;
}
}
ll ans1[maxn],ans2[maxn];
int main(void)
{
//freopen("in.txt","r",stdin);
int t;
int cas = ;
scanf ("%d",&t);
while (t--)
{
init();
for (int i = ; i < m; i++)
{
char op[];
int u,v;
scanf ("%s%d%d%d",op,&u,&v,&k);
pre_update(u,v,op[]-'');
}
for (int i = ; i <= n; i++)
{
addv[][i] += addv[][i-];
addv[][i] += addv[][i-];
ans1[fp[i]] = addv[][i];
ans2[link[fp[i]]] = addv[][i];
}
printf("Case #%d:\n",cas++);
printf("%I64d",ans1[]);
for (int i = ; i <= n; i++)
{
printf(" %I64d",ans1[i]);
}
printf("\n");
if (n>)
printf("%I64d",ans2[]);
for (int i = ; i < n; i++)
{
printf(" %I64d",ans2[i]);
}
printf("\n");
}
return ;
}
HDU5044---Tree 树链剖分的更多相关文章
- hdu5044 Tree 树链拆分,点细分,刚,非递归版本
hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
随机推荐
- O - 覆盖的面积 - hdu 1255(求面积)
分析:求一层的面积覆盖是非常简单的事情,不过多层面积覆盖应该怎么搞???也是比较简单的事情,只需要用两个变量记录就好了,一个记录覆盖一次的,一个记录覆盖两次的,就很容易解决了 ************ ...
- Power(int base, int exponent) 函数实现
这个是个高效的算法,时间复杂度为 O(logn) 原理: a的n次方: #include<iostream> #include<cmath> using namespace s ...
- RHEL修改主机名和IP
1, 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=NEWHOSTNAME #修改该值作为主机名,如:NEWPC ...
- iBatis查询结果部分为null的解决办法
今天第一天接触iBatis,没有系统学习过,遇到了一个简单却闹心的错误:用iBatis查询数据库中某个表的多列结果作为一个对象返回时,会出现对象的部分属性为null值得错误.例如,查询用户表中的用户I ...
- UITableView的刷新
UITableView的刷新1> 数据刷新的总体步骤* 修改模型数据* 刷新表格(刷新界面) 2> 刷新表格(刷新界面)的方法* 全局刷新(每一行都会重新刷新)- (void)reload ...
- 在Ubuntu下构建Bullet以及执行Bullet的样例程序
在Ubuntu下构建Bullet以及执行Bullet的样例程序 1.找到Bullet的下载页,地址是:https://code.google.com/p/bullet/downloads/list 2 ...
- [React Testing] Conditional className with Shallow Rendering
Often our components have output that shows differently depending on the props it is given; in this ...
- Android getResources的作用和须要注意点
今天做一个Android的文件管理器,里面用到非常多的地方用到了getResources. Drawable currentIcon = null; currentIcon = getResource ...
- i春秋30强挑战赛pwn解题过程
80pts: 栈溢出,gdb调试发现发送29控制eip,nx:disabled,所以布置好shellcode后getshell from pwn import * #p=process('./tc1' ...
- vmstat
vmstat(virtual memory statitics)命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况, ...