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) ...
随机推荐
- 理解nginx的配置
Nginx配置文件主要分成四部分:main(全局设置).server(主机设置).upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)和 location(URL匹配特定位置后的设置) ...
- 技术往事:改变世界的TCP/IP协议(珍贵多图、手机慎点)
1.前言 作为应用层开发人员,接触最多的网络协议通常都是传输层的TCP(与之同处一层的另一个重要协议是UDP协议),但对于IP协议,对于应用程序员来说更多的印象还是IP地址这个东西,再往深一点也就很难 ...
- MongoDB修改器的使用1
为什么要使用修改器? 通常我们只会修改文档的一部分,这时候更新整个文档就显得很麻烦,通常是通过原子性的更新修改器来完成. 1."$set"修改器 "$set ...
- 【Discuz】关闭QQ互联插件提示信息:系统繁忙,请稍后再试
版本:X3.2.20160601 提示信息 系统繁忙,请稍后再试 解决方案 Step1.删除QQ互联插件目录 网站的根目录\source\plugin\qqconnect Step2.上传原始QQ互联 ...
- javascript中Date对象的应用——简易日历的实现
× 目录 [1]效果 [2]HTML [3]CSS[4]JS 前面的话 简易日历作为javascript中Date对象的常见应用,用途较广泛.本文将详细说明简易日历的实现思路 效果演示 HTML说明 ...
- 关于CPU Cache -- 程序员需要知道的那些事
本文将介绍一些作为程序猿或者IT从业者应该知道的CPU Cache相关的知识.本章从"为什么会有CPU Cache","CPU Cache的大致设计架构",&q ...
- RAC碎碎念
1. 如何查看Oracle是否启动了RAC. SQL> show parameter cluster_database; NAME TYPE VALUE ------------------- ...
- zmNgFrameWork 架构升级ng1.5和md5静态资源缓存方案【angular1.x】
前言: 在我前面的博客,angular项目总结——angular + browserify + gulp + bower + less 架构分享 把我开发angular的架构进行了分享,并上传到了g ...
- [Linux] LD_LIBRARY_PATH
该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径(该路径在默认路径之前查找).若共享库不在缺省路径/lib或者/usr/lib下,就需要指定其他路径.实践中的一种解决方案是, ...
- ASP.NET 网站从Sever2003迁移到Sever 2008部署后不能访问
最近公司运维迁移网站遇到部署后始终不能访问,一直提示无法访问请求的页面,但是请求页面正常,程序没问题,在本地电脑运行正常,运维找了好久没找到原因. 后来问我,我也找了好久,最后终于解决了. 解决方法是 ...