AC日记——Count on a tree II spoj
思路:
树上莫队;
先分块,然后,就好办了;
来,上代码:
#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的更多相关文章
- AC日记——Count on a tree bzoj 2588
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- Count on a tree II SPOJ - COT2 && bzoj1086 王室联邦 && bzoj2589
https://cn.vjudge.net/problem/SPOJ-COT2 这个是树上莫队模版啊.. 树上莫队有两种,第一种就是括号序莫队 设节点i在括号序中首次出现位置为pl[i] 那么路径(i ...
- 【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 ...
- 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 ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 【BZOJ2589】[SPOJ10707]Count on a tree II
[BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...
- 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 ...
随机推荐
- Java-JNA使用心得
自上个月20号,历时整整一个月,终于找到工作入职了. 然后这段时间一直看公司的框架还有业务方面的东西.其实由于给分配了一个研究Java调用C语言接口的问题,导致框架业务方面的东西还不熟,然后现在手上又 ...
- vim 简单命令
(1)查找结果全部单独显示 命令: :lvimgrep /pattern/ % | lopen (2)设置文本高亮 命令: :colorscheme evening 把 ":colorsch ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目5
2014-03-21 21:37 题目:给定一个字符串数组,但是其中夹杂了很多空串“”,不如{“Hello”, “”, “World”, “”, “”, “”, “Zoo”, “”}请设计一个算法在其 ...
- USACO Section1.4 Mother's Milk 解题报告
milk3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- 把现有Unity3d游戏向Windows Phone 8.1移植(基础)
最近在将一款现有的游戏向Windows Phone平台移植,暂时完成了一个小阶段,做一个总结. 开发环境: Windows 8.1 系统及以上,愿意的话,用Windows 10 尝鲜也可以. 微软账号 ...
- springboot04 Ajax json Jquery
一.Ajax 1.同步&异步请求 在所有的请求响应交互世界里,我们有通常会划分出来两种形态的请求, 一种是同步请求.另一种是异步请求 .比如注册.登录.添加数据等等这些请求执行的就是同步请求, ...
- HTML5初识Canvas
HTML5初识Canvas <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- HDU 1532 基础EK Drainage Ditches
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 E: IQ(iq)
http://acm.ocrosoft.com/problem.php?cid=1316&pid=4 题目描述 根据世界某权威学会的一项调查,学信息学的学生IQ非常高.举个最好的例子,如果我们 ...
- php 值传递和引用传递的区别
值传递:函数范围内对值的任何改变在函数外部都会被忽略 引用传递:函数范围内对值的任何改变在函数外部也能反映出这些修改 A:按值传递时,php必须复制值.特别是对于大型的字符串和对象来说,这将会是一个代 ...