题目分析:

考虑欧拉序,这里的欧拉序与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 【树上莫队】的更多相关文章

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

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  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. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  5. [SPOJ]Count on a tree II(树上莫队)

    树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #incl ...

  6. 【SPOJ10707】 COT2 Count on a tree II

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

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

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

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

随机推荐

  1. NODE 模块 FS-EXTRA

    fs-extra模块是系统fs模块的扩展,提供了更多便利的 API,并继承了fs模块的 API. 1.复制文件 copy(src, dest, [options], callback) 示例: var ...

  2. Jq相关常用操作

    1.select下拉列表操作 $(".kstitle").live('change', function () { var workType = $(this).val(); // ...

  3. H5 27-优先级之important

    27-优先级之important 我是段落 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  4. Python学习第五篇——如何访问字典

    # the example_1 aim to tell how to use dctionary,and how to access list or dictionary infos={"f ...

  5. p68理想的性质

    1.如何由2.2.4推出后面的结论? 2.为什么A可以等于R? 3.如何证明3? π:R->R/M套用定理2.2.4(2)和(1) R2是R/M,I是R/M的理想也就是R2的理想,所以f^(-1 ...

  6. Leaf——美团点评分布式ID生成系统 UUID & 类snowflake

    Leaf——美团点评分布式ID生成系统 https://tech.meituan.com/MT_Leaf.html

  7. Hibernate two table same id

    Hibernate更新数据(不用update也可以) - 森林木马 - 博客园 https://www.cnblogs.com/owenma/p/3481497.html hibernate级联更新会 ...

  8. 【翻译】FluentValidation验证组件的使用

    由于本文是翻译,所以将原文原原本本的搬上来,大家看原文有什么不懂的也可以对照这里. 给出地址:https://fluentvalidation.net/ FluentValidation fluent ...

  9. Day 3-6 生成器&迭代器

    ---恢复内容开始--- 列表生成式: list = [i*i for i in range(20)] # 这就是一个列表生成式 print(list) # [0, 1, 4, 9, 16, 25, ...

  10. 剑指offer(14)

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 思路: 这里有个细节,我们发现,6节点的子节点在操作之后并没有发生变化,所以等会我们在交换的时候,交换的不是节点的数值,而是整个节点. 另外我们进 ...