题目大意:给定一棵 N 个节点的无根树,每个节点有一个颜色。现有 M 个询问,每次询问一条树链上的不同颜色数。

题解:学会了树上莫队。

树上莫队是将节点按照欧拉序进行排序,将树上问题转化成序列上的问题进行求解的算法。需要分两种情况进行讨论,第一种情况是对于询问 x,y 来说,x 为 y 的祖先,则询问的区间为 \(st[x],st[y]\),第二种情况是 x 与 y 处在两个不同的子树内,这时发现 \(lca(x,y)\) 的欧拉序并不在 [ed[x], st[y]] 内,因此需要额外考虑 lca 的贡献。

代码如下

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define cls(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=4e4+10;
const int maxm=1e5+10;
const double eps=1e-6;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll fpow(ll a,ll b,ll c){ll ret=1%c;for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;return ret;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
/*------------------------------------------------------------*/ vector<int> G[maxn];
int f[maxn][20],dep[maxn],idfn[maxn<<1],st[maxn],ed[maxn],e_clk;
int n,m,a[maxn],d[maxn],tot,bsize;
struct query{int id,bl,l,r,lca;}q[maxm];
bool cmp(const query &x,const query &y){return x.bl!=y.bl?x.bl<y.bl:x.r<y.r;}
inline int get(int pos){return (pos-1)/bsize+1;} void dfs(int u,int fa){
st[u]=++e_clk,idfn[e_clk]=u;
for(int i=1;i<=16;i++)f[u][i]=f[f[u][i-1]][i-1];
for(auto v:G[u]){
if(v==fa)continue;
dep[v]=dep[u]+1,f[v][0]=u;
dfs(v,u);
}
ed[u]=++e_clk,idfn[e_clk]=u;
}
int getlca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=16;~i;i--)if(dep[f[x][i]]>=dep[y])x=f[x][i];
if(x==y)return x;
for(int i=16;~i;i--)if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
return f[x][0];
} void read_and_parse(){
n=read(),m=read(),bsize=sqrt(2*n);
for(int i=1;i<=n;i++)a[i]=d[i]=read();
sort(d+1,d+n+1);
tot=unique(d+1,d+n+1)-d-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(d+1,d+tot+1,a[i])-d;
for(int i=1,x,y;i<n;i++){
x=read(),y=read();
G[x].pb(y),G[y].pb(x);
}
dep[1]=1,dfs(1,0);
for(int i=1,x,y;i<=m;i++){
x=read(),y=read();
if(st[x]>st[y])swap(x,y);
int lca=getlca(x,y);
q[i].id=i;
if(lca==x)q[i].l=st[x],q[i].r=st[y],q[i].bl=get(q[i].l);
else q[i].l=ed[x],q[i].r=st[y],q[i].lca=lca,q[i].bl=get(q[i].l);
}
sort(q+1,q+m+1,cmp);
} bool vis[maxn];
int l=1,r=0,now=0,cnt[maxn],ans[maxm]; inline void update(int pos){
int u=idfn[pos];
if(vis[u]){
--cnt[a[u]];
if(!cnt[a[u]])--now;
}else{
if(!cnt[a[u]])++now;
++cnt[a[u]];
}
vis[u]^=1;
} void solve(){
for(int i=1,l=1,r=0;i<=m;i++){
while(r<q[i].r)update(++r);
while(r>q[i].r)update(r--);
while(l<q[i].l)update(l++);
while(l>q[i].l)update(--l);
if(q[i].lca)update(st[q[i].lca]);
ans[q[i].id]=now;
if(q[i].lca)update(st[q[i].lca]);
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
}
int main(){
read_and_parse();
solve();
return 0;
}

【SPOJ10707】COT2 - Count on a tree II的更多相关文章

  1. 【SPOJ10707】 COT2 Count on a tree II

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

  2. 【树上莫队】【SP10707】 COT2 - Count on a tree II

    Description 给定一棵 \(n\) 个点的树,每个节点有一个权值,\(m\) 次询问,每次查询两点间路径上有多少不同的权值 Input 第一行是 \(n\) 和 \(m\) 第二行是 \(n ...

  3. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

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

  5. COT2 - Count on a tree II(树上莫队)

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

  6. SPOJ10707 COT2 - Count on a tree II 【树上莫队】

    题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以 ...

  7. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)

    http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...

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

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

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

随机推荐

  1. object-fit 属性的用法介绍

    这个要在宽,高都是100%的情况下才能提现 object-fit 属性的用法介绍 fill(不保持纵横比缩放图片,使图片完全适应) contain(保持纵横比缩放图片,使图片的长边能完全显示出来) c ...

  2. 给普通用户添加root权限

    >>提君博客原创  http://www.cnblogs.com/tijun/  << 第一步,以root用户查看/etc/sudoers [root@ltt2 hadoop] ...

  3. 搞了一下午时间全浪费在这了,其实是自己拷贝了patch文件,导致tab变成了空格的错

    很老实的基于最新的kernel,源文件,修改了代码.通过diff -uNr --show-c-function dir1 dir2 > ipv6.patch制作了patch文件,准备代码上库构建 ...

  4. Git官方推荐用书

    用Git看了N多的Blog, 乱七八糟. 官方的推荐用书写得最好,最权威.还可以下载pdf.记录一笔. https://git-scm.com/book/zh/v2/

  5. vue監聽屬性

    使用$watch,就是監聽到某個值發生變化,執行回調函數.

  6. Javascript 实现复制(Copy)动作方法大全

    一.实现点击按钮,复制文本框中的的内容 <script type="text/javascript"> function copyUrl2() { var Url2=d ...

  7. BZOJ 1800 [Ahoi2009]fly 飞行棋

    题目链接 思路 终于有一道自己想出来的题了,开心. 因为是矩形,一定有直角,所以考虑直径,之后由于矩形对角线是两条直径,所以考虑组合数. 直径有n条,矩形有c(n,2)个. #include<i ...

  8. NFS共享存储的使用

    概述 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系统能够彼此 ...

  9. MySQL 同一台服务器同步数据

    声明:我配置出来的slave_io_running和slave_sql_running都是yes.但是数据并没有同步! 希望有遇到相同问题的朋友,能够告诉我一下解决方案? 首先,如何在同一个服务器安装 ...

  10. 基准对象object中的基础类型----字符串 (三)

    object有如下子类: CLASSES object basestring str unicode buffer bytearray classmethod complex dict enumera ...