2014 ICPC---Relief grain(树链剖分)
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.
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.
For the first test case, the relief grain in the 1st village is {1, 2}, and the relief grain in the 2nd village is {1, 2, 2}.
方法就是打标记。线段树维护的是颜色。也就是维护的是[a,b]就是维护a颜色到b颜色某种颜色出现的最多次数。
假设我们处理的是序列而不是树吧。比如我们要把区间[x,y]图成a颜色.那么我们就在x出加个标记a。在y+1就标记-a。
多个标记用邻接表连起来就行了。然后从序列的最左端处理到最右端先把所有标记更新到线段树里。a则a颜色+1。
-a则在线段树将a颜色-1.然后再询问线段树里出现最多的次数就是序列该位置的次数最多的颜色了。相当于递推的思想吧。知道了x位置的颜色线段树.x+1位置的颜色线段树 无非是多了一些颜色或者少了某些颜色。多了减掉。少了的加上就是自己这个位置上的了。这样做之所以高效的原因是标记的是区间的端点而不是区间类的每一个元素。总的 时间复杂度m*log(n)*log(c)。m为询问数。n为结点数。c为颜色种数。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define N 100005
#define M 200005
using namespace std;
int n,q,cnt,sz;
int fa[N][],deep[N],size[N],head[N];
int pos[N],belong[N];
bool vis[N];
int mv[],leaf[],h[M]; struct data
{
int to,next;
} e[M],w[M*]; void insert(int u,int v)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
e[++cnt].to=u;
e[cnt].next=head[v];
head[v]=cnt;
} void init()
{
cnt=;
sz=;
memset(deep,,sizeof(deep));
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
memset(h,,sizeof(h));
memset(mv,,sizeof(mv));
for(int i=; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
insert(x,y);
}
} void dfs1(int x)
{
size[x]=;
vis[x]=;
for(int i=; i<=; i++)
{
if(deep[x]<(<<i))break;
fa[x][i]=fa[fa[x][i-]][i-];//倍增处理祖先信息
}
for(int i=head[x]; i; i=e[i].next)
{
if(vis[e[i].to])continue;
deep[e[i].to]=deep[x]+;
fa[e[i].to][]=x;
dfs1(e[i].to);
size[x]+=size[e[i].to];
}
} void dfs2(int x,int chain)
{
int k=;
sz++;
pos[x]=sz;//分配x结点在线段树中的编号
belong[x]=chain;
for(int i=head[x]; i; i=e[i].next)
if(deep[e[i].to]>deep[x]&&size[e[i].to]>size[k])
k=e[i].to;//选择子树最大的儿子继承重链
if(k==)return;
dfs2(k,chain);
for(int i=head[x]; i; i=e[i].next)
if(deep[e[i].to]>deep[x]&&k!=e[i].to)
dfs2(e[i].to,e[i].to);//其余儿子新开重链
} void build(int rt,int l,int r)//建线段树
{
if(l==r)
{
leaf[l]=rt;
return;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
} void update(int rt,int c)
{
if(c>) c=;
else c=-;
mv[rt]+=c;
while(rt>)
{
rt>>=;
mv[rt]=max(mv[rt<<],mv[rt<<|]);
}
} int qu(int L,int R,int rt)
{
int ls,rs,mid;
if(mv[rt]==)
return ;
while(L<R)
{
ls=rt<<,rs=ls|,mid=(L+R)>>;
if(mv[rs]>mv[ls])
L=mid+,rt=rs;
else
R=mid,rt=ls;
}
return L;
} void adde(int x,int d)
{
w[++cnt].to=d;
w[cnt].next=h[x];
h[x]=cnt;
} void uppath(int u,int v,int d)
{
int f1=belong[u],f2=belong[v];
while(f1!=f2)
{
if(deep[f1]<deep[f2])
swap(f1,f2),swap(u,v);
adde(pos[f1],d);
adde(pos[u]+,-d);
u=fa[f1][],f1=belong[u];
}
if(deep[u]>deep[v])
swap(u,v);
adde(pos[u],d);
adde(pos[v]+,-d);
} void solve()
{
build(,,);
cnt=;
for(int i=; i<=q; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
uppath(x,y,z);
}
int ans[N];
for(int i=;i<=n;i++)
{
for(int j=h[i];j;j=w[j].next)
{
update(leaf[abs(w[j].to)],w[j].to);
}
ans[i]=qu(,,);
}
for(int i=;i<=n;i++)
{
printf("%d\n",ans[pos[i]]);
}
} int main()
{
while(scanf("%d%d",&n,&q)&&(n+q))
{
init();
dfs1();
dfs2(,);
solve();
}
return ;
}
高手的代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=;
int fa[maxn],siz[maxn],son[maxn],w[maxn],p[maxn],dep[maxn],fp[maxn],Rank[maxn],ans[maxn];
//fa为父节点,siz为子节点中siz最大的,dep为深度,son为重儿子,w表示在线段树中的位置
int num[maxn<<],ppp[maxn<<];
int tree_id,n;
vector<int>G[maxn];
void dfs1(int u,int ff,int deep){
son[u]=;fa[u]=ff;siz[u]=;dep[u]=deep;
for(unsigned int i=;i<G[u].size();i++){
int v=G[u][i];
if(v==ff) continue;
dfs1(v,u,deep+);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int ff){
w[u]=++tree_id;p[u]=ff;Rank[w[u]]=u;
if(son[u]) dfs2(son[u],ff);
else return ;
for(unsigned int i=;i<G[u].size();i++){
int v=G[u][i];
if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
}
void pushup(int node){
if(num[node<<]>=num[node<<|]){
num[node]=num[node<<];ppp[node]=ppp[node<<];
}else{
num[node]=num[node<<|];ppp[node]=ppp[node<<|]; }
}
void buildtree(int le,int ri,int node){
if(le==ri){
num[node]=;ppp[node]=le;
return ;
}
int t=(le+ri)>>;
buildtree(le,t,node<<);
buildtree(t+,ri,node<<|);
pushup(node);
}
void update(int pos,int val,int le,int ri,int node){
if(le==ri){
num[node]+=val;
return ;
}
int t=(le+ri)>>;
if(pos<=t) update(pos,val,le,t,node<<);
else update(pos,val,t+,ri,node<<|);
pushup(node);
}
struct nnnn{
int u,v,z;
nnnn(int a,int b,int c){u=a;v=b;z=c;}
};
vector<nnnn>GG;
vector<int>GGG[maxn];
void getsum(int u,int v,int z){
int f1=p[u],f2=p[v];
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2);
swap(u,v);
}
GG.push_back(nnnn(w[f1],w[u],z));
u=fa[f1];f1=p[u];
}
if(dep[u]>dep[v]) swap(u,v);
GG.push_back(nnnn(w[u],w[v],z));
}
int main(){
int u,v,q,op,z;
while(scanf("%d%d",&n,&q)!=-){
if(n==&&q==) break;
for(int i=;i<maxn;i++) G[i].clear(),GGG[i].clear();
GG.clear();
memset(son,,sizeof(son));tree_id=;
for(int i=;i<n-;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(,,);
dfs2(,);
int max1=;
for(int i=;i<=q;i++){
scanf("%d%d%d",&u,&v,&z);
max1=max(max1,z);
getsum(u,v,z);
}
if(q==){
for(int i=;i<=n;i++) printf("0\n");
continue;
}
buildtree(,max1,);
for(int i=;i<GG.size();i++){
nnnn ne=GG[i];
GGG[ne.u].push_back(ne.z);
GGG[ne.v+].push_back(-ne.z);
}
for(int i=;i<=n;i++){
for(int j=;j<GGG[i].size();j++){
int ttt=GGG[i][j];
if(ttt<) update(-ttt,-,,max1,);
else update(ttt,,,max1,);
}
if(num[]==) ans[Rank[i]]=;
else ans[Rank[i]]=ppp[];
}
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
}
return ;
}
2014 ICPC---Relief grain(树链剖分)的更多相关文章
- hdu 5029 Relief grain(树链剖分+线段树)
题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...
- HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值
Relief grain Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5029 Relief grain --树链剖分第一题
题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...
- hdu_5029_relief grain(树链剖分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 题意:给你一个树,然后给你两点,将这两点之间的点涂上颜色,问涂色最多的那个颜色是什么,如果数量相 ...
- HDU5029--Relief grain (树链剖分+线段树 )
题意:n个点构成的无根树,m次操作, 对于操作 x y z, 表示 x 到 y 路径上的 每个点 加一个 z 数字,可重复加.最后输出每个点 加的次数最多的那个数字,如果没有输出0. 赤裸裸的树链剖分 ...
- HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...
- 2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问
链接:https://nanti.jisuanke.com/t/38229 题意: 给一棵树,多次查询,每次查询两点之间权值<=k的边个数 题解: 离线询问,树链剖分后bit维护有贡献的位置即可 ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 树链剖分+线段树+离线(广州网选赛第八题hdu5029)
http://acm.hdu.edu.cn/showproblem.php?pid=5029 Relief grain Time Limit: 10000/5000 MS (Java/Others) ...
随机推荐
- 为什么说基于TCP的移动端IM仍然需要心跳保活?
1.前言 很多人认为,TCP协议自身先天就有KeepAlive机制,为何基于它的通讯链接,仍然需要在应用层实现额外的心跳保活?本文将从移动端IM实践的角度告诉你,即使使用的是TCP协议,应用层的心跳保 ...
- CSS垂直三列居中,中间自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- KendoUI系列:TabStrip
<link href="@Url.Content("~/Content/kendo/2014.1.318/kendo.common.min.css")" ...
- IL指令速查
名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...
- 用户代理字符串userAgent可实现的四个识别
定义 用户代理字符串:navigator.userAgent HTTP规范明确规定,浏览器应该发送简短的用户代理字符串,指明浏览器的名称和版本号.但现实中却没有这么简单. 发展历史 [1]1993年美 ...
- BOM之navigator、history、screen对象
navigator对象 [定义] navigator已经成为识别客户端浏览器的事实标准.下表中列出存在于所有浏览器的属性和方法 [检测插件] 检测浏览器插件是一种最常见的检测例程. [1]对于非IE浏 ...
- 【总结】探索Newlife组件:服务代理利器XAgent的前世今生
本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html Newlife XCode组件相关文章目录:http://www.cn ...
- 数据结构:JAVA_二叉数查找树基本实现(上)
数据结构:二叉数查找树基本实现(JAVA语言版) 1.写在前面 二叉查找树是一种能将链表插入的灵活性与有序数组查找的高效性结合在一起的一种数据结构. ..... 2.代码分解 2.1 对节点的结构定义 ...
- 分享一个Jquery 分页插件 Jquery Pagination
分页插件来说,我觉得适用就行,尽量简单然后能够根据不同的应用场景能够换肤.展现形式等. 对于初学者想写分页插件的同学,也可以看下源码,代码也挺简单明了的,也助于自己写个小插件. 不过我比较懒,一般直接 ...
- PHP学习总结(一)
对最近学习PHP做个简单的总结吧 书籍:<PHP和MySQL Web开发> 环境/工具:wamp/Editplus&Chrome 时间:8月2日-8月7日 内容: 以前把前3章学了 ...