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. linux systemctl 指令 —— 阮一峰

    Systemd 指令 原文链接如果有效,请点击原文链接查看.原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.ht ...

  2. [SCOI2005]超级格雷码

    题目 BZOJ 洛谷 做法 爆搜真有意思 满足不重复且异或后仅一位为\(1\) 利用奇偶性交叉搜索(仅改变一位) My complete code #include<bits/stdc++.h& ...

  3. bootstrap 模态框中弹出层 input不能获得焦点且不可编辑

    bootstrap 模态框中弹出层 input不能获得焦点且不可编辑 问题描述:bs框架支持一层model层的情况下,在模态框中弹出了自定义的弹出层.发现自定义弹出层的输入框不能获得焦点且不可编辑. ...

  4. 20145240 《Java程序设计》第二次实验报告

    20145240 <Java程序设计>第二次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.12 实验 ...

  5. python安装包

    1. python常用包下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy numpy包下载地址:https://pypi.python.org ...

  6. linux 安装jdk1.7 环境

    由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创 ...

  7. 安装配置Apollo-Prota web中心平台

    首先要求2g以上内存哈,JDK1.8 搭建数据库 创建表 apollo-build-scripts-master 整个阿波罗环境包 使用一个命令启动整个阿波罗服务环境 创建两个数据库 分别为:apol ...

  8. sql server update时,是行锁还是表锁

    https://bbs.csdn.net/topics/120000749 http://www.cnblogs.com/s021368/articles/2148659.html 问题: udpat ...

  9. Centos7 docker容器 搭建

    Dockerfile 文件如下: # # MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> # DOCKER-VERSION # # Dock ...

  10. 简介web服务器的工作原理

    一 什么是web服务器,应用服务器和web容器? 对于web服务器与应用服务器:“在过去它们是有区别的,但是这两个不同的分类慢慢地合并了,而如今在大多在情况下和使用中可以把它们看成一个整体.” 但是当 ...