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. 利用京东云擎架设免费Wordpress 博客(git方式)

    京东云擎(JAE)是京东推出的一款公有云产品,是京东云平台生态圈的核心组成部分.与百度的BAE.新浪的SAE同样.可是JAE刚上线不久,可能非常多人都还没听说过或不太了解.我尝试在JAE安装WordP ...

  2. 管理http服务的脚本

    因为老是须要又一次安装系统,重一次都要又一次设置http服务的启动脚本.麻烦,所以这一次就把脚本备份出来. httpd for Ubuntu system: nginx + php-fpm #! /b ...

  3. 使用map端连接结合分布式缓存机制实现Join算法

    前面我们介绍了MapReduce中的Join算法,我们提到了可以通过map端连接或reduce端连接实现join算法,在文章中,我们只给出了reduce端连接的例子,下面我们说说使用map端连接结合分 ...

  4. 使用JAXB来实现Java合xml之间的转换

    使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了. //javaBean-->xml @Test public void test1() { try { JAXBContex ...

  5. yii2 验证码的使用

    @see  http://www.yiiframework.com/doc-2.0/yii-captcha-captcha.html 以下根据 MVC 模型的顺序来添加代码 1. model 层, 或 ...

  6. 40多个非常有用的Oracle 查询语句

    给大家介绍是40多个非常有用的Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有Oracle 开发者都必备的技能,所以快快收藏吧! 日期 ...

  7. jquery ajax获取和解析数据

    最近项目中用到了ajax技术,之前虽然写过一点点,但是没有系统的总结过.趁着刚刚用过,手热就记录一下,方便以后查阅. $.ajax中的参数 $.ajax的函数格式: $.ajax({ type: 'P ...

  8. TCP的服务流程

    TCP协议提供的是可靠的.面向连接的传输控制协议,即在传输数据前要先建立逻辑连接,然后再传输数据,最后释放连接3个过程.TCP提供端到端.全双工通信:采用字节流方式,如果字节流太长,将其分段:提供紧急 ...

  9. Asp.Net Mvc4 Ajax提交数据成功弹框后跳转页面

    1.cshtml页面代码 @model Model.UserInfo @{     ViewBag.Title = "Edit"; var options = new AjaxOp ...

  10. JS 定時刷新父類頁面

    function timeCount() { var url = "MAC.aspx"; parent.location.href = url; } function beginC ...