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 ...
随机推荐
- C语言的那些事
变量的存数类型: 1:静态变量:凡是在代码任何快之外声明的变量总是存储在静态内存内,也就是不属于堆栈的内存. 对于这类变量.你无法对它们制指定存储类型. 2:存储于堆栈中,称为自动变量.当程序执行到声 ...
- 在myeclipse中修改发布到tomcat的项目目录名称
在项目上右键-->Properties 搜索web-->Project Facets(Web) 更改右侧Web Content-root的信息
- CSS学习总结2:CSS框模型
1.CSS框模型概述 CSS框模型规定了元素框处理元素内容.内边框.边框和外边框的方式. 元素框的最内部分是实际的内容,直接包围内容的是内边距.内边距呈现了元素的背景.内边距的边缘是边框.边框以外是外 ...
- 5I - 汉诺塔IV
还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到小盘的上面.xhd在想如果我们允许最大的盘子放到最上面会怎么样 ...
- Junit4知识梳理
一.junit官网 junit4:http://junit.org/junit4/ junit5:http://junit.org/junit5/ 二.github junit4: https://g ...
- python下的MySQL数据库编程
https://www.tutorialspoint.com/python/python_database_access.htm if you need to access an Oracle dat ...
- Python编程笔记(第一篇)Python基础语法
一.python介绍 1.编程语言排行榜 TIOBE榜 TIOBE编程语言排行榜是编程语言流行趋势的一个指标,每月更新,这份排行榜排名基于互联网有经验的程序员.课程和第三方厂商的数量. 2.pytho ...
- Visual code 搭建Vue项目
使用VS Code搭建Vue项目 1.安装 VScode 2..安装最新node.JS 2.安装cnpm镜像 淘宝镜像(node自带安装了npm,故不再安装) npm install -g cnpm ...
- 为什么要用jvm .
挚享科技 2018.4.8 运行java程序字节码,实现跨平台.. Java语言使用Java虚拟机屏蔽了与具体平台相关的信息,使得Java语言编译程序只需生成 在Java虚拟机上运行的目标代码(字节码 ...
- 使用django发送邮件(smtp)
首先在 seeting 最下面+上 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_USE_TLS = False ...