SPOJ10707 COT2 - Count on a tree II 【树上莫队】
题目分析:
考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同。然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以改成询问$dfsin[u]$到$dfsin[v]$。否则改成$dfsout[u]$到$dfsin[v]$,并加上LCA上的影响,如果在询问过程中没有加入就加入,否则删除。
代码:
#include<bits/stdc++.h>
using namespace std; const int maxn = ;
const int maxm = ;
const int srt = ; int n,m;
int a[maxn],tb[maxn],arr[maxn],fa[maxn],pm[maxm],ans[maxm];
int dfsin[maxn],dfsout[maxn],Num,pre[maxn];
vector <int> g[maxn];
vector <pair<int,int> > Qy[maxn];
struct query{int l,r,bel,rem;}Q[maxm]; int found(int x){
int rx = x; while(pre[rx] != rx) rx = pre[rx];
while(pre[x] != rx){int tmp = pre[x]; pre[x] = rx; x = tmp;}
return rx;
} void dfs(int now,int ff){
arr[now] = ;pm[++Num] = now;fa[now] = ff;
dfsin[now] = Num;
for(int i=;i<Qy[now].size();i++){
if(!arr[Qy[now][i].first])continue;
Q[Qy[now][i].second].bel=found(Qy[now][i].first);
}
for(int i=;i<g[now].size();i++){
if(g[now][i]==fa[now])continue;
dfs(g[now][i],now);
}
pre[now] = fa[now];
pm[++Num] = now; dfsout[now] = Num;
} void read(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]),tb[i] = a[i];
sort(tb+,tb+n+); int num=unique(tb+,tb+n+)-tb-;
for(int i=;i<=n;i++) a[i]=lower_bound(tb+,tb+num+,a[i])-tb;
memset(tb,,sizeof(tb));
for(int i=;i<n;i++){
int u,v; scanf("%d%d",&u,&v);
g[u].push_back(v); g[v].push_back(u);
}
for(int i=;i<=m;i++) scanf("%d%d",&Q[i].l,&Q[i].r);
} int cmp(query alpha,query beta){
if(alpha.l/srt == beta.l/srt) return alpha.r < beta.r;
else return alpha.l/srt < beta.l/srt;
} void init(){
for(int i=;i<=m;i++){
Qy[Q[i].l].push_back(make_pair(Q[i].r,i));
Qy[Q[i].r].push_back(make_pair(Q[i].l,i));
}
for(int i=;i<=n;i++) pre[i] = i;
dfs(,);
for(int i=;i<=m;i++){
if(dfsin[Q[i].l] > dfsin[Q[i].r]) swap(Q[i].l,Q[i].r);
if(Q[i].bel == Q[i].l || Q[i].bel == Q[i].r){
Q[i].l = dfsin[Q[i].l]; Q[i].r = dfsin[Q[i].r];
Q[i].bel = i; Q[i].rem = ;
}else{
Q[i].l = dfsout[Q[i].l]; Q[i].r = dfsin[Q[i].r];
Q[i].rem = Q[i].bel; Q[i].bel = i;
}
}
sort(Q+,Q+m+,cmp);
} void work(){
int L = ,R = ,as=; tb[a[]]++;
for(int i=;i<=m;i++){
int nowl = Q[i].l,nowr = Q[i].r;
while(R < nowr){
R++;
if(dfsin[pm[R]]==R||dfsin[pm[R]]<L){
if(!tb[a[pm[R]]])as++;
tb[a[pm[R]]]++;
}else{
if(tb[a[pm[R]]] == ) as--;
tb[a[pm[R]]]--;
}
}
while(L > nowl){
L--;
if(dfsout[pm[L]]==L||dfsout[pm[L]]>R){
if(!tb[a[pm[L]]])as++;
tb[a[pm[L]]]++;
}else{
if(tb[a[pm[L]]] == ) as--;
tb[a[pm[L]]]--;
}
}
while(R > nowr){
if(dfsin[pm[R]]==R||dfsin[pm[R]]<L){
if(tb[a[pm[R]]] == ) as--;
tb[a[pm[R]]]--;
}else{
if(!tb[a[pm[R]]])as++;
tb[a[pm[R]]]++;
}
R--;
}
while(L < nowl){
if(dfsout[pm[L]]==L||dfsout[pm[L]]>R){
if(tb[a[pm[L]]] == ) as--;
tb[a[pm[L]]]--;
}else{
if(!tb[a[pm[L]]])as++;
tb[a[pm[L]]]++;
}
L++;
}
if(Q[i].rem && !tb[a[Q[i].rem]])ans[Q[i].bel]=as+;
else ans[Q[i].bel] = as;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
} int main(){
read();
init();
work();
return ;
}
SPOJ10707 COT2 - Count on a tree II 【树上莫队】的更多相关文章
- spoj COT2 - Count on a tree II 树上莫队
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- SP10707 COT2 - Count on a tree II [树上莫队学习笔记]
树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...
- SPOJ COT2 Count on a tree II 树上莫队算法
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...
- [SPOJ]Count on a tree II(树上莫队)
树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #incl ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 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 ...
- 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 ...
随机推荐
- Linux Namespace : Mount
Mount namespace 为进程提供独立的文件系统视图.简单点说就是,mount namespace 用来隔离文件系统的挂载点,这样进程就只能看到自己的 mount namespace 中的文件 ...
- k8s
https://www.cnblogs.com/sheng-jie/p/10591794.html
- 安装pandas时出现环境错误
在安装pandas时出现Could not install packages due to an EnvironmentErrorConsider using the `--user` option ...
- ElasticSearch(简称ES)
Windows下安装ElasticSearch ElasticSearch(简称ES)是一个基于Lucene的分布式全文搜索服务器,和SQL Server的全文索引(Fulltext Index) ...
- Oracle的数据类型详述
数据类型 (1)字符型 CHAR: 定长最多(2000字节)特定情况下用 VARCHAR2:可变长度的字符串最多(4000字节) LONG:大文本类型最多(2个G) (2)数值型 NUMBER:可以是 ...
- 【学亮IT手记】angularJS+select2多选下拉框实例
永远保持对大部分知识的好奇心,学习从不枯燥,也没有被逼学习一说,乐此不疲才是该有的心态和境界!!! 引入相关js库: html部分代码: angularJS定义数据源变量:
- 转《基于Ionic3实现微信支付和支付宝支付》
在Ionic应用里实现支付并不难,但是有的坑真是不爬不知道. 一:支付宝支付 网上关于支付宝支付cordova插件真是非常多,但是大多会报一些让你很无语的错误.比如sdk早已过时不是最新的,或者没有出 ...
- php5.6.x到php7.0.x特性
php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array,self,Clas ...
- PHP5.4.0新特性研究
PHP5.4.0新特性研究 1.内建Web Server 这的确是个好的改进,大大的方便了开发人员.以后开发机不装nginx,httpd也行 cd $PHP_INSTALL_PATH ./bin/ph ...
- 【git】如何去解决fatal: refusing to merge unrelated histories
我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传. 先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法p ...