Relief grain

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5029

Description

The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.

We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.

There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.

Input

The input consists of at most 25 test cases.

For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.

The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village. 
   
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)

The input ends by n = 0 and m = 0.

Output

For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.

Sample Input

2 4
1 2
1 1 1
1 2 2
2 2 2
2 2 1
5 3
1 2
3 1
3 4
5 3
2 3 3
1 5 2
3 3 3
0 0

Sample Output

1
2
2
3
3
0
2

HINT

题意

有n个点的树,有m次操作

操作是在x-y的链上的每一个点,都增加一个W

然后让你输出每个点出现次数最多的数是什么

题解:

看到链状,很显然就是树链剖分

我们对于每个询问都按照lca去打上标记,就G[u].push_back(v,1),G[v].push_back(v,-1)这种

打完标记之后,我们再按照树链剖分的顺序去处理这些标记就好了

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <vector>
using namespace std;
const int N=;
const int INF=<<; int n,tim;
vector<pair<int,int> >G[];
long long num[N];
int siz[N],top[N],son[N];
int dep[N],tid[N],Rank[N],fa[N];
int head[N],to[*N],Next[*N],edge;
int flag = ;
struct Edge
{
int u,v;
};
Edge tmp[*N];
int ans[N];
void Init()
{
memset(head,-,sizeof(head));
memset(son,-,sizeof(son));
tim=;
edge=;
for(int i=;i<;i++)
G[i].clear();
memset(ans,,sizeof(ans));
} void addedge(int u,int v)
{
to[edge]=v,Next[edge]=head[u],head[u]=edge++;
to[edge]=u,Next[edge]=head[v],head[v]=edge++;
} //树链剖分部分
void dfs1(int u,int father,int d)
{
dep[u]=d;
fa[u]=father;
siz[u]=;
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=father)
{
dfs1(v,u,d+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
}
} void dfs2(int u,int tp)
{
top[u]=tp;
tid[u]=++tim;
Rank[tid[u]]=u;
if(son[u]==-) return;
dfs2(son[u],tp);
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=son[u]&&v!=fa[u])
dfs2(v,v);
}
} void Do(int l,int r,int x)
{
//cout<<l<<" "<<r<<endl;
G[l].push_back(make_pair(x,));
G[r].push_back(make_pair(x,-));
} void solve(int x,int y,int val)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Do(top[x],x,val);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
Do(x,y,val);
}
/////////////////////////////////////////////////////////线段树 typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType Max1 , lazy ,Max2;
}; treenode tree[N]; inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].Max1 = ;
tree[o].Max2 = L;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR)
tree[o].Max1 += v;
else
{
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
if(tree[o*].Max1>=tree[o*+].Max1)
tree[o].Max1 = tree[o*].Max1,tree[o].Max2 = tree[o*].Max2;
else
tree[o].Max1 = tree[o*+].Max1,tree[o].Max2 = tree[o*+].Max2;
}
} ///////////////////////////////////////////////////
int MM;
void Get(int u,int tp)
{
//cout<<u<<endl;
for(int j=;j<G[u].size();j++)
{
if(G[u][j].second==-)continue;
//cout<<G[u][j].first<<" "<<G[u][j].second<<endl;
updata(G[u][j].first,G[u][j].first,G[u][j].second,);
}
//cout<<u<<" "<<tree[1].Max2<<" flag"<<endl;
ans[u]=tree[].Max2;
for(int j=;j<G[u].size();j++)
{
if(G[u][j].second==)continue;
//cout<<G[u][j].first<<" "<<G[u][j].second<<endl;
updata(G[u][j].first,G[u][j].first,G[u][j].second,);
}
if(son[u]==-) return;
Get(son[u],tp);
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=son[u]&&v!=fa[u])
Get(v,v);
}
}
int main()
{
while(scanf("%d%d",&n,&MM)!=EOF)
{
if(n==&&MM==)break;
Init();
for(int i=; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
}
dfs1(,,);
dfs2(,);
while(MM--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
solve(x,y,z);
}
build_tree(,,);
Get(,);
for(int i=;i<=n;i++)
printf("%d\n",ans[i]);
}
return ;
}

HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值的更多相关文章

  1. BZOJ4012[HNOI2015]开店——树链剖分+可持久化线段树/动态点分治+vector

    题目描述 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现她们面临着一个 ...

  2. BZOJ 2243: [SDOI2011]染色 树链剖分 倍增lca 线段树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  3. HDU 3966 Aragorn's Story(模板题)【树链剖分】+【线段树】

    <题目链接> 题目大意: 给定一颗带点权的树,进行两种操作,一是给定树上一段路径,对其上每个点的点权增加或者减少一个数,二是对某个编号点的点权进行查询. 解题分析: 树链剖分的模板题,还不 ...

  4. 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)

    题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...

  5. LOJ2269 [SDOI2017] 切树游戏 【FWT】【动态DP】【树链剖分】【线段树】

    题目分析: 好题.本来是一道好的非套路题,但是不凑巧的是当年有一位国家集训队员正好介绍了这个算法. 首先考虑静态的情况.这个的DP方程非常容易写出来. 接着可以注意到对于异或结果的计数可以看成一个FW ...

  6. HYSBZ 4034 【树链剖分】+【线段树 】

    <题目链接> 题目大意: 有一棵点数为 N 的树,以点 1 为根,且树点有权值.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x ...

  7. POJ 2763 Housewife Wind 【树链剖分】+【线段树】

    <题目链接> 题目大意: 给定一棵无向树,这棵树的有边权,这棵树的边的序号完全由输入边的序号决定.给你一个人的起点,进行两次操作: 一:该人从起点走到指定点,问你这段路径的边权总和是多少. ...

  8. 焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】

    You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she wil ...

  9. 【树链剖分】【线段树】bzoj3626 [LNOI2014]LCA

    引用题解: http://blog.csdn.net/popoqqq/article/details/38823457 题目大意: 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深 ...

随机推荐

  1. Spring学习之基本概念

    Spring 基本概念 Spring优点: 1.Spring不同于其它的Framework,它要提供的是一种管理你的业务对象的方法. 2.DI有效的降低了耦合度 3.AOP提供了通用任务的集中管理 4 ...

  2. 【大数阶乘】NYOJ-28

    大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?   输入 输入一个整数 ...

  3. iOS 获取已连接的wifi信息

    转:http://blog.csdn.net/marujunyy/article/details/16843173 首先需要   #import <SystemConfiguration/Cap ...

  4. js除法余数

    return (Math.round(rs*100)/100); //保保留小数点后两位数: //如果要保留三位则改为:Math.round(rs*1000)/1000; //如果要保留四位则改为:M ...

  5. web自动化框架之一介绍与环境搭建(Selenium+Eclipse+Python)

    看到一篇环境搭建文章,详细又全面,这里就不一一重复了 http://blog.csdn.net/dyllove98/article/details/9390649 其它: 1.框架介绍      整个 ...

  6. gcc编译器与基本类型3

    C语言发展史 1969年贝尔实验室 肯尼斯·蓝·汤普逊,丹尼斯·李奇开发了B语言 ->Unix,New B语言,改名C语言83年提出C语言标准 1989年十二月正式通过C语言标准,C89标准 C ...

  7. 机器学习真的可以起作用吗?(3)(以二维PLA为例)

    前两篇文章已经完成了大部分的工作,这篇文章主要是讲VC bound和 VC dimension这两个概念. (一)前文的一点补充 根据前面的讨论,我们似乎只需要用来替代来源的M就可以了,但是实际公式却 ...

  8. Java相关书籍推荐

    Java从入门到精通(第3版 附光盘) 作      者 明日科技 编 出 版 社 清华大学出版社 出版时间 2012-08-01 版      次 3 页      数 564 印刷时间 2012- ...

  9. Kindle Paperwhite 2使用体验

    博客开通后一懒就扔下了几十天,着实自惭.鉴于是第一篇,先说点题外话. 一转眼读研的生活已经过去一年有余.曾经的同学已经在职场拼搏,同龄人的生活状态也自然地带给自己一份紧迫感:不敢再贪恋校园生活的安逸, ...

  10. matlab的&和&&操作

    A&B(1)首先判断A的逻辑值,然后判断B的值,然后进行逻辑与的计算.(2)A和B可以为矩阵(e.g. A=[1 0],B=[0 0]).A&&B(1)首先判断A的逻辑值,如果 ...