正题

题目链接:https://www.luogu.com.cn/problem/P6177


题目大意

\(n\)个点的一棵树\(m\)次询问树上颜色。

强制在线

\(1\leq n\leq 4\times 10^5,1\leq m\leq 10^5,0\leq val_i<2^{31}\)


解题思路

把所有深度为\(\sqrt n\)并且下面至少有\(\sqrt n\)的深度的点标记,这样保证关键点数量不超过\(\sqrt n\)。

然后每个点到他周围关键点的距离也不会超过 \(\sqrt n\) 。

这样可以处理出关键点两两之间的颜色\(bitset\),然后每次路径找两个最近的关键点爆做就好了。

时间复杂度\(O((m+n)(\sqrt n+\frac{n}{\omega}))\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cmath>
using namespace std;
const int N=41000,T=300,M=(4e4)/T+10;
struct node{
int to,next;
}a[N<<1];
int n,m,tot,cnt,sum,fa[N],v[N],top[N],ls[N],d[N],dep[N],w[N],b[N],mark[N],dfn[N],ed[N],g[M][M];
bitset<N> f[M][M],bt;
void addl(int x,int y){
a[++tot].to=y;
a[tot].next=ls[x];
ls[x]=tot;return;
}
void dfs(int x){
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(y==fa[x])continue;
dep[y]=dep[x]+1;
fa[y]=x;dfs(y);
d[x]=max(d[x],d[y]+1);
}
if(dep[x]%T==0&&d[x]>=T)
mark[x]=++cnt;
return;
}
void dFs(int x){
if(mark[x])top[x]=x;dfn[x]=++cnt;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(y==fa[x])continue;
top[y]=top[x];dFs(y);
}
ed[x]=cnt;
return;
}
void calc(int x,int p,int fa){
if(!v[w[x]])bt[w[x]]=1,sum++;v[w[x]]++;
if(mark[x])f[p][mark[x]]=bt,g[p][mark[x]]=sum;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(y==fa)continue;
calc(y,p,x);
}
v[w[x]]--;if(!v[w[x]])bt[w[x]]=0,sum--;
return;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]),b[i]=w[i];
sort(b+1,b+1+n);
int L=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)
w[i]=lower_bound(b+1,b+1+L,w[i])-b;
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
addl(x,y);addl(y,x);
}
dfs(1);cnt=0;dFs(1);
for(int i=1;i<=n;i++)
if(mark[i])calc(i,mark[i],i);
int last=0;
while(m--){
int x,y;
scanf("%d%d",&x,&y);
x^=last;
if(top[x]==top[y]){
int xx=x,yy=y,ans=0;
while(x!=y){
if(dep[x]<dep[y])swap(x,y);
if(!v[w[x]])ans++,v[w[x]]=1;
x=fa[x];
}
if(!v[w[x]])ans++,v[w[x]]=1;
printf("%d\n",ans);last=ans;
x=xx;y=yy;
while(x!=y){
if(dep[x]<dep[y])swap(x,y);
v[w[x]]=0;x=fa[x];
}
v[w[x]]=0;
}
else{
if(dfn[x]>=dfn[y]&&dfn[x]<=ed[y])swap(x,y);
if(dfn[y]>=dfn[x]&&dfn[y]<=ed[x]){
int z=top[y];
while(top[fa[z]]!=top[x])
z=top[fa[z]];
bt=f[mark[z]][mark[top[y]]];
int ans=g[mark[z]][mark[top[y]]];
while(y!=top[y]){
if(!bt[w[y]])ans++,bt[w[y]]=1;
y=fa[y];
}
while(z!=x){
if(!bt[w[z]])ans++,bt[w[z]]=1;
z=fa[z];
}
if(!bt[w[z]])ans++,bt[w[z]]=1;
printf("%d\n",ans);last=ans;
}
else{
bt=f[mark[top[x]]][mark[top[y]]];
int ans=g[mark[top[x]]][mark[top[y]]];
while(x!=top[x]){
if(!bt[w[x]])ans++,bt[w[x]]=1;
x=fa[x];
}
while(y!=top[y]){
if(!bt[w[y]])ans++,bt[w[y]]=1;
y=fa[y];
}
printf("%d\n",ans);last=ans;
}
}
}
return 0;
}

P6177-Count on a tree II/[模板]树分块的更多相关文章

  1. 洛谷 P6177 - Count on a tree II/【模板】树分块(树分块)

    洛谷题面传送门 好家伙,在做这道题之前我甚至不知道有个东西叫树分块 树分块,说白了就是像对序列分块一样设一个阈值 \(B\),然后在树上随机撒 \(\dfrac{n}{B}\) 个关键点,满足任意一个 ...

  2. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  3. 【BZOJ2589】[SPOJ10707]Count on a tree II

    [BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...

  4. 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  ...

  5. 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 ...

  6. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

  7. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  8. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  9. 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 ...

随机推荐

  1. SpringCache(redis)

    pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  2. HBuilder mui 手机app开发 Android手机app开发 ios手机app开发 打开新页面 预加载页面 关闭页面

    创建子页面 在mobile app开发过程中,经常遇到卡头卡尾的页面,此时若使用局部滚动,在android手机上会出现滚动不流畅的问题: mui的解决思路是:将需要滚动的区域通过单独的webview实 ...

  3. flutter中将widget转为base64

    flutter中可以通过RepaintBoundary widget中的toImage方法将页面中的widget转为base64. 如何使用? 首先要在全局定义一个global key,分配给Repa ...

  4. snoop的基本用法

    关于Snoop的用法   snoop是开发wpf应用程序的利器.用它可以观察WPF的可视树,监听事件,更改元素属性等.   下面我介绍下snoop一些用法.   1.获取指定应用程序的UI     打 ...

  5. C#多线程详解(一) Thread.Join()的详解

    bicabo   C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...

  6. kettle 查询 tinyint 值为 Y,kettle 查询 tinyint 为布尔值

     问题解决方法 1.在数据库连接中的[选项]命令参数中加入:tinyInt1isBit = false,如下图: 实际场景:

  7. HbaseWAL

    1.WAL意为 Write Ahead Log ,类似MySQL中的binlog,用来做灾难恢复之用,HLog记录数据的所有变更,一旦数据修改,就可以从Log中进行恢复. Hbase采用类LSM的架构 ...

  8. 带你走进MySQL全新高可用解决方案-MGR

    ​一.初识MGR 相信很多人对MGR这个词比较陌生,其实MGR(全称 MySQL Group Replication [MySQL 组复制])是Oracle MySQL于2016年12月发布MySQL ...

  9. 微信小程序的button按钮设置宽度无效

    亲,你是不是也遇到了微信小程序的button按钮设置宽度无效.让我来告诉你怎么弄 方法1. 样式中加入!important,即:width: 100% !important; wxss代码示例 1 2 ...

  10. VS2017 创建并测试 C++ dll

    生成DLL 创建工程: Create new project -> 选择Visual C++ -> Windows Desktop -> Dynamic-Link Library ( ...