SPOJCOT2 Count on a tree II
分析
树上莫队裸题。
好博客
树剖的时候不能再次dfs重儿子。(好像是废话,但我因为这个问题调了三小时)
代码
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x){
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
const int MAXN=1e5+7;
int n,q;
map<int,int>M;
int a[MAXN],id;
int belong[MAXN],block;
struct Edge
{
int nx,to;
}E[MAXN<<2];
int head[MAXN],ecnt;
void addedge(int x,int y)
{
E[++ecnt].to=y;
E[ecnt].nx=head[x],head[x]=ecnt;
}
int fa[MAXN],dep[MAXN],sz[MAXN],son[MAXN],top[MAXN];
int st[MAXN],ed[MAXN],pot[MAXN],clk;
void dfs1(int x,int f)
{
fa[x]=f,sz[x]=1;
st[x]=++clk,pot[clk]=x;
dep[x]=dep[f]+1;
for(int i=head[x];i;i=E[i].nx)
{
int y=E[i].to;
if(y==f)
continue;
dfs1(y,x);
sz[x]+=sz[y];
if(sz[y]>sz[son[x]])
son[x]=y;
}
ed[x]=++clk,pot[clk]=x;
}
void dfs2(int x,int topf)
{
top[x]=topf;
if(!son[x])
return;
dfs2(son[x],topf);
for(int i=head[x];i;i=E[i].nx)
{
int y=E[i].to;
if(y==fa[x]||y==son[x]) // edit 1:cannot dfs son[x] twice and wrongly
continue;
dfs2(y,y);
}
}
int lca(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
struct Quiz
{
int l,r,f,id;
bool operator<(const Quiz&x)
{
if(belong[l]^belong[x.l])
return l<x.l;
else
return r<x.r;
}
}Q[MAXN];
int ql,qr,res;
bool used[MAXN];
int cnt[MAXN],ans[MAXN];
void add(int x)
{
if(++cnt[x]==1)
++res;
}
void del(int x)
{
if(--cnt[x]==0)
--res;
}
void change(int x)
{
used[x]?del(a[x]):add(a[x]);
used[x]^=1;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);read(q);
block=sqrt(n);
for(int i=1;i<=n;++i)
{
read(a[i]);
if(!M[a[i]])
M[a[i]]=++id;
a[i]=M[a[i]];
}
for(int i=1;i<=2*n;++i) // edit 1:belong should be inited to 2n
belong[i]=(i-1)/block+1;
for(int i=1;i<n;++i)
{
int x,y;
read(x);read(y);
addedge(x,y);
addedge(y,x);
}
dfs1(1,0);
dfs2(1,1);
for(int i=1;i<=q;++i)
{
int x,y;
read(x);read(y);
if(st[x]>st[y])
swap(x,y);
int f=lca(x,y);
if(f==x)
Q[i].l=st[x],Q[i].r=st[y];
else
Q[i].l=ed[x],Q[i].r=st[y],Q[i].f=f;
Q[i].id=i;
}
sort(Q+1,Q+q+1);
ql=1,qr=0,res=0;
for(int i=1;i<=q;++i)
{
while(ql>Q[i].l)
change(pot[--ql]);
while(qr<Q[i].r)
change(pot[++qr]);
while(ql<Q[i].l)
change(pot[ql++]);
while(qr>Q[i].r)
change(pot[qr--]);
if(Q[i].f)
change(Q[i].f);
ans[Q[i].id]=res;
if(Q[i].f)
change(Q[i].f);
}
for(int i=1;i<=q;++i)
printf("%d\n",ans[i]);
// fclose(stdin);
// fclose(stdout);
return 0;
}
SPOJCOT2 Count on a tree II的更多相关文章
- SPOJcot2 Count on a tree II (树上莫队)
You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 【BZOJ2589】[SPOJ10707]Count on a tree II
[BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 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 ...
- AC日记——Count on a tree II spoj
Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...
- 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 ...
- 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 ...
随机推荐
- ORACLE COMMENTON 使用
oracle中用comment on命令给表或字段加以说明,语法如下:COMMENT ON { TABLE [ schema. ] { table | view } | COLUMN [ s ...
- LeetCode--225--用队列实现栈
问题描述: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队 ...
- yarn的淘宝镜像
现在有很多人使用npm但是很多人也开始使用了更快的更方便额的yarn,可是yarn也有下载速度慢,容易被墙的担忧~又不能像npm那样直接安装淘宝镜像,所以就有了更改yarn的yarn源~ yarn c ...
- JavaScript In OA Framework
原文地址:JavaScript In OA Framework (需FQ) “To be or not to be…… is the question…..!” The famous soliloqu ...
- JAVA计算文件的crc32校验码
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- Hibernate---hbm2ddl和数据库方言的配置
Hibernate---hbm2ddl和数据库方言的配置 hibernate配置文件--hbm2ddl.auto属性值的区别: update: 最常用的取值,如果但其数据库中不存在表结构,那么自动创建 ...
- spring--boot @Valid的使用
spring--boot @Valid的使用 每天一个小知识点,每天进步一点点,总结是积累. springBoot @Valid的使用,解释一下.就是给摸个bean类属性(数据库字段)加一个门槛,比如 ...
- 一篇关于oracle psu的文章(转)
Oracle Database PSU/CPU Posted on 2011-07-28 16:27 dbblog 阅读(2569) 评论(0) 编辑 收藏 1. 什么是PSU/CPU?CPU: Cr ...
- React脚手架create-react-app+elementUI使用
一.介绍 1.create-react-app是FaceBook官方发布了一个无需配置的.用于快速构建开发环境的脚手架工具. 2.优点 a.无需配置:官方的配置堪称完美,几乎不用你再配置任何东西,就可 ...
- ArrayList与List<T>笔记
ArrayList与List<T>笔记 ArrayList是在System.Collections命名空间的一个类, 通过Add的方法添加一个项, 当进到这个类的元数据时, 可以看到这个方 ...