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) ...
随机推荐
- Java学习之路:不走弯路,就是捷径
1.如何学习程序设计? JAVA是一种平台,也是一种程序设计语言,如何学好程序设计不仅仅适用于JAVA,对C++等其他程序设计语言也一样管用.有编程高手认为,JAVA也好C也好没什么分别,拿来就用.为 ...
- MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突
在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...
- Atiti attilax主要成果与解决方案与案例rsm版 v2
Atiti attilax主要成果与解决方案与案例rsm版 v2 1. ##----------主要成果与解决方案与 参与项目1 ###开发流程与培训系列1 #-----组织运营与文化建设系列1 # ...
- atitit 业务 触发器原理. 与事件原理 docx
atitit 业务 触发器原理. 与事件原理 docx 1.1. 呵呵,你需要需要一个业务 触发器..1 1.2. 触发器/事件/中断的原理1 1.3. Io 硬件中断的原理( 中断的低层有cpu轮询 ...
- asp.net/html清理页面缓存的方法
(1) MVC BaseController: Controller内 protected override void Initialize(System.Web.Routing.RequestC ...
- CCNA网络工程师学习进程(6)vlan相关协议的配置与路由器简单配置介绍
前面已经介绍了大部分与vlan技术相关的交换机的协议的配置,更深层次的还有STP协议和以太网端口聚合技术,接着还会简单介绍一下路由器的基本应用. (1)STP(Spanning-tre ...
- WinRAR注册
新建一个txt文件并命名为"rarreg.key",添加以下内容保存,然后放置在WinRAR安装目录: RAR registration data Federal Agency f ...
- 体验WP 8.1 Update1开发不一定要更新VS2013 Update3
WP 8.1开发者预览版,估计大家也用得很High了,最近,MS推送了Update,主要的东西,不用说,就是最近被说得很火的小娜(Cortana),其实在推Update前几天,还有过一次小更新,当然我 ...
- springMVC robots.txt 处理
正常情况这样就好使 <mvc:resources mapping="/robots.txt" location="/lib/robots.txt"/> ...
- CSS 魔法系列:纯 CSS 绘制各种图形《系列六》
我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...