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. 当Hadoop 启动节点Datanode失败解决

    Hadoop 启动节点Datanode失败解决 [日期:2014-11-01] 来源:Linux社区  作者:shuideyidi [字体:大 中 小] 当我动态添加一个Hadoop从节点的之后,出现 ...

  2. 记录下MoKee编译过程

    纯属记录帖 关注和了解这个rom有段时间了. 最近有需要了解odex,折腾了几天还是在坑里. 索性,先编译下MoKee看看. 之前make过 4.2 和 5.1 ,刷到模拟器和N5里. 编译教程可以参 ...

  3. thinkPHP 表单自动验证功能

    昨天晚上我们老大叫我弄表单自动验证功能,愁了半天借鉴了好多官网的知识,才出来,诶,总之分享一下我自己的成果吧! thinkphp 在Model基类为我们定义了自动验证的函数和正则表达式,我们只需要在对 ...

  4. jeakins忘记密码时的处理(简单粗暴)

    1.打开config文件(通过ps -elf | grep jenkins查看JENKINS_HOME目录,然后在目录下查找config.xml文件) 2.修改<useSecurity>t ...

  5. ironic state information

    参考: http://blog.csdn.net/zhonglinzhang/article/details/74202562 http://blog.csdn.net/wanghuiict/arti ...

  6. 第三方库的安装:Pangolin

    Pangolin: 一款开源的OPENGL显示库,可以用来视频显示.而且开发容易. 代码我们可以从Github 进行下载:https://github.com/stevenlovegrove/Pang ...

  7. React跨域问题解决

    https://segmentfault.com/q/1010000012732581 非跨域问题报错 -rpccorsdomain="http://localhost:3000" ...

  8. 游戏开发学习ing

    创建工程 python cocos.py new HelloWorld -p com.cocos2dx.org -l cpp -d myprojects 这个命令就是运行python然后编译cos.p ...

  9. java与C#对比文章阅读

    文章:JAVA与C#的区别 讲了C#与java一些基本异同.    易语言官网有个表,比较了易语言.Java.C#的区别,比较全面可以借鉴.    

  10. jQuery基础知识点(下)

    在实际开发中,jQuery的实践性非常强大.上一篇本人已经整理出了一部分基础知识点,该文即是对以上的补充和扩展. 1.表单值的操作 //获取文本框的值 //txt.value var val = $( ...