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 ...
随机推荐
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- js过滤空格
点击查看效果 点击进入下载 <html> <head> <title> 过滤空格 </title> <SCRIPT LANGUAGE=" ...
- [React] Extracting Private React Components
we leverage private components to break our render function into more manageable pieces without leak ...
- shell脚本加密
如何保护自己编写的shell程序要保护自己编写的shell脚本程序,方法有很多,最简单的方法有两种:1.加密 2.设定过期时间,下面以shc工具为例说明: 一.下载安装shc工具shc是一个加密s ...
- Http请求的 HttpURLConnection 和 HttpClient
HTTP 请求方式: GET和POST的比较 请求包.png 例子.png 响应包.png 例子.png 请求头描述了客户端向服务器发送请求时使用的http协议类型,所使用的编码,以及发送内容的长度, ...
- Multi-touch.
TP(Multi-touch). 1.Win7有6个版本,分别如下. Windows 7 Starter (初级版) 不支 ...
- NPM与调试工具的使用
1)NPM 2)nodemon 自动监视文件的变化并重启应用 3)pm2 启动node,资源共享 4)node-inspector node调试工具 5)Chrome Developer Tools
- 【转】IOS缓存机制详解
人魔七七:http://www.cnblogs.com/qiqibo/ 为什么要有缓存 应用需要离线工作的主要原因就是改善应用所表现出的性能.将应用内容缓存起来就可以支持离线.我们可以用两种不同的缓存 ...
- python自动化执行脚本
---恢复内容开始--- 1 (1)首先在你的.py文件上加上一行代码注释: #!/usr/local/bin/python2.7 (2)终端下执行: crontab -e 进入后,输入i 进入可编辑 ...
- (转)WCF入门教程(一)简介
原文系列来自http://www.cnblogs.com/yank/p/3653160.html 1.WCF是什么? WCF( Windows Communication Foundation), 是 ...