挺好的一道题目,怎么就没有版权了呢?大数据拍过了,精神AC。。。。

发现几种颜色这性质比较垃圾,不可加,莫队硬上。

%了一发popoqqq大神的博客,

看了一波VFK关于糖果公园的博客,

又找了wjmzbmr的博客看了看块状树。

感觉看了很多,明白了:

还TM是抄代码舒服

同序列上的莫队算法,只需要分块,排序之后进行统计,但是要注意更新状态的方法。

根据VFK所说的,为了方便操作,(否则直接暴力维护会有四种情况需要讨论)

我们统计时,把两点间的lca统一去掉,然后类似方法去维护。查询答案时加上、统计、再删去即可。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define maxn 100005
int n,m,col[maxn],h[maxn],to[maxn],ne[maxn],en=0;
int f[maxn][20],dep[maxn],sta[maxn],top=0,blo,cnt;
int bel[maxn],dfsid[maxn],tot=0,hav[maxn],in[maxn];
int nowans,ans[maxn],siz[maxn];
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
void add(int a,int b)
{to[en]=b;ne[en]=h[a];h[a]=en++;} void dfs(int x,int fa)
{
dfsid[x]=++tot;f[x][0]=fa;
if (siz[bel[fa]]==blo) bel[x]=++cnt;
else bel[x]=bel[fa];
siz[bel[x]]++;
for (int i=h[x];i>=0;i=ne[i])
if (to[i]!=fa){
dep[to[i]]=dep[x]+1;
dfs(to[i],x);
}
} struct Query{
int u,v,a,b,id;
void gett(){
u=read();v=read();a=read();b=read();
if (dfsid[u]>dfsid[v]) swap(u,v);
}
}b[maxn]; bool cmp(Query x,Query y)
{
return bel[x.u]==bel[y.u]?
bel[x.v]<bel[y.v]:bel[x.u]<bel[y.u];
} int lca(int a,int b)
{
if (dep[a]<dep[b]) swap(a,b);
if (a==b) return a;
int dist=dep[a]-dep[b];
D(i,15,0) if (dist&(1<<i)) a=f[a][i];
if (a==b) return a;
D(i,15,0) if (f[b][i]!=f[a][i])
{
b=f[b][i];
a=f[a][i];
}
return f[a][0];
} void rev(int x)
{
if (!x) return;
if (in[x])
{
in[x]^=1,hav[col[x]]--;
if (hav[col[x]]==0) nowans--;
}
else
{
in[x]^=1,hav[col[x]]++;
if (hav[col[x]]==1) nowans++;
}
} void rever(int x,int y)
{
int z=lca(x,y);
while (x!=z) rev(x),x=f[x][0];
while (y!=z) rev(y),y=f[y][0];
} int main()
{
memset(h,-1,sizeof h);
n=read();m=read();
blo=sqrt(n+1)+1e-5;
F(i,1,n)
col[i]=read();
F(i,1,n)
{
int a,b;
a=read();b=read();
add(a,b);add(b,a);
}
dfs(0,0); while(top)bel[sta[top--]]=cnt;
F(i,1,15) F(j,0,n) f[j][i]=f[f[j][i-1]][i-1];
F(i,1,m) b[i].gett(),b[i].id=i;
sort(b+1,b+m+1,cmp);
int nowu=0,nowv=0;
F(i,1,m)
{
rever(nowu,b[i].u);
rever(nowv,b[i].v);
nowu=b[i].u;nowv=b[i].v;
rev(lca(b[i].u,b[i].v));
ans[b[i].id]=nowans-(hav[b[i].a]&&hav[b[i].b]&&(b[i].a!=b[i].b));
rev(lca(b[i].u,b[i].v));
}
F(i,1,m) printf("%d\n",ans[i]);
}

  

最新文章

  1. opensuse 13.1 安装配置从0开始
  2. 从RAM新建QIcon对象 / Create a QIcon from binary data
  3. python排序算法的实现-快速排序
  4. 实现让Lync client也能够&quot;潜水&quot;隐身聊天
  5. 对于这个函数const int func(const int&amp; a) const声明中,三个const分别是什么意思?
  6. Maven-002-eclipse 插件安装及实例
  7. 为什么要使用SLF4J而不是Log4J
  8. iOS:UIAlertController和UIAlertAction的详解
  9. eclipse安装Gradle
  10. hdu 2602 Bone Collector(01背包)
  11. Service的启动与停止、绑定与解绑
  12. Java正则表达式(1)
  13. jdbc 日期 时间
  14. linux(Centos 6.3)学习笔记
  15. HDU 5107 线段树扫描线
  16. [Wc2010]重建计划
  17. ASP.NET Core 2 学习笔记(一)
  18. Error in Log_event::read_log_event(): &#39;Event too small&#39;, data_len: 0, event_type: 0
  19. python中的*arg和**kwargs
  20. Project 03- STM32F4xx PID controller

热门文章

  1. Jquery AJAX使用踩坑小记
  2. 浅析linux下软件的安装
  3. 记录我开发工作中遇到HTTP跨域和OPTION请求的一个坑
  4. Airbnb:别抵制我,宝宝要过 10 岁生日
  5. Robotium实践之路源码创建测试项目
  6. python:加密模块
  7. javase(6)_异常
  8. Codeforces Round #510 #B
  9. CF-1110 (2019/02/08)
  10. 【动态规划】luoguP1941 飞扬的小鸟