Count on a tree II

思路:

  树上莫队;

  先分块,然后,就好办了;

来,上代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 40005
#define maxm 100005 struct QueryType {
int u,v,id;
};
struct QueryType qu[maxm]; int n,m,ti[maxn],num[maxn],Hash[maxn],siz;
int bel[maxn],f[maxn],deep[maxn],ans[maxm];
int E[maxn<<],V[maxn<<],head[maxn],cnt=;
int top[maxn],size[maxn],dis[maxn],sizee; bool if_[maxn]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void pre(int now,int fa)
{
f[now]=fa,deep[now]=deep[fa]+;
bel[now]=(++cnt+)/siz,size[now]=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
pre(V[i],now),size[now]+=size[V[i]];
}
} void dfs(int now,int chain)
{
top[now]=chain;int pos=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==f[now]) continue;
if(size[V[i]]>size[pos]) pos=V[i];
}
if(pos==) return ;
dfs(pos,chain);
for(int i=head[now];i;i=E[i])
{
if(V[i]==f[now]||V[i]==pos) continue;
dfs(V[i],V[i]);
}
} int solve_lca(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} bool cmp(QueryType aa,QueryType bb)
{
if(bel[aa.u]==bel[bb.u]) return bel[aa.v]<bel[bb.v];
else return bel[aa.u]<bel[bb.u];
} inline void updata(int to)
{
if(if_[to])
{
ti[num[to]]--;
if(ti[num[to]]==) cnt--;
}
else
{
ti[num[to]]++;
if(ti[num[to]]==) cnt++;
}
if_[to]=!if_[to];
} int main()
{
in(n),in(m);siz=sqrt(n);
for(int i=;i<=n;i++) in(num[i]),Hash[i]=num[i];
sort(Hash+,Hash+n+);
sizee=unique(Hash+,Hash+n+)-Hash-;int u,v;
for(int i=;i<=n;i++) num[i]=lower_bound(Hash+,Hash+sizee+,num[i])-Hash;
for(int i=;i<n;i++)
{
in(u),in(v);
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
cnt=,pre(,),dfs(,);
for(int i=;i<=m;i++)
{
in(qu[i].u),in(qu[i].v),qu[i].id=i;
if(bel[qu[i].u]>bel[qu[i].v]) swap(qu[i].u,qu[i].v);
}
sort(qu+,qu+m+,cmp);u=,v=,cnt=;
for(int no=;no<=m;no++)
{
int lca=solve_lca(u,qu[no].u);
while(u!=f[lca]) updata(u),u=f[u];u=qu[no].u;
while(u!=f[lca]) updata(u),u=f[u];u=qu[no].u;
lca=solve_lca(v,qu[no].v);
while(v!=f[lca]) updata(v),v=f[v];v=qu[no].v;
while(v!=f[lca]) updata(v),v=f[v];v=qu[no].v;
lca=solve_lca(u,v);
updata(lca),ans[qu[no].id]=cnt,updata(lca);
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}

AC日记——Count on a tree II spoj的更多相关文章

  1. AC日记——Count on a tree bzoj 2588

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  2. Count on a tree II SPOJ - COT2 && bzoj1086 王室联邦 && bzoj2589

    https://cn.vjudge.net/problem/SPOJ-COT2 这个是树上莫队模版啊.. 树上莫队有两种,第一种就是括号序莫队 设节点i在括号序中首次出现位置为pl[i] 那么路径(i ...

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

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

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

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

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

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

  7. 【SPOJ10707】 COT2 Count on a tree II

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

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

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

  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. Field 'flag' doesn't have a default value错误

    错误代码: java.sql.SQLException: Field 'flag' doesn't have a default value at com.mysql.jdbc.SQLError.cr ...

  2. 编译IOS下 libpcap1.7.2

    1.修改build-libpcap.sh libpcap版本号 2.执行脚本编译 出现问题: 2.发现configure过后,Makefile存在问题 实际configure的-arch armv7s ...

  3. luogu4196 [CQOI2006]凸多边形 半平面交

    据说pkusc出了好几年半平面交了,我也来水一发 ref #include <algorithm> #include <iostream> #include <cstdi ...

  4. 我的转行之路(电气转IT)------2018阿里校招面经

    博主本专业电气,今年3月下定决心转向互联网行业,本来想依仗自己比较自信的学习能力自学成才的,不过学了一段时间感觉还是需要一个人来指点,不仅仅是指点一些技术性的问题,更是需要有人来指点一下方向性的问题. ...

  5. 很全的 Python 面试题

    很全的 Python 面试题 Python语言特性 1 Python的函数参数传递 看两个例子:           Python   1 2 3 4 5 a = 1 def fun(a):      ...

  6. 剑指Offer - 九度1522 - 包含min函数的栈

    剑指Offer - 九度1522 - 包含min函数的栈2013-12-01 23:44 题目描述: 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 输入: 输入可能包含多个测 ...

  7. Swift 与众不同的地方

    Swift 与众不同的地方 switch(元组) 特点 其他语言中的switch语句只能比较离散的整形数据(字符可以转换成整数) 但是swift中可以比较整数.浮点数.字符.字符串.和元组数据类型,而 ...

  8. cloud.cfg_for_centos

    users: - default disable_root: 0 ssh_pwauth: 1 locale_configfile: /etc/sysconfig/i18n mount_default_ ...

  9. CodeForces-1061D TV Shows

    题目链接 https://vjudge.net/problem/CodeForces-1061D 题面 Description There are nn TV shows you want to wa ...

  10. selenium webdriver——JavaScript警告窗处理

    在WebDriver中处理JavaScript所生成的alert.confirm以及prompt,具体方法是使用switch_to_alert()方法定位到alert.confirm以及 prompt ...