题目分析:

考虑欧拉序,这里的欧拉序与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. Redis系列文章总结:ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

    引言:最近回头看了看开发的.Net Core 2.1项目的复盘总结,其中在多处用到Redis实现的分布式锁,虽然在OnResultExecuting方法中做了防止死锁的处理,但在某些场景下还是会发生死 ...

  2. Fastreport.net 如何在开发MVC应用程序时使用报表

    当你使用MVC模板创建自己的Web项目,会出现一个合理的问题 - 如何在其中使用FastReport.Net Web报表? 在这篇文章中,我会为你演示如何做到这一点. 由于在MVC体系结构中,视图与逻 ...

  3. Vs2017_创建项目引用Core2.2报错找不到

    错误: 解决方案: 这个勾一定要画上

  4. [Offer收割]编程练习赛97

    链接 [https://hihocoder.com/contest/offers97/problems] 题意 题目1 : 放置矩形 时间限制:10000ms 单点时限:1000ms 内存限制:256 ...

  5. 数据快速批量添加到Elasticsearch

    如何把数据快速批量添加到Elasticsearch中 问题来源 最近新做一个项目,有部分搜索比较频繁的数据,而且量级比较大,预计一两年时间很可能达到100G,项目要求不要存在数据库中,最终出来有两个方 ...

  6. [已解决]关于python无法显示中文的问题:SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no encoding declared。

    想在python代码中输出汉字.但是老是出现SyntaxError: Non-ASCII character '\xe4' in file test.py on line , but no encod ...

  7. ntpd、ntpdate、hwclock的区别

    hwclock --systohc 使用ntpdate更新系统时间 - 潜龙勿用 - CSDN博客https://blog.csdn.net/suer0101/article/details/7868 ...

  8. Windows BAT 命令下del 与 rd 命令

    https://blog.csdn.net/jigetage/article/details/81180757 RD 与 DEL 命令 windows bat 目录和文件的删除处理. 命令:RD,删除 ...

  9. [转帖]ulimit、limits.conf、sysctl和proc文件系统

    ulimit.limits.conf.sysctl和proc文件系统 来源:https://blog.csdn.net/weixin_33918114/article/details/86882372 ...

  10. Django--CRM

    一 . 什么是CRM CRM就是客户关系管理系统(customer relationship management) 二 . 用户登录 # models.py文件 class UserProfile( ...