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 ...
随机推荐
- 关于C和C++
最开始学的就是C和C++,但只是学过,根本就不知道怎么使用. 后来接触了Python和Perl才知道怎么将编程应用于实际需求当中,读取文件,存放到数据结构,处理,输出. 但脚本语言有其固有的缺点,不能 ...
- English trip -- VC(情景课)1 D
Read 阅读 Welcome! Meet our new student. His first name is Ernesto. 欧内斯托 His last name is Delgado. 德尔 ...
- 3-13《元编程》第5章Class Definitions 3-14(5-4Singleton Classes,2小时)3-15(3小时✅)
类宏 环绕别名 singleton class Class就是增强的模块,类定义也就是模块定义. 5.1 Class Definitions Demystified 5.11 Inside Class ...
- 安装 tensorflow 时遇到 OSError: [Errno 1] Operation not permitted 的解决办法
Installing collected packages: numpy, scipy, six, pyyaml, Keras, opencv-python, h5py, html5lib, blea ...
- 『Kaggle』分类任务_决策树&集成模型&DataFrame向量化操作
决策树这节中涉及到了很多pandas中的新的函数用法等,所以我单拿出来详细的理解一下这些pandas处理过程,进一步理解pandas背后的数据处理的手段原理. 决策树程序 数据载入 pd.read_c ...
- XML文档的读、写
代码: XmlDocument doc = new XmlDocument(); doc.Load("Books.xml"); //1.加载要读取的XML文件 //要想看到数据得先 ...
- BZOJ2590 [Usaco2012 Feb]Cow Coupons
好吧...想了半天想错了...虽然知道是贪心... 我们每次找没有被买的两种价格最小的牛,比较a = 当前差价最大的 + 当前优惠券价格最小的牛与b = 当前非优惠券价格最小的牛 所以...我们要 先 ...
- 什么是REST API?
REST指一组架构约束条件和原则,满足约束条件和原则的应用程序设计.架构,软件体系结构分为三部分:构建,用于描述计算机:连接器,用于描述构建的链接部分:配置将构建和连接器组成有机整体.web基本技术: ...
- Java——多线程练习
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 1.2 C++命名空间(namespace)
参考:http://www.weixueyuan.net/view/6326.html 总结: C++语言引入命名空间(Namespace)这一概念主要是为了避免命名冲突,其关键字为 namespac ...