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) ...
随机推荐
- 用css3实现各种图标效果(2)
写在前面 写的一模一样的css样式,结果却导致原来出来不一样的效果图. 用chrome的开发者工具查看,比较起来还是一模一样的css样式,可为什么会出现不一样的placeholder效果呢?一个白色粗 ...
- 使用python原生的方法实现发送email
使用python原生的方法实现发送email import smtplib from email.mime.text import MIMEText from email.mime.multipart ...
- Enterprise Solution 开发框架功能点
1. 通用查询模块,可以通过关联数据库表,存储过程或程序代码开发查询,多个查询之间也可构成主从关联查询. 2. 业务异常处理 支持统一的异常处理. 3. 内置一个简单的SQL Server查询分析器, ...
- 深入理解javascript作用域系列第一篇——内部原理
× 目录 [1]编译 [2]执行 [3]查询[4]嵌套[5]异常[6]原理 前面的话 javascript拥有一套设计良好的规则来存储变量,并且之后可以方便地找到这些变量,这套规则被称为作用域.作用域 ...
- javase基础复习攻略《六》
学习JAVA的同学都知道,sun为我们封装了很多常用类,本篇就为大家总结一下我们经常使用的类.上一篇博客一位朋友留言问我String是不是引用数据类型?我通过查找资料发现String属于应用数据类型, ...
- C#搭建足球赛事资料库与预测平台(1) 基本介绍
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源C#彩票数据资料库系列文章总目录:[目录]C#搭建足球赛事资料库与预测平台与彩票数据分析目录 去年4月到现在,一年 ...
- python类定义与c#的一些区别
c#中可以定义一个空类,但是python中定义空类需要加pass class EmptyClass(object): pass python的lei是多继承 python子类继承了基类,如果子类也 ...
- [OpenCV] Samples 01: drawing
基本的几何图形,标注功能. commondLineParser的使用参见:http://blog.csdn.net/u010305560/article/details/8941365 #includ ...
- Deep learning:四十(龙星计划2013深度学习课程小总结)
头脑一热,坐几十个小时的硬座北上去天津大学去听了门4天的深度学习课程,课程预先的计划内容见:http://cs.tju.edu.cn/web/courseIntro.html.上课老师为微软研究院的大 ...
- Apache Kylin 部署之不完全指南
1. 引言 Apache Kylin(麒麟)是由eBay开源的分布式分析引擎,提供Hadoop之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据.底层存储用的是HBase,数据输入与cu ...