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. HEXO next live2d插件删除问题

    title: HEXO next live2d插件删除问题 date: 2018-03-06 13:09:12 updated: tags: [hexo,next,建站,学习,前端技术,疑问] des ...

  2. chrome 切换到其他网络后重新加载网

    chrome 突然提示 “您与 www.google.com 之间的安全连接目前正受到干扰.  请等待几分钟后再尝试重新加载网页,或在切换到其他网络后重新加载网” 查看证书发现 已经过期 解决:同步下 ...

  3. 获取<考试>博文密码!o(*≧▽≦)ツ

    就是CJ高二组通用的密码 如果你想知道,请联系QQ,3057244225,或者直接面对面问博主(...) 是我们的内部材料,原创题目是不能外传的.请谅解. 当然如果是原题的话我们是不会上锁的啦

  4. ScrollView中ViewPager无法正常滑动问题

    本文主要介绍如何解决ViewPager在ScrollView中滑动经常失效.无法正常滑动问题. 解决方法只需要在接近水平滚动时ScrollView不处理事件而交由其子View(即这里的ViewPage ...

  5. 【Kth Smallest Element in a BST 】cpp

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  6. Kotlin的数据类:节省很多行代码(KAD 10)

    作者:Antonio Leiva 时间:Jan 25, 2017 原文链接:https://antonioleiva.com/data-classes-kotlin/ 在前面的文章中,我们已经见到了类 ...

  7. pandas.read_csv to_csv参数详解

    pandas.read_csv参数整理   读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/pandas ...

  8. user namespace 2

    昨天又试了一下, echo 0 1000 1 > /proc/<PID>/uid_map 想着直接把一个进程的uid设置成root,这样他就可以在自己的user namespace ...

  9. Scala 基础(8)—— 占位符_和部分应用函数

    1. 占位符 _ 针对 Scala 基础(7)—— 函数字面量和一等函数 中最后提到的,关于 filter 函数字面量的精简写法,可以使用占位符 _ 来代替. 占位符_,用来表示一个或者多个参数.(这 ...

  10. 【Luogu】P2498拯救小云公主(spfa)

    题目链接 我水爆了- - 容易发现可以把两个圆之间连边,左上为起点右下为终点,最小生成树直到起点跟终点连起来,输出边权/2就行. 然后80. 并不理解为什么这可以转化成spfa求最短路,邻接矩阵暴力跑 ...