luogu

题意

给定一个n个节点的树,每个节点表示一个整数,问u到v的路径上有多少个不同的整数。

sol

也就是路径数颜色。树上莫队板子题。

我这种分块的姿势貌似是假的。

所以跑的是最慢的QAQ。

update 2018.4.5:真的是假的明明不带修改我块的大小还设的\(n^{0.6}\)。\(\sqrt n\)即可。

code

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 1e5+5;
int n,m,block,col[N],o[N],len,to[N<<1],nxt[N<<1],head[N],cnt;
int fa[N],dep[N],sz[N],son[N],top[N],dfn[N];
int Stack[N],tp,bl[N],ccnt,vis[N],tong[N],ans[N],Ans;
struct query{
int u,v,id;
bool operator < (const query &b) const
{return bl[u]==bl[b.u]?bl[v]<bl[b.v]:bl[u]<bl[b.u];}
}q[N];
void link(int u,int v){to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;}
void dfs1(int u,int f)
{
fa[u]=f;dep[u]=dep[f]+1;sz[u]=1;
for (int e=head[u];e;e=nxt[e])
if (to[e]!=f)
{
dfs1(to[e],u);
sz[u]+=sz[to[e]];
if (sz[to[e]]>sz[son[u]]) son[u]=to[e];
}
}
void dfs2(int u,int up)
{
top[u]=up;dfn[u]=++cnt;int ttp=tp;
if (son[u]) dfs2(son[u],up);
if (tp-ttp>=block) {++ccnt;while (tp>ttp) bl[Stack[tp--]]=ccnt;}
for (int e=head[u];e;e=nxt[e])
if (to[e]!=fa[u]&&to[e]!=son[u])
{
dfs2(to[e],to[e]);
if (tp-ttp>=block) {++ccnt;while (tp>ttp) bl[Stack[tp--]]=ccnt;}
}
Stack[++tp]=u;
}
int getlca(int u,int v)
{
while (top[u]!=top[v])
{
if (dep[top[u]]<dep[top[v]]) swap(u,v);
u=fa[top[u]];
}
return dep[u]<dep[v]?u:v;
}
void update(int x)
{
if (!vis[x])
{
vis[x]=1;++tong[col[x]];
if (tong[col[x]]==1) ++Ans;
}
else
{
vis[x]=0;--tong[col[x]];
if (tong[col[x]]==0) --Ans;
}
}
void change(int u,int v)
{
while (u!=v)
if (dep[u]>dep[v]) update(u),u=fa[u];
else update(v),v=fa[v];
}
int main()
{
n=gi();m=gi();block=pow(n,0.5);
for (int i=1;i<=n;++i) o[i]=col[i]=gi();
sort(o+1,o+n+1);len=unique(o+1,o+n+1)-o-1;
for (int i=1;i<=n;++i) col[i]=lower_bound(o+1,o+n+1,col[i])-o;
for (int i=1;i<n;++i)
{
int u=gi(),v=gi();
link(u,v);link(v,u);
}
dfs1(1,0);cnt=0;dfs2(1,1);
while (tp) bl[Stack[tp--]]=ccnt;
for (int i=1;i<=m;++i)
{
q[i]=(query){gi(),gi(),i};
if (bl[q[i].u]>bl[q[i].v]) swap(q[i].u,q[i].v);
}
sort(q+1,q+m+1);
change(q[1].u,q[1].v);
int gg=getlca(q[1].u,q[1].v);
update(gg);ans[q[1].id]=Ans;update(gg);
for (int i=2;i<=m;++i)
{
change(q[i].u,q[i-1].u);change(q[i].v,q[i-1].v);
gg=getlca(q[i].u,q[i].v);
update(gg);ans[q[i].id]=Ans;update(gg);
}
for (int i=1;i<=m;++i) printf("%d\n",ans[i]);
return 0;
}

[SPOJ10707]Count on a tree II的更多相关文章

  1. 【BZOJ2589】[SPOJ10707]Count on a tree II

    [BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...

  2. 【SPOJ10707】 COT2 Count on a tree II

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

  3. 「SPOJ10707」Count on a tree II

    「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...

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

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

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

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

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

  7. AC日记——Count on a tree II spoj

    Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...

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

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

随机推荐

  1. UI设计中的各种小控件

    xib支持图形化操作,提供了几乎所有的控件可供选择,只需拖动到相应的位置即可,但是控件的后台代码仍然需要手动编写,一定程度上加速了前台的开发. xib快速开发程序,手写代码速度比较慢 xib适合做静态 ...

  2. HTML系列(1)简介

        开始整理html的知识.     (1)HTML HTML 是用来描述网页的一种语言. 1.HTML指的是超文本标记语言: HyperText Markup Language 2.HTML不是 ...

  3. webpack基础配置

    webpack运行规则: Webpack 会给每个模块分配一个唯一的id并通过这个id索引和访问模块.在页面启动时,会先执行入口文件中的代码,其它模块会在运行 require 的时候再执行. 运行时主 ...

  4. CSS3手风琴下拉菜单

    在线演示 本地下载

  5. Kubernetes Headless Service

    1. Headless Service headless service 需要将 spec.clusterIP 设置成 None. 因为没有ClusterIP,kube-proxy 并不处理此类服务, ...

  6. contenteditable支持度

    contenteditable attribute (basic support) - Working Draft Global user stats*: Support: 86.71% Partia ...

  7. File类之在指定目录中查找文件

    package IoDemo; import java.io.File; /** * @Title:FileDemo2 * @Description:在指定的目录中查找文件 * @author Cra ...

  8. 写2个线程,一个打印1-52,一个打印A-Z,打印顺序是12A34B。。。(采用同步代码块和同步方法两种同步方法)

    1.同步方法 package Synchronized; /************************************同步方法****************************** ...

  9. OpenStack企业私有云新需求(1):Nova 虚机支持 GPU

    作者:Sammy Liu 刘世民 本系列会介绍OpenStack 企业私有云的几个需求: GPU 支持 自动扩展(Auto-scaling)支持 混合云(Hybrid cloud)支持 物理机(Bar ...

  10. Maven基础配置

    重要网址 Maven主页:http://maven.apache.org/ Maven central repository:http://search.maven.org/ Maven aliyun ...