树链剖分+离散+扫描(HDU5044)
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.
2
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
Case #1:
1 1 0 1
0 2 2
Case #2:
5 0 0 5
0 4 0题意:给出一棵树,树上的点和边的权值开始都是0,有两种操作,对于第一种操作,ADD1:在u到v的路径上每个点的权值+w;对于第二种操作ADD2:在u到v的路径上每个边的权值+w;最后询问每个点的权值和每条边的权值(按照输入的顺序)分析:首先两个dfs进行轻重链剖分,然后对于每个区间[L,R],在L的位置+w,在R+1的位置-w,保存在g1数组中,同理,对于边也一样,保存在g2数组里,最后从前往后做一遍扫描即可;程序:#pragma comment(linker, "/STACK:1024000000,1024000000")
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 110009
#define eps 1e-5
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int v;
node(int vv){v=vv;}
};
vector<node>edge[M];
int pos,son[M],fa[M],p[M],fp[M],deep[M],top[M],num[M],g1[M],g2[M],ans[M];
void dfs(int u,int f,int d)
{
deep[u]=d;
fa[u]=f;
num[1]=1;
for(int i=0;i<(int)edge[u].size();i++)
{
int v=edge[u][i].v;
if(v==f)continue;
dfs(v,u,d+1);
num[u]+=num[v];
if(son[u]==-1||num[son[u]]<son[v])
son[u]=v;
}
}
void getpos(int u,int sp)
{
top[u]=sp;
p[u]=pos++;
fp[p[u]]=u;
if(son[u]==-1)return;
getpos(son[u],sp);
for(int i=0;i<(int)edge[u].size();i++)
{
int v=edge[u][i].v;
if(v==son[u]||v==fa[u])continue;
getpos(v,v);
}
}
void init()
{
pos=0;
memset(son,-1,sizeof(son));
dfs(1,1,1);
getpos(1,1);
}
void getnode(int u,int v,int d)
{
int f1=top[u];
int f2=top[v];
while(f1!=f2)
{
if(deep[f1]<deep[f2])
{
swap(f1,f2);
swap(u,v);
}
g1[p[f1]]+=d;
g1[p[u]+1]-=d;
u=fa[f1];
f1=top[u];
}
if(u==v)
{
g1[p[u]]+=d;
g1[p[u]+1]-=d;
return;
}
if(deep[u]>deep[v])swap(u,v);
g1[p[u]]+=d;
g1[p[v]+1]-=d;
return;
}
void getedge(int u,int v,int d)
{
int f1=top[u];
int f2=top[v];
while(f1!=f2)
{
if(deep[f1]<deep[f2])
{
swap(f1,f2);
swap(u,v);
}
g2[p[f1]]+=d;
g2[p[u]+1]-=d;
u=fa[f1];
f1=top[u];
}
if(u==v)
return;
if(deep[u]>deep[v])swap(u,v);
g2[p[son[u]]]+=d;
g2[p[v]+1]-=d;
return;
}
struct lede
{
int u,v;
}e[M];
int main()
{
int T,m,n,i,u,v,w,kk=1;
char ch[22];
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
edge[i].clear();
for(i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
e[i].u=u;
e[i].v=v;
}
init();
memset(g1,0,sizeof(g1));
memset(g2,0,sizeof(g2));
while(m--)
{
scanf("%s%d%d%d",ch,&u,&v,&w);
if(strcmp(ch,"ADD1")==0)
getnode(u,v,w);
else
getedge(u,v,w);
}
printf("Case #%d:\n",kk++);
int sum=0;
for(i=0;i<pos;i++)
{
sum+=g1[i];
ans[i]=sum;
}
for(i=1;i<=n;i++)
{
if(i==1)
printf("%d",ans[p[i]]);
else
printf(" %d",ans[p[i]]);
}
printf("\n");
sum=0;
for(i=1;i<pos;i++)
{
sum+=g2[i];
ans[i]=sum;
}
for(i=1;i<n;i++)
{
if(deep[e[i].u]<deep[e[i].v])
swap(e[i].u,e[i].v);
if(i==1)
printf("%d",ans[p[e[i].u]]);
else
printf(" %d",ans[p[e[i].u]]);
}
printf("\n");
}
}
树链剖分+离散+扫描(HDU5044)的更多相关文章
- HDU5029--Relief grain (树链剖分+线段树 )
题意:n个点构成的无根树,m次操作, 对于操作 x y z, 表示 x 到 y 路径上的 每个点 加一个 z 数字,可重复加.最后输出每个点 加的次数最多的那个数字,如果没有输出0. 赤裸裸的树链剖分 ...
- HDU 4366 Successor(树链剖分+zkw线段树+扫描线)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...
- HDU - 3966 Aragorn's Story(树链剖分入门+线段树)
HDU - 3966 Aragorn's Story Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- LightOJ 1348 (树链剖分 + 线段树(树状数组))
题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- codevs 1228 苹果树 树链剖分讲解
题目:codevs 1228 苹果树 链接:http://codevs.cn/problem/1228/ 看了这么多树链剖分的解释,几个小时后总算把树链剖分弄懂了. 树链剖分的功能:快速修改,查询树上 ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
随机推荐
- R语言--saprkR基本使用
1.在sparkR的shell中交互式使用 sparkR --masterspark://10.130.2.20:7077 sparkR --masterlocal[6] #sparkR --mast ...
- am335x LCD背光问题
/**************************************************************** * am335x backlight problem * * 本问记 ...
- JavaScript 学习笔记(三)
本章学习内容: 1.数组的使用 2.类和对象细节. 3.this关键字的使用 4.构造函数,成员函数的使用 1.数组的使用 在任何的语言中,必须要有的就是数组了,有了数组,使得很多操作都变得非常的 ...
- PHP 去除iphone,ios,emoji表情
public static function removeEmoji($text) { $clean_text = ""; // Match Emoticons $regexEmo ...
- mysql命令收集
1.显示当前用户的权限
- php判断来访者是否是搜索引擎的蜘蛛
我们可以通过HTTP_USER_AGENT来判断是否是蜘蛛,搜索引擎的蜘蛛都有自己的独特标志,下面列取了一部分. function is_crawler() { $userAgent = strtol ...
- iftop、ifstat详解
ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB/s in KB/s out KB/s in KB/s out 0 ...
- 谈谈django里的Contex和RequestContext---向模板里添加全局变量
一直很想仔细研究一下,我在django模板里,可以直接访问变量user, request之类的变量,哪里来的,到底都有哪些?这会儿周五,我有空来仔细看看代码. 模拟一下需求: 我们做一个在线商城,需要 ...
- 彩色图像的直方图均衡化matlab代码
彩色图像的直方图均衡化 - YangYudong2014的专栏 - CSDN博客 http://blog.csdn.net/yangyudong2014/article/details/4051503 ...
- jquery插件大全
241个jquery插件—jquery插件大全 免费JQuery插件 推荐12款非常有用的流行 jQuery 插件