COT2 - Count on a tree II

http://www.spoj.com/problems/COT2/

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 : ask for how many different integers that represent the weight of nodes there are on the path from u tov.

Input

In the first line there are two integers N and M. (N <= 40000, M <= 100000)

In the second line there are N integers. The i-th integer denotes the weight of the i-th 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 two integers u v, which means an operation asking for how many different integers that represent the weight of nodes there are on the path from u to v.

Output

For each operation, print its result.

Example

Input:
8 2
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5
7 8
Output:
4
4
 

题意:问树上两点间有多少不同的权值

树上莫队

开始狂T,发现自己竟是按节点编号划分的块!!

dfs分块。。

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 40001
#define M 100001
int n,m,siz,tmp;
int hash[N],key[N];
int front[N],to[N*],nxt[N*],tot;
int fa[N],deep[N],id[N],son[N],bl[N],block[N];
bool vis[N];
int sum[N],ans[M];
struct node
{
int l,r,id;
bool operator < (node p) const
{
if(block[l]!=block[p.l]) return block[l]<block[p.l];
return block[r]<block[p.r];
}
}e[M];
int read(int &x)
{
x=; char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') { x=x*+c-''; c=getchar(); }
}
void add(int x,int y)
{
to[++tot]=y; nxt[tot]=front[x]; front[x]=tot;
to[++tot]=x; nxt[tot]=front[y]; front[y]=tot;
}
void dfs(int x)
{
son[x]++;
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==fa[x]) continue;
deep[to[i]]=deep[x]+;
fa[to[i]]=x;
dfs(to[i]);
son[x]+=son[to[i]];
}
}
void dfs2(int x,int top)
{
id[x]=++tot;
bl[x]=top;
block[x]=(tot-)/siz+;
int y=;
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==fa[x]) continue;
if(son[to[i]]>son[y]) y=to[i];
}
if(!y) return;
dfs2(y,top);
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==fa[x]||to[i]==y) continue;
dfs2(to[i],to[i]);
}
}
void point(int u)
{
if(vis[u]) tmp-=(!--sum[hash[u]]);
else tmp+=(++sum[hash[u]]==);
vis[u]^=;
}
void path(int u,int v)
{
while(u!=v)
{
if(deep[u]>deep[v]) point(u),u=fa[u];
else point(v),v=fa[v];
}
}
int get_lca(int u,int v)
{
while(bl[u]!=bl[v])
{
if(deep[bl[u]]<deep[bl[v]]) swap(u,v);
u=fa[bl[u]];
}
return deep[u]<deep[v] ? u : v;
}
int main()
{
read(n);read(m); siz=sqrt(n);
for(int i=;i<=n;i++) read(key[i]),hash[i]=key[i];
sort(key+,key+n+);
key[]=unique(key+,key+n+)-(key+);
for(int i=;i<=n;i++) hash[i]=lower_bound(key+,key+key[]+,hash[i])-key;
int x,y;
for(int i=;i<n;i++)
{
read(x); read(y);
add(x,y);
}
tot=;
dfs();
dfs2(,);
for(int i=;i<=m;i++)
{
read(e[i].l); read(e[i].r);
e[i].id=i;
}
sort(e+,e+m+);
int L=,R=,lca;
for(int i=;i<=m;i++)
{
if(id[e[i].l]>id[e[i].r]) swap(e[i].l,e[i].r);
path(L,e[i].l);
path(R,e[i].r);
lca=get_lca(e[i].l,e[i].r);
point(lca);
ans[e[i].id]=tmp;
point(lca);
L=e[i].l; R=e[i].r;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
}
 

spoj COT2 - Count on a tree II的更多相关文章

  1. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  2. SPOJ COT2 Count on a tree II(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

  3. SPOJ COT2 Count on a tree II (树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

  4. spoj COT2 - Count on a tree II 树上莫队

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  5. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  6. SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)

    题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...

  7. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  8. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  9. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

随机推荐

  1. javascript对table的添加,删除行的操作

    <body> <form name="myForm"> <table width="100%" id="tab" ...

  2. Huffuman树

    问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的过程如下: 1. ...

  3. jQuery对象和DOM对象使用说明

    1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery对象和DOM对象以及它们之间的关系.DOM对象,即是我们用传 ...

  4. Alpha-5

    前言 失心疯病源5 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 14:30~15:30 阅读blob分析相关论文,找到一篇很早年的论文,但是作者讲解十分细 ...

  5. LintCode-211.字符串置换

    字符串置换 给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换. 置换的意思是,通过改变顺序可以使得两个字符串相等. 样例 "abc" 为 "cb ...

  6. Flink的序列化与flink-hadoop-compatibility

    最近 用户提交了一个问题 说他的jar包里明明包含相关的类型 但是在提交Flink作业的时候 却报出classnotfound的错误 查看之后发现 这里是flink的一个没有说的太明白的地方 用户的代 ...

  7. jCanvaScript canvas的操作库

    在jcscript.com上下载最新的jCanvaScript.1.5.18.min.js文件  里面有很多关于canvas的方法都已经是封装好了的,只需直接调用,但是要注意调用之前和调用之后都要写: ...

  8. matlab eval【转】

    Matlab 简单谈谈EVAL函数的用法 EVAL(s)相当于把字符串s的内容作为语句来执行. 比如:eval('a=3*5') 和直接在command 窗口中输入 a=3*5 等效 eval 一个经 ...

  9. 【bzoj3110】[Zjoi2013]K大数查询 权值线段树套区间线段树

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

  10. HTML5 不再支持哪些元素

    HTML5中已经不支持以下的元素,不建议在进行开发时再使用以下的元素. 1.acronym(建议abbr):定义首字母缩写 2.applet(建议object):定义applet 3.basefont ...