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
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 主席树的更多相关文章

  1. spoj COT - Count on a tree(主席树 +lca,树上第K大)

    您将获得一个包含N个节点的树.树节点的编号从1到Ñ.每个节点都有一个整数权重. 我们会要求您执行以下操作: uvk:询问从节点u到节点v的路径上的第k个最小权重 输入 在第一行中有两个整数Ñ和中号.( ...

  2. 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 ...

  3. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  4. BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

    分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...

  5. SP10628 COT - Count on a tree 主席树

    Code: #include<cstdio> #include<cstring> #include<algorithm> #include<string> ...

  6. 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  ...

  7. 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  ...

  8. 【BZOJ-2588】Count on a tree 主席树 + 倍增

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 3749  Solved: 873[ ...

  9. Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

随机推荐

  1. 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)

    目前国内对于双卡智能手机的需求还是很大的,各种复杂的业务会涉及到双卡模块:而android标准的api又不提供对双卡的支持.导致国内双卡模块标准混乱,各个厂商各玩各的.目前我知道的双卡解决方案就有:m ...

  2. Fastjson介绍

    简单介绍 Fastjson是一个Java语言编写的高性能功能完好的JSON库. 高性能 fastjson採用独创的算法,将parse的速度提升到极致,超过全部json库,包含以前号称最快的jackso ...

  3. Nginx vs Apache--reference

    May 14th, 2014 - By Walker Rowe https://anturis.com/blog/nginx-vs-apache/ What is the Nginx web and ...

  4. MVVM架构的一次实践,重写iOS头条客户端

    前言: 一个iOS头条APP,使用MVVM架构实现,代码中有注释,封装了AFN网络请求,解媾代码,使用起来非常方便.用最经典的TableView展示,后续不断更新,喜欢就star或fork一下,有问题 ...

  5. 今天给大家分享一下Android中的资源与国际化的问题

    摘要:该文章将向大家分享Android中的资源与国际化的问题. 今天给大家分享一下Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layo ...

  6. apache下配置虚拟主机案例详解

    案例说明:    域名:        www.tianyik.com  /var/html/www        blog.tianyik.com  /var/html/blog        bb ...

  7. HDFS的Java客户端操作代码(HDFS删除文件或目录)

    1.HDFS删除文件或目录 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.hadoo ...

  8. Unity5.0 手动激活

    提供Unity5.0.1.f1(32-bit)下载http://pan.baidu.com/s/1bg5sDK 密码 ns75 有时候会发现,用激活工具是激活不了的,这个时候就要手动激活,其实个人觉得 ...

  9. [Excel] CsvHelper---C#关于CSV文件的导入和导出以及转化 (转载)

    点击下载 CsvHelper.rar 这个类是关于Csv文件的一些高级操作1.DataTable导出到CSV2.将Csv读入DataTable看下面代码吧 /// <summary> // ...

  10. js 配置基础启动文件

    页面启动文件boot.js,获取存放该文件的路径,放置通用的css,js代码,方便html页面调用. __CreateJSPath = function (js) { var scripts = do ...