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# fckeditor浏览服务器和上传目录不一致,看不到上传过的文件
fckeditor在上传标签页面,传过文件后,在浏览服务器那里,看不到之前上传的文件,通过浏览服务器页面上传文件,保存的目录也和上传标签页面上传的不是同一个文件夹. 修改方法如下: 打开fckedit ...
- hdu 5692(dfs+线段树) Snacks
题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...
- hdu 5459(2015沈阳网赛) Jesus Is Here
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意 给出一组字符串,每个字符串都是前两个字符串相加而成,求第n个字符串的c的各个坐标的差的和,结果 ...
- Android.ApplicationCrash
1. 如何调试分析Android中发生的tombstone http://www.360doc.com/content/12/1017/10/7580194_241974419.shtml tombs ...
- Porsche PIWIS TESTER III
Allscanner VXDIAG Porsche Piwis III with Lenovo T440P Laptop Porsche Piwis tester III V37.250.020 N ...
- PHP学习笔记(二)
1.表单 PHP 的 $_GET和 $_POST用于检索表单中的值,比如用户输入. $_GET和$_POST变量分别用于收集来自 method="get" 和method=&quo ...
- UI设计师如何提升审美?
不得不承认,作为一名设计师,独特的审美能力是设计的灵感所在,不过很多刚刚从事UI设计的人,审美能力真的非常的一般,所以心中难免有这样的疑问,我的审美能通过后天的努力提升吗?关于这点,可以非常肯定的说, ...
- swift textfiled 输入完毕 return 隐藏键盘 方法
学习swift 真是件头疼的事情 会的人少,又没有OC基础,所以 且学切珍惜吧. 在做登录的时候发现textfiled 自动调用键盘后不能隐藏?头疼 ~~~~询问了好多人 终于有人自写解答 为了方便后 ...
- PDF下载网
http://www.java1234.com/a/javabook/javaweb/2018/1103/12297.html
- Max Chunks To Make Sorted II LT768
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...