spoj COT2(树上莫队)
模板。树上莫队的分块就是按dfn分,然后区间之间转移时注意一下就好。有个图方便理解http://blog.csdn.net/thy_asdf/article/details/47377709;
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int ccc[maxn],col[maxn],n,m,last[maxn],pre[maxn*],other[maxn*],d[maxn],bel[maxn],f[maxn][];
int res,bo,ind,dfn[maxn],ans[maxn*],t,cnt,sta[maxn],top,vis[maxn],tong[maxn],ttt[maxn];
struct que{
int u,v,id;
bool operator<(const que&tmp)const{
if(bel[u]==bel[tmp.u])return dfn[v]<dfn[tmp.v];
return bel[u]<bel[tmp.u];
}
}q[maxn*];
void add(int x,int y){++t;pre[t]=last[x];last[x]=t;other[t]=y;}
int dfs(int x){
int siz=;
dfn[x]=++ind;
for(int i=last[x];i;i=pre[i]){
int v=other[i];
if(v==f[x][])continue;
d[v]=d[x]+;f[v][]=x;
siz+=dfs(v);
if(siz>=bo){
++cnt;
for(int j=;j<=siz;++j)
bel[sta[top--]]=cnt;
siz=;
}
}
sta[++top]=x;
return siz+;
}
void change(int x){
if(!vis[x]){vis[x]=;tong[col[x]]++;if(tong[col[x]]==)res++;}
else{vis[x]=;tong[col[x]]--;if(tong[col[x]]==)res--;}
}
void solve(int u,int v){
while(u!=v){
if(d[u]>d[v])change(u),u=f[u][];
else change(v),v=f[v][];
}
}
int lca(int x,int y){
if(d[x]<d[y])swap(x,y);
for(int i=;i>=;--i)
if(d[f[x][i]]>=d[y])x=f[x][i];
if(x==y)return x;
for(int i=;i>=;--i)
if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
return f[x][];
}
int main(){
cin>>n>>m;
int x,y;bo=sqrt(n+0.5);
for(int i=;i<=n;++i){
scanf("%d",&ccc[i]);
ttt[i]=ccc[i];
}
sort(ttt+,ttt+n+);
int len=unique(ttt+,ttt+n+)-ttt-;
for(int i=;i<=n;++i){
col[i]=lower_bound(ttt+,ttt+len+,ccc[i])-ttt;
}
for(int i=;i<n;++i){
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
d[]=;dfs();
++cnt;
while(top)bel[sta[top--]]=cnt;
for(int j=;j<=;++j)
for(int i=;i<=n;++i)
f[i][j]=f[f[i][j-]][j-];
for(int i=;i<=m;++i){
q[i].id=i;
scanf("%d%d",&q[i].u,&q[i].v);
if(dfn[q[i].u]>dfn[q[i].v])swap(q[i].u,q[i].v);
}
sort(q+,q+m+);
int op=lca(q[].u,q[].v);
solve(q[].u,q[].v);
change(op);ans[q[].id]=res;change(op);
for(int i=;i<=m;++i){
solve(q[i-].u,q[i].u);solve(q[i-].v,q[i].v);
op=lca(q[i].u,q[i].v);
change(op);ans[q[i].id]=res;change(op);
}
for(int i=;i<=m;++i){
printf("%d\n",ans[i]);
}
//system("pause");
return ;
}
spoj COT2(树上莫队)的更多相关文章
- 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 ...
 - spoj COT2 - Count on a tree II   树上莫队
		
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
 - 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
		
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...
 - SPOJ  COT2  Count on a tree II (树上莫队,倍增算法求LCA)
		
题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...
 - 【SPOJ】Count On A Tree II(树上莫队)
		
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
 - 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 ...
 - SP10707 COT2 - Count on a tree II (树上莫队)
		
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
 - [SPOJ]Count on a tree II(树上莫队)
		
树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #incl ...
 - SP10707 COT2 - Count on a tree II [树上莫队学习笔记]
		
树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...
 
随机推荐
- 10.19JS日记
			
1.函数 关键词(function) var是js的关键字,用于声明变量,声明在内存模块完成,定义(=)是在执行模块完成 var可以在内存模块提前完成(js代码执行前),所以有变量提升这个功能 c ...
 - C# 使用printDocument1.Print打印时不显示 正在打印对话框
			
C#使用printDocument1.Print打印时不显示正在打印对话框有两种方法 第一种,使用PrintController PrintController printControll ...
 - hdu 2571 (命运) 那个配图女神
			
http://acm.hdu.edu.cn/showproblem.php?pid=2571 枚举每一个点,找出按照题目要求的这个点的上一点的最大值,合并到当前点,注意只取前面的一种情况 #inclu ...
 - JVM 类加载器 (二)
			
1.类加载器(ClassLoader)负责加载class文件,class文件在文件开头有特定的文件标识,并且ClassLoader只负责 class 文件的加载,至于class文件是否能够运行则由Ex ...
 - mtcp的快速编译(连接)
			
mtcp的快速编译 http://mos.kaist.edu/guide/config/03_build_mtcp.html 介绍DPDK中使用mtcp的文档 https://dpdksummit.c ...
 - 2019年学Java开发有优势吗?
			
随着信息科技的发展,在我们的日程生活和工作中到处充斥和使用着互联网信息技术.事实说明,互联网已经越来越广泛地深入到人们生活的方方面面,Java技术服务市场需求空缺会越来越大.学会一门IT技术,将拥有更 ...
 - java.lang.NoClassDefFoundError: org/apache/ibatis/cursor/Cursor
			
因为mybatis的版本和mybatis-spring的版本不兼容导致的,解决方法:mybatis的3.4.0及以上版本用mybatis-spring1.3.0及以上版本:mybatis的3.4.0以 ...
 - Rime小狼毫个人配置文件
			
default.custom.yaml customization: distribution_code_name: Weasel distribution_version: 0.9.30 gener ...
 - Eclipse生成部署描述符(web.xml)
			
右键点击你的web项目名--->Java EE Tools-->Generate Deployment Descriptor Stub 要想自动生成,只需在创建web项目时,把最后一页的 ...
 - Netty 源码 NioEventLoop(三)执行流程
			
Netty 源码 NioEventLoop(三)执行流程 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 上文提到在启动 N ...