spoj cot: Count on a tree 主席树
10628. Count on a tree
Problem code: COT
You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight.
We will ask you to perform the following operation:
- u v k : ask for the kth minimum weight on the path from node u to node v
Input
In the first line there are two integers N and M.(N,M<=100000)
In the second line there are N integers.The ith integer denotes the weight of the ith node.
In the next N-1 lines,each line contains two integers u v,which describes an edge (u,v).
In the next M lines,each line contains three integers u v k,which means an operation asking for the kth minimum weight on the path from node u to node v.
Output
For each operation,print its result.
Example
Input:
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2
Output:
2
8
9
105
7 NOI前的最后一道题编这道主席树的题算是了解了对于主席树的“恐惧“。
这道题的大致思路是对于树上每一个点,都在其父节点基础上建主席树。
第一次编没有初始化建树操作的线段树来优化内存。还算比较顺利。以后看题的时候注意对于没有指明范围的量离散化。
虽然参加不了NOI现场赛,但还是希望自己在同步赛中rp++
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 211000
#define MAXE 211000
#define MAXT 16001000
int n,m;
int ptr[MAXN];
struct Edge
{
int np;
Edge *next;
}E[MAXE],*V[MAXN];
int tope=-;
void addedge(int x,int y)
{
E[++tope].np=y;
E[tope].next=V[x];
V[x]=&E[tope];
}
int wei[MAXN];
int depth[MAXN];
int fa[MAXN];
void dfs(int now,int d)
{
Edge *ne;
depth[now]=d;
for (ne=V[now];ne;ne=ne->next)
{
if (ne->np==fa[now])continue;
fa[ne->np]=now;
dfs(ne->np,d+);
}
}
int jump[MAXN][];
void init_lca()
{
int i,j;
for (i=;i<=n;i++)
{
jump[i][]=fa[i];
}
for (j=;j<;j++)
{
for (i=;i<=n;i++)
{
jump[i][j]=jump[jump[i][j-]][j-];
}
}
}
void swim(int &now,int d)
{
int i=;
while (d)
{
if (d&)now=jump[now][i];
i++;
d>>=;
}
}
int lca(int x,int y)
{
if (depth[x]<depth[y])
{
swim(y,depth[y]-depth[x]);
}
if (depth[y]<depth[x])
{
swim(x,depth[x]-depth[y]);
}
if (x==y)return x;
int i;
for (i=;i>=;i--)
{
if (jump[x][i]!=jump[y][i])
{
x=jump[x][i];
y=jump[y][i];
}
}
return fa[x];
}
struct node
{
int lch,rch,sum;
}tree[MAXT];
int topt=;
int get_node(node *nd)
{
if (nd==NULL)
{
return ++topt;
}
topt++;
tree[topt]=*nd;
return topt;
}
/*void build_tree(int &now,int l,int r)
{
now=++topt;
// tree[now].l=l;
// tree[now].r=r;
if (l==r)return ;
int mid=(l+r)/2;
build_tree(tree[now].lch,l,mid);
build_tree(tree[now].rch,mid+1,r);
}*/
void add_val(int &now,int &base,int l,int r,int pos,int v)
{
now=get_node(&tree[base]);
tree[now].sum+=v;
if (l==pos&&r==pos)
{
return ;
}
int mid=(l+r)/;
if (pos<=mid)
{
add_val(tree[now].lch,tree[base].lch,l,mid,pos,v);
return ;
}
if (mid<pos)
{
add_val(tree[now].rch,tree[base].rch,mid+,r,pos,v);
return ;
}
}
int root[MAXN];
void dfs2(int now)
{
Edge *ne;
add_val(root[now],root[fa[now]],,m,wei[now],);
for (ne=V[now];ne;ne=ne->next)
{
if (ne->np==fa[now])continue;
dfs2(ne->np);
}
}
struct weight_t
{
int id,w;
}wei2[MAXN];
bool cmp_w(const weight_t &w1,const weight_t &w2)
{
return w1.w<w2.w;
}
bool cmp_id(const weight_t &w1,const weight_t &w2)
{
return w1.id<w2.id;
}
int main()
{
//freopen("input.txt","r",stdin);
int i,j,k,x,y,z;
int q;
m=;
scanf("%d%d",&n,&q);
for (i=;i<=n;i++)
{
scanf("%d",&wei2[i].w);
wei2[i].id=i;
}
sort(&wei2[],&wei2[n+],cmp_w);
x=;y=-;
for (i=;i<=n;i++)
{
if (wei2[i].w!=y)
{
y=wei2[i].w;
ptr[x+]=wei2[i].w;
wei2[i].w=++x;
}else wei2[i].w=x;
}
sort(&wei2[],&wei2[n+],cmp_id);
for (i=;i<=n;i++)
wei[i]=wei2[i].w;
for (i=;i<n-;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
Edge *ne;
dfs(,);
init_lca();
//build_tree(root[0],1,m);
add_val(root[],root[],,m,wei[],);
for (ne=V[];ne;ne=ne->next)
dfs2(ne->np);
int t,temp;
int a[],topa;
int ans;
int l,r,mid;
for (i=;i<q;i++)
{
scanf("%d%d%d",&x,&y,&z);
t=lca(x,y);
if (t!=)a[]=root[fa[t]];
else a[]=;
a[]=root[t];
a[]=root[x];
a[]=root[y];
ans=;
l=,r=m;
while (true)
{
mid=(l+r)/;
temp=ans;
if (l==r)break;
if (a[])ans-=tree[tree[a[]].lch].sum;
if (a[])ans-=tree[tree[a[]].lch].sum;
if (a[])ans+=tree[tree[a[]].lch].sum;
if (a[])ans+=tree[tree[a[]].lch].sum;
if (ans>=z)
{
if (a[])a[]=tree[a[]].lch;
if (a[])a[]=tree[a[]].lch;
if (a[])a[]=tree[a[]].lch;
if (a[])a[]=tree[a[]].lch;
r=mid;
ans=temp;
}else
{
if (a[])a[]=tree[a[]].rch;
if (a[])a[]=tree[a[]].rch;
if (a[])a[]=tree[a[]].rch;
if (a[])a[]=tree[a[]].rch;
l=mid+;
}
}
printf("%d\n",ptr[l]);
}
}
spoj cot: Count on a tree 主席树的更多相关文章
- spoj COT - Count on a tree(主席树 +lca,树上第K大)
您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- SP10628 COT - Count on a tree 主席树
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<string> ...
- SPOJ Count on a tree(主席树+LCA)
一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to ...
- SPOJ - COT Count on a tree
地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N ...
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)
题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...
随机推荐
- 开源免费天气预报接口API以及全国全部地区代码!!(国家气象局提供)
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- [MySQL5.6] 一个简单的optimizer_trace示例
[MySQL5.6] 一个简单的optimizer_trace示例 前面已经介绍了如何使用和配置MySQL5.6中optimizer_trace(点击博客),本篇我们以一个相对简单的例子来跟踪op ...
- 项目FAQ
报错: Conversion from String Literal to Char* is deprecated http://stackoverflow.com/questions/1369030 ...
- WebService学习笔记系列(四)
今天主要来看看服务端的编写及发布. 服务端的编写主要包括三个步骤: 1.编写一个接口,即SEI(server endpoint interface) 2.编写接口的实现类,即SIB (server i ...
- Linux下安装、配置、授权、调优Mysql
以前在linux已经安装了很多次的Mysql,但是时间间隔长了以后,安装步骤总是会遗漏,趁这次安装,做一下安装记录. 检查系统是否已经安装Mysql rpm -qa|grep -i mysql Mys ...
- SCIP读书笔记(1)
这书也算是必修吧,尤其是我这种非科班人员,还是应该抽时间尽量学习一下.大致翻过一遍,习题非常多,尽力吧. ##构造过程抽象 * 为了表述认知,每种语言都提供了三种机制:基本元素:组合方式:抽象方法. ...
- Big Data 應用:第二季(4~6月)台湾地区Game APP 变动分布趋势图
图表简介: 该示意图表示了台湾地区第二季内所有Game APP类别的分布情形,经由该图表我们可以快速的了解到在这三个月内,哪类型的APP是很稳定:抑或者哪类型的APP是非常不稳定的. 名词解释: 类别 ...
- s标签可以if elseif else
首先引用s标签: <%@ taglib prefix="s" uri="/struts-tags" %> 使用s标签进行if elseif else ...
- CI框架篇之模型篇--直接操作(2)
在CI里面对数据库的操作有两种形式,一种是直接通过最原始的sql语句操作 一种则是通过CI里面的AR模型进行操作.两种操作各有千秋,应当有机的结合 现在讲解第一种操作的方式: 装载数据路操作类后就能够 ...
- query 防止ajax重复提交
项目用到js了,首选jquery,能用库用库,原则. 碰到重复提交的问题,禁止住才行.百度google,还是Google给力. 知乎上有个高人,总结了四种,利用Jquery .post方法返回jqXH ...