SP10707 COT2 - Count on a tree II 莫队
链接
https://vjudge.net/problem/SPOJ-COT2
https://www.luogu.org/problemnew/show/SP10707
思路
dfs欧拉序转化为普通莫队(并不算树上莫队,不过也可做)
好神仙啊,原来欧拉序是可以求任意两点的点,不过要加lca。
代码
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,m,w[N],now_ans,top;
vector<int> G[N];
int st[N][30],dep[N],belong[N<<1],ans[N];
int lsh[N],a[N<<1],be[N],en[N<<1],T[N];
int js[N];
struct node {
int l,r,other,id;
bool operator < (const node &b) const {
return belong[l]==belong[b.l] ? belong[l]&1 ? r<b.r : r>b.r :belong[l]<belong[b.l];
}
}Q[N];
void dfs(int u,int f) {
a[++top]=u;
be[u]=top;
for(vector<int>::iterator it=G[u].begin();it!=G[u].end();++it) {
if(*it==f) continue;
st[*it][0]=u;
dep[*it]=dep[u]+1;
dfs(*it,u);
}
a[++top]=u;
en[u]=top;
}
int lca(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
for(int i=25;i>=0;--i)
if(dep[st[x][i]]>=dep[y]) x=st[x][i];
if(x==y) return x;
for(int i=25;i>=0;--i)
if(st[x][i]!=st[y][i]) x=st[x][i],y=st[y][i];
return st[x][0];
}
void add(int x) {
if(!T[x]) now_ans++;
T[x]++;
}
void del(int x) {
T[x]--;
if(!T[x]) now_ans--;
}
int main() {
//freopen("a.in","r",stdin);
n=read(),m=read();
for(int i=1;i<=n;++i) lsh[i]=w[i]=read();
sort(lsh+1,lsh+1+n);
int len=unique(lsh+1,lsh+1+n)-lsh-1;
for(int i=1;i<=n;++i) w[i]=lower_bound(lsh+1,lsh+1+len,w[i])-lsh;
for(int i=1;i<n;++i) {
int x=read(),y=read();
G[x].push_back(y);
G[y].push_back(x);
}
dep[1]=1;
dfs(1,0);
for(int j=1;j<=25;++j) {
for(int i=1;i<=n;++i) {
st[i][j]=st[st[i][j-1]][j-1];
}
}
for(int i=1;i<=m;++i) {
int x=read(),y=read(),z=lca(x,y);
if(be[x]>be[y]) swap(x,y);
if(z==x||z==y) {
Q[i].l=be[x];
Q[i].r=be[y];
Q[i].other=0;
} else {
Q[i].l=en[x];
Q[i].r=be[y];
Q[i].other=z;
}
Q[i].id=i;
}
int k=sqrt(2*n);
for(int i=1;i<=2*n;++i) belong[i]=(i-1)/k+1;
sort(Q+1,Q+1+m);
for(int i=1,l=1,r=0;i<=m;++i) {
while(r<Q[i].r) {//add(++r)
++r;
if(js[a[r]])
del(w[a[r]]);
else
add(w[a[r]]);
js[a[r]]^=1;
}
while(l>Q[i].l) {//add(--l)
--l;
if(js[a[l]])
del(w[a[l]]);
else
add(w[a[l]]);
js[a[l]]^=1;
}
while(l<Q[i].l) {//del(l++)
if(js[a[l]])
del(w[a[l]]);
else
add(w[a[l]]);
js[a[l]]^=1;
l++;
}
while(r>Q[i].r) {//del(r--)
if(js[a[r]])
del(w[a[r]]);
else
add(w[a[r]]);
js[a[r]]^=1;
r--;
}
if(Q[i].other)
add(w[Q[i].other]);
ans[Q[i].id]=now_ans;
if(Q[i].other)
del(w[Q[i].other]);
// cout<<Q[i].l<<" "<<Q[i].r<<"\n";
}
for(int i=1;i<=m;++i) printf("%d\n",ans[i]);
return 0;
}
SP10707 COT2 - Count on a tree II 莫队的更多相关文章
- SP10707 COT2 - Count on a tree II 莫队上树
题意:求一条链 \((u,v)\) 上不同的颜色数. 我们可以求出树的出栈入栈序(or 括号序?我也不确定). 图(from attack) 然后有一个很优美的性质: 设点 \(u\) 的入栈时间为 ...
- 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 ...
- [SP10707]COT2 - Count on a tree II
题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序 ...
- 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 ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 【树上莫队】【SP10707】 COT2 - Count on a tree II
Description 给定一棵 \(n\) 个点的树,每个节点有一个权值,\(m\) 次询问,每次查询两点间路径上有多少不同的权值 Input 第一行是 \(n\) 和 \(m\) 第二行是 \(n ...
随机推荐
- clear/reset select2,重置select2,恢复默认
4.0 version //方法一$('#yourButton').on('click', function() { $('#yourfirstSelect2').val(null).trigger( ...
- [ Windows BAT Script ] BAT 脚本获取windows权限
BAT 脚本获取windows权限 @echo off echo I am trying to run as Administrator %1 %2 ver|find "5."&g ...
- yum 与 apt 的对比
一.概念 使用yum/apt之前,你很可能会遇到配置源(ubuntu下一般内置的就比较好,所以可能很少人手动配置),那这个源是什么呢,就是告诉apt/yum,安装软件的时候你要从哪里下载.比如你使用1 ...
- workerman程序调试
现象1 启动后报错类似如下: php start.php start PHP Warning: stream_socket_server(): unable to connect to tcp://x ...
- yii2 rules验证规则,ajax验证手机号码是否唯一
<?php namespace frontend\models; use Yii; use yii\base\Model; /** * Signup form */ class SignupFo ...
- html5-fieldset和legend和keygen元素的用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Object-C-Foundation-反射
主要方法和类型 Class 变量名 = [类或者对象 class]; Class 变量名 = [类或者对象 superclass]; Class 变量名 = NSClassFromString(方 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列1-基本概念和架构
前段时间研究了Window Azure ServiceBus Messaging消息队列技术,搞了很多技术研究和代码验证,最近准备总结一下,分享给大家. 首先,Windows Azure提供了两种类型 ...
- 一 django框架?
Django-1 一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞 ...
- linux常用命令:mkdir 命令
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...