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 ...
随机推荐
- easyui-datagrid单选模式下隐藏表头的全选框
easyui-datagrid可以不使用复选框来进行单选,直接使用onSelect和 singleSelect:true就可以实现单选,但是有一些用户会比较习惯使用勾选框,这时会加一列checkbox ...
- laravel5.5用户认证源码分析
目录 1. 生成相关文件和配置 2. 分析路由文件 3. 以登陆开始为例,分析auth到底是怎么工作的 3.1 分析登录文件 3.2 分析门面Auth. 1. 生成相关文件和配置 快速生成命令 php ...
- 剑指Offer - 九度1350 - 二叉树的深度
剑指Offer - 九度1350 - 二叉树的深度2013-11-23 00:54 题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的 ...
- global js库
var GLOBAL = {}; GLOBAL.namespace = function(str) { var arr = str.split("."), o = GLOBAL,i ...
- diskimage-builder-command
yum -y install python-virtualenv.noarch virtualenv ~/dib-virtualenv . ~/dib-virtualenv/bin/activate ...
- CSU-2110 Keeping Cool
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2110 题目 Description Kevin has just got ...
- vue 自定义过度组件用法
HTML: <div id="example-1"> <button @click="show = !show"> Toggle ren ...
- 微信小程序--背景图片手机无法预览
目前小程序好像没有支持手机预览背景本地图片,所以将本地图片改为网络图片链接就可以了 background: url("https://..../img/no.png") no-re ...
- 初识 HTML5(一)
H5其实就是H4的一个增强版本,我们在利用H5进行网页的构造会更简便,标签语义更简洁明了.首先,我们要理解HTML4,它是HTML的标记+css2+JavaScript的一些基本应用,简言之,就是AP ...
- Tomcat源码浅析
最近在学习tomcat源码,算是把tomcat的整个流程梳理通了. 从上图来看,tomcat把模块化使用到了极致,配合组件生命周期的管理,让代码看起来结构清晰,而且很容易进行业务扩展. 1.上图的接口 ...