[SPOJ10707]Count on a tree II
题意
给定一个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的更多相关文章
- 【BZOJ2589】[SPOJ10707]Count on a tree II
[BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 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 ...
- AC日记——Count on a tree II spoj
Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...
- 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 ...
- 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 ...
随机推荐
- PAT 天梯赛 L1-032. Left-pad 【字符串】
题目链接 https://www.patest.cn/contests/gplt/L1-032 思路 要分两种情况处理 ①字符串长度 <= 填充长度 就在字符串前面输出(填充长度 - 字符串长度 ...
- $2015 武汉森果公司web后端开发实习日记----书写是为了更好的思考
找暑期实习,3月份分别投了百度和腾讯的实习简历,都止步于笔试,总结的主要原因有两点:基础知识不扎实,缺乏项目经验.后来到拉勾网等网站上寻找实习,看了很多家,都还是处于观望状态.后来参加了武汉实习吧在大 ...
- php大转盘抽奖
抽奖大转盘演示:http://www.sucaihuo.com/php/3301.html function getRand($proArr, $proCount) { $result = ''; $ ...
- 计算机网络概述 传输层 TCP拥塞控制
TCP拥塞控制 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就 ...
- JSP JavaBeans
Javabean的设计原则 公有类 无参公有构造方法 私有属性 getter和setter方法 在Jsp页面中如何使用Javabeans? 像使用普通Java类一样,创建JavaBeans实例. 在J ...
- jprofile查看hprof文件[转]
用jprofile打开hprof文件,查看内存泄露情况,有几个常用的功能说明一下: 工具下载:到官网下载jprofile7.0.1 64位的.再申请一个注册号,注册号的申请好像是一个邮件只能用一次. ...
- Centos6.5下Hbase安装
下载 http://mirror.bit.edu.cn/apache/hbase/hbase-0.94.26/hbase-0.94.26.tar.gz 2. 解压 tar -zxvf hbase-0 ...
- 用简单的反射优化代码(动态web项目)
在动态web项目中,没有使用框架时,只是简单的jsp访问servlet实现增删改查, 无论是哪个方法都需要经过Servlet中的doGet()方法或doPost()方法,我们可以在链接中附带参数进行区 ...
- Visual Studio中用于ASP.NET Web项目的Web服务器
当您在 Visual Studio 中开发 Web 项目时,需要 Web 服务器才能测试或运行它们. 利用 Visual Studio,您可以使用不同的 Web 服务器进行测试,包括 IIS Expr ...
- monkey参数应用
1.指定seed值 adb shell monkey -v -p package -s 100 100 2.touch事件(参数后都跟百分比) 3.设定动作百分比 4.轨迹球 5.基本导航事件 输入 ...