HDU 5044 Tree 树链剖分+区间标记
Tree
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
The first line of each case contains two integers N ,M (1 ≤ N, M ≤105),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -105 ≤ k ≤ 105)
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
4 2
1 2
2 3
2 4
ADD1 1 4 1
ADD2 3 4 2
4 2
1 2
2 3
1 4
ADD1 1 4 5
ADD2 3 2 4
1 1 0 1
0 2 2
Case #2:
5 0 0 5
0 4 0
题意:
给你一棵树,n个结点
1、将u到v之间的结点权值加k;
2、将u到v之间的边权加k。
输出经过修改后所有的边权和点权。
题解:
这题线段树超时
需要用到一个区间更新小技巧
比如x,y这个区间+k,那么sum[x] += k, sum[y+1] -= k;
最后统计一个前缀和就可以了
这题读入挂超时,不用能A,格式也要注意了
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e3+, mod = 1e9+, inf = 2e9; int dep[N],head[N],t=,sz[N],fa[N],indexS,top[N],pos[N],son[N],idpos[N];
struct ss{int to,next,id;}e[N*];
int n;
void add(int u,int v,int id)
{e[t].to = v;e[t].next = head[u];e[t].id=id;head[u] = t++;}
void dfs(int u) {
int k = ;
sz[u] = ;
dep[u] = dep[fa[u]] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa[u]) continue;
fa[to] = u;
idpos[to] = e[i].id;
dfs(to);
sz[u] += sz[to];
if(sz[to] > sz[k]) k = to;
}
if(k) son[u] = k;
}
void dfs(int u,int chain) {
int k = ;
pos[u] = ++indexS;
top[u] = chain;
if(son[u] > )
dfs(son[u],chain);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(dep[to] > dep[u] && son[u] != to)
dfs(to,to);
}
}
LL sum[N],sum2[N];
void updateL(int x,int y,int k) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
sum2[pos[top[x]]] +=k;
sum2[pos[x]+] -=k;
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x,y);
sum2[pos[y]] +=k;
sum2[pos[x]+] -=k;
}
void updateW(int x,int y,int k) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
sum[pos[top[x]]] += k;
sum[pos[x] + ] -= k;
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
sum[pos[x] + ] += k;
sum[pos[y] + ] -= k;
}
void init() {
t = ;
memset(head,,sizeof(head));
indexS = ;fa[] = ;
for(int i = ; i <= n+; ++i) sum[i] = , sum2[i] = ;
for(int i = ; i <= n; ++i) son[i] = ,sz[i] = ;
}
int T,m,cas = ;
LL f[N];
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
init();
for(int i = ; i < n; ++i) {
int x,y;
scanf("%d%d",&x,&y);
add(x,y,i),add(y,x,i);
}
dfs();
dfs(,);
while(m--) {
char ch[];
int x,y,k;
scanf("%s",ch);
scanf("%d%d%d",&x,&y,&k);
if(ch[] == '') {
updateL(x,y,k);
}
else if(x!=y){
updateW(x,y,k);
}
}
printf("Case #%d:\n",cas++);
for(int i = ; i <= n; ++i)
sum[i]+=sum[i-],sum2[i]+=sum2[i-];
for(int i = ; i <= n; ++i) f[idpos[i]] = sum[pos[i]];
for(int i = ; i < n; ++i)
printf("%I64d ",sum2[pos[i]]);
printf("%I64d\n",sum2[pos[n]]);
for(int i = ; i < n-; ++i) printf("%I64d ",f[i]);
if(n- > )printf("%I64d",f[n-]);
puts("");
}
return ;
}
HDU 5044 Tree 树链剖分+区间标记的更多相关文章
- HDU 5044 Tree --树链剖分
题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...
- HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...
- 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-> ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值
Relief grain Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12247 Accepted: 3151 Descriptio ...
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...
- HDU 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
随机推荐
- 使用Docker compose编排Laravel应用
前言 Laravel官方开发环境推荐的是Homestead(其实就是一个封装好的Vagrant box),我感觉这个比较重,于是自己用Docker compose编排了一套开发环境,在这里分享下. 环 ...
- js实现复制粘贴功能
在项目中使用到复制粘贴功能,虽然网上有很多大牛封装了很多的插件,但是还是想不去使用插件,就像自己来实现这个功能. 另一篇是禁止复制粘贴 前端er怎样操作剪切复制以及禁止复制+破解等 初步想法: 1. ...
- 洛谷 1196 [NOI2002]银河英雄传说【模板】带权并查集
[题解] 经典的带权并查集题目. 设cnt[i]表示i前面的点的数量,siz[i]表示第i个点(这个点是代表元)所处的联通块的大小:合并的时候更新siz.旧的代表元的cnt,路径压缩的时候维护cnt即 ...
- 【01】Firebug 教程
Firebug 教程 什么是 Firebug? Firebug 是一个开源的web开发工具. 现在浏览器自带firebug了. 安装 Firebug Firebug下载地址: https: ...
- 如何取SQL结果集的第一条记录
在SQL Server数据库中,使用top关键字: SELECT TOP number|percent column_name(s) FROM table_name 在MySQL数据库中,使用LIMI ...
- 大数据学习——ip改成固定ip
vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改BOOTPROTO为static 添加IPADDR=192.168.74.100 添加NETMASK=25 ...
- CentOS7 设置代理
大多数公司的网络都使用局域网加代理上网,也就是说上外网必须使用公司指定的代理服务器,这有几个好处: 1. 首先代理可以一定程度提高浏览速度,因为可以将更多的网页缓存在代理服务器上,需要的时候直接拿就很 ...
- XTUOJ 15503 - C
15503 - C Accepted: 6 Submissions: 27 Time Limit: 3000 ms Memory Limit: 1048576 KB 在解决了小女孩的 ...
- UVA674-Coin Change,用动归思想来递推!
674 - Coin Change 题意:有1分,5分,10分,25分,50分共5种硬币,数量不限.给你一个n求有多少种方法凑齐n,注意:d[0]=1; 思路:推了前几组样例,可以发现直接用当前状态累 ...
- 【python可视化系列】python数据可视化利器--pyecharts
学可视化就跟学弹吉他一样,刚开始你会觉得自己弹出来的是噪音,也就有了在使用python可视化的时候,总说,我擦,为啥别人画的图那么溜: [python可视化系列]python数据可视化利器--pyec ...