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. openstack ocata版(脚本)计算节点安装

    一.初始化环境: 1.安装软件包: yum -y install centos-release-openstack-ocata yum -y upgrade yum -y install python ...

  2. windows下docker toolbox无法下载boot2docker.iso

    GitHub连不上导致自动更新失败.(网络形势严峻!) 通过别的途径手动下载了指定的最新的boot2docker.iso文件.(比方说迅雷!比方说迅雷!比方说迅雷!) https://github.c ...

  3. pyhton3 sys模块

    Python常用模块之sys sys模块提供了一系列有关Python运行环境的变量和函数. 1 ). sys.stdin 标准输入流.2 ).sys.stdout 标准输出流.3 ). sys.std ...

  4. JDK1.8(JRE)和eclipse-jee不匹配解决放

    想要用eclipse-jee的话,需要jdk1.8一下版本才能用. 1.需要下载jdk1.7 2.把jdk1.7安装(不需要设置环境变量). 3.在项目上右击选择properties 4.选择Java ...

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

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

  6. github资源下载速度慢的解决办法

    xx-net:https://github.com/XX-net/XX-Net

  7. libhdfs的配置和使用

    测试环境:centos6.10,hadoop2.7.3,jdk1.8 测试代码:HDFSCSample.c #include "hdfs.h" #include <strin ...

  8. perl FAQ(zz)

    1. Why do you write a program in Perl? Ans : Easy to use and fast execution since perl script underg ...

  9. 20145230《java程序设计》第6周学习总结

    20145230 <Java程序设计>第6周学习总结 教材学习内容 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出,可以 ...

  10. codeforces 439C 模拟

    http://codeforces.com/problemset/problem/439/C 题意:给你n个数,分成k个非空集合,其中有p个集合的元素和为偶数,其余k-p个集合的元素和为奇数. 思路: ...