链接

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 莫队的更多相关文章

  1. SP10707 COT2 - Count on a tree II 莫队上树

    题意:求一条链 \((u,v)\) 上不同的颜色数. 我们可以求出树的出栈入栈序(or 括号序?我也不确定). 图(from attack) 然后有一个很优美的性质: 设点 \(u\) 的入栈时间为 ...

  2. SP10707 COT2 - Count on a tree II (树上莫队)

    大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...

  3. SP10707 COT2 - Count on a tree II [树上莫队学习笔记]

    树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...

  4. [SP10707]COT2 - Count on a tree II

    题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序 ...

  5. 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  ...

  6. 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 ...

  7. 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 ...

  8. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  9. 【树上莫队】【SP10707】 COT2 - Count on a tree II

    Description 给定一棵 \(n\) 个点的树,每个节点有一个权值,\(m\) 次询问,每次查询两点间路径上有多少不同的权值 Input 第一行是 \(n\) 和 \(m\) 第二行是 \(n ...

随机推荐

  1. 001- CreateProcess failed with error 216 (no message available)错误详解

    问题详解 runnerw.exe: CreateProcess failed with error 216 (no message available) 看描述,创建进程失败,应该是main这个入口文 ...

  2. http与https区别

    1.安全. 内容采用对称加密,身份认证.建立一个信息安全通道来保证数据传输的安全.采用对称加密算法,来加密真实传输的数据.采用非对称加密算法(公钥和私钥),来保证连接的安全性.防止内容被第三方冒充和篡 ...

  3. [1]传奇3服务器源码分析一 LoginGate

    服务端下载地址: 点击这里 网上基本上都有分析该源码的分析详解,如:请点击该链接,但容易晕,而且也不全!所以才有了本文! 一.首先来看服务端的LoginGate源码 先来张图比较让人容易理解

  4. jQuery事件--mouseover()、mouseout()、mouseenter()和mouseleave()

       mouseover([[data],fn]) 概述 当鼠标指针位于元素上方时,会发生 mouseover 事件.该事件大多数时候会与 mouseout 事件一起使用 注释:与 mouseente ...

  5. RabbitMQ生产者消费者

    package com.ra.car.rabbitMQ; import java.io.IOException; import java.util.HashMap; import java.util. ...

  6. Second LearningConvolutionalNeuralNetworksforGraphs Experience

    paper +ppt 链接:https://pan.baidu.com/s/1ZLBvv7mP8OoseQ4tnwhr_A 提取码:4amg

  7. flask 在视图函数中验证表单

    在视图函数中验证表单 因为现在的basic_form视图同时接受两种类型的请求:GET请求和POST请求.所以我们要根据请求方法的不同执行不同的代码.具体来说,首先是实例化表单,如果是GET请求,就渲 ...

  8. 开始Nginx的SSL模块

    nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/n ...

  9. urlopen和urlretrieve

    import urllib import re url = "https://www.duitang.com/search/?kw=%E9%AC%BC%E6%80%AA&type=f ...

  10. Symfony2 学习笔记之服务容器

    现在的PHP应用程序都是面向对象开发,所以主要是由对象构成.有的对象可以方便的分发邮件信息而有的可能帮你把信息写入到数据库中.在你的应用程序中,你可能创建一个对象用于管理你的产品库存,或者另外一个对象 ...