题意:求一条链 \((u,v)\) 上不同的颜色数。

我们可以求出树的出栈入栈序(or 括号序?我也不确定)。

图(from attack

然后有一个很优美的性质:

设点 \(u\) 的入栈时间为 \(dfn[u]\) ,出栈时间为 \(low[u]\)

设两个点 \(u,v\) 满足 \(dfn[u]<dfn[v]\)

若 \(u\) 为 \(v\) 的 \(lca\),那么我们只需查询 \([dfn[u],dfn[v]]\) 中只出现一次的点有多少不同的颜色。

若 \(u\) , \(v\) 不在一个子树中,我们只需查询 \([low[u],dfn[v]]\) 中只出现一次的点有多少不同的颜色,并单独检查 \(lca\) 的颜色。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define R register int
using namespace std;
namespace Luitaryi {
inline int g() { R x=0,f=1;
register char s; while(!isdigit(s=getchar())) f=s=='-'?-1:f;
do x=x*10+(s^48); while(isdigit(s=getchar())); return x*f;
} const int N=200010,M=200010; bool vis[N];
int n,m,B,cnt,num,mem[N<<1],pos[N<<1],a[N],b[N],c[N],ans[N],tot;
int vr[N<<1],nxt[N<<1],fir[N],dfn[N],low[N],pre[N],top[N],sz[N],son[N],d[N];
inline void add(int u,int v) {
vr[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;
vr[++cnt]=u,nxt[cnt]=fir[v],fir[v]=cnt;
}
inline void dfs(int u) { sz[u]=1,dfn[u]=++num,mem[num]=u;
for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
if(d[v]) continue; pre[v]=u,d[v]=d[u]+1,dfs(v);
if(sz[son[u]]<sz[v]) son[u]=v; sz[u]+=sz[v];
} low[u]=++num,mem[num]=u;
}
inline void dfs2(int u,int tp) { top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(R i=fir[u];i;i=nxt[i]) { R v=vr[i];
if(v!=son[u]&&v!=pre[u]) dfs2(v,v);
}
}
inline int lca(int u,int v) {
for(;top[u]!=top[v];u=pre[top[u]]) if(d[top[u]]<d[top[v]]) swap(u,v);
return d[u]<d[v]?u:v;
}
struct node {int l,r,op,id;
inline bool operator < (const node& that) const
{return pos[l]==pos[that.l]?(pos[l]&1)?r<that.r:r>that.r:l<that.l;}
}q[M];
inline void add(int x) {if(++c[x]==1) ++tot;}
inline void sub(int x) {if(--c[x]==0) --tot;}
inline void f(int u) {vis[u]?sub(a[u]):add(a[u]); vis[u]^=1;}
inline void main() {
n=g(),m=g(); B=sqrt(n);
for(R i=1;i<=n;++i) a[i]=g();
memcpy(b,a,sizeof b);
sort(b+1,b+n+1); R t=unique(b+1,b+n+1)-b-1;
for(R i=1;i<=n;++i) a[i]=lower_bound(b+1,b+t+1,a[i])-b;
for(R i=1,u,v;i<n;++i) u=g(),v=g(),add(u,v); d[1]=1,dfs(1),dfs2(1,1);
for(R i=1,u,v,l;i<=m;++i) {
u=g(),v=g(),l=lca(u,v);
if(dfn[u]>dfn[v]) swap(u,v);
if(l==u) q[i]=(node){dfn[u],dfn[v],0,i};
else q[i]=(node){low[u],dfn[v],l,i};
} for(R i=1;i<=2*n;++i) pos[i]=(i-1)/B+1;
sort(q+1,q+m+1);
for(R i=1,l=1,r=0,op,LL,RR,id;i<=m;++i) {
LL=q[i].l,RR=q[i].r,op=q[i].op,id=q[i].id;
while(l<LL) f(mem[l++]);
while(l>LL) f(mem[--l]);
while(r<RR) f(mem[++r]);
while(r>RR) f(mem[r--]);
ans[id]=tot+(op&&c[a[op]]==0);
} for(R i=1;i<=m;++i) printf("%d\n",ans[i]);
}
} signed main() {Luitaryi::main(); return 0;}

2019.11.21

SP10707 COT2 - Count on a tree II 莫队上树的更多相关文章

  1. SP10707 COT2 - Count on a tree II 莫队

    链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...

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

    大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...

  3. SP10707 COT2 - Count on a tree II [树上莫队学习笔记]

    树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...

  4. [SP10707]COT2 - Count on a tree II

    题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序 ...

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

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

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

  8. 【SPOJ10707】 COT2 Count on a tree II

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

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

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

随机推荐

  1. Mybatis @Many注解一对多关联映射

    @Many注解:fetchType属性用于配置是否延迟加载

  2. FastDFS安装指南

    FastDFS安装指南 提前准备好的文件资料: 1.FastDFS--tracker安装 1.1 FastDFS安装环境 FastDFS是C语言开发,建议在linux上运行,本教程使用Centos7. ...

  3. git重置账号密码

    1.打开控制面板(快捷打开win+R,输入control) 2.点击打开用户账户 3.点击凭据管理器 4.点击windows凭据删除你的git凭据即可

  4. CCF 2016-12-1 最大波动

    CCF 2016-12-1 最大波动 题目 问题描述 小明正在利用股票的波动程度来研究股票.小明拿到了一只股票每天收盘时的价格,他想知道,这只股票连续几天的最大波动值是多少,即在这几天中某天收盘价格与 ...

  5. 第一个.NET小程序

    一.用户需求 做一个简单的网页版销售合同签核系统 1.业务员需要在手机或者电脑上操作,Key入销售合同 2.业务员填入相应的合同信息,对应主管签核 3.最终签核完,生成PDF版的销售合同,且上面自动加 ...

  6. .net core 定时程序

    using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft. ...

  7. 关闭 OSX 10.11 SIP (System Integrity Protection) 功能

    关闭 OSX 10.11 SIP (System Integrity Protection) 功能 来源 https://cms.35g.tw/coding/%E9%97%9C%E9%96%89-os ...

  8. 【洛谷 P2226】 [HNOI2001]遥控赛车比赛(最短路)

    题目链接 首先拆点,把每个点拆成4个点,表示到达这个点的时候赛车的朝向. 然后考虑连边. 相邻同向并且都是可以走的点直接连边权1的边. 至于怎么转向,只需在每个点\(i\)向每个方向一直拓展直到不能走 ...

  9. React 核心思想之声明式渲染

    React 发展很快,概念也多,本文目的在于帮助初学者理清 React 核心概念. React 及 React 生态 React 的核心概念只有 2 点: 声明式渲染(Declarative) 基于组 ...

  10. HTTP协议复习三--TCP/IP的网络分层模型和OSI 网络分层模型

    TCP/IP网络分层模型 第一层叫“链接层”(link layer),负责在以太网.WiFi这样的底层网络上发送原始数据包,工 作在网卡这个层次,使用MAC地址来标记网络上的设备,所以有时候也叫MAC ...