思路

树上莫队的题目

每次更新(u1,u2)和(v1,v2)(不包括lca)的路径,最后单独统计LCA即可

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;
int v[100100*2],fir[100100],nxt[100100*2],cnt=0,w_p[100100],b[100100],n,m,tx,sum,jump[100100][20],dep[100100],in_ans[100100],sz,belong[100100],block_cnt,ans[100100],color[100100],U,V;
stack<int> S;
void addedge(int ui,int vi){
++cnt;
v[cnt]=vi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
void dfs(int u,int f){
jump[u][0]=f;
for(int i=1;i<20;i++)
jump[u][i]=jump[jump[u][i-1]][i-1];
dep[u]=dep[f]+1;
int t=S.size();
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==f)
continue;
dfs(v[i],u);
if(S.size()-t>=sz){
++block_cnt;
while(S.size()>t){
belong[S.top()]=block_cnt;
S.pop();
}
}
}
S.push(u);
}
void init(void){
sz=sqrt(n);
dfs(1,0);
}
int lca(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
for(int i=19;i>=0;i--)
if(dep[jump[x][i]]>=dep[y])
x=jump[x][i];
if(x==y)
return x;
for(int i=19;i>=0;i--)
if(jump[x][i]!=jump[y][i])
x=jump[x][i],y=jump[y][i];
return jump[x][0];
}
void modi_point(int x){
if(in_ans[x]){//erase
color[w_p[x]]--;
if(!color[w_p[x]])
sum--;
}
else{
if(!color[w_p[x]])
sum++;
color[w_p[x]]++;
}
in_ans[x]^=1;
}
void move_path(int x,int y){//(x,y) except LCA(x,y)
if(dep[x]<dep[y])
swap(x,y);
while(dep[x]>dep[y]){
modi_point(x);
x=jump[x][0];
}
while(x!=y){
modi_point(x);
modi_point(y);
x=jump[x][0];
y=jump[y][0];
}
}
struct Query{
int u,v,id;
bool operator < (const Query &b) const{
return (belong[u]==belong[b.u])?belong[v]<belong[b.v]:belong[u]<belong[b.u];
}
}Q[100100];
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w_p[i]),b[i]=w_p[i];
sort(b+1,b+n+1);
tx=unique(b+1,b+n+1)-(b+1);
for(int i=1;i<=n;i++)
w_p[i]=lower_bound(b+1,b+n+1,w_p[i])-b;
for(int i=1;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(a,b);
addedge(b,a);
}
init();
for(int i=1;i<=m;i++){
scanf("%d %d",&Q[i].u,&Q[i].v);
Q[i].id=i;
}
sort(Q+1,Q+m+1);
U=V=1;
for(int i=1;i<=m;i++){
move_path(U,Q[i].u);
move_path(V,Q[i].v);
U=Q[i].u;
V=Q[i].v;
int Lca=lca(Q[i].u,Q[i].v);
modi_point(Lca);
ans[Q[i].id]=sum;
modi_point(Lca);
}
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
return 0;
}

SPOJ 10707 COT2 - Count on a tree II的更多相关文章

  1. bzoj2589【 Spoj 10707】 Count on a tree II

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor lastans和v这两个节点间有多少种不同的点权.其中lastans是上一个询问的答案,初始为0,即第一 ...

  2. SPOJ:COT2 Count on a tree II

    题意 给定一个n个节点的树,每个节点表示一个整数,问u到v的路径上有多少个不同的整数. n=40000,m=100000 Sol 树上莫队模板题 # include <bits/stdc++.h ...

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

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

  5. 【SPOJ10707】 COT2 Count on a tree II

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

  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(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

  8. SPOJ COT2 Count on a tree II (树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

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

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

随机推荐

  1. 最新 巨人网络java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.巨人网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了巨人网络.6.7月主要是做系统复习.项目复盘.Leet ...

  2. 如何运行spring boot 工程

    1.右键工程,Run As, Maven install, 2.右键工程,Run As,Spring Boot App 3.在地址栏输入127.0.0.1:8080 动图示例

  3. SQL 查询建表SQL

    1.新建一个查询语句,按执行按钮 2.结果页面会显示一条sql语句,复制该语句即可建表 3.建表测试

  4. CMake速记

    目录 CMake速记 我的demo 外部构建 基础语法 常用指令 环境变量 一些变量 参考文档 title: CMake速记 date: 2019/11/18 19:17:40 toc: true - ...

  5. 编写一个自定义事件类,包含on/off/emit/once方法

    function Event() { this._events = {}; } Event.prototype.on = function(type, fn) { if (!this._events[ ...

  6. Java编程思想(三)控制程序流程

    3.1.10逗号运算符 我们可以使用一系列由逗号分隔的语句,而且哪些语句均会独立执行. 3.1.15复习计算顺序

  7. sshpass ssh登录时自动输入密码

    安装 直接安装 sudo apt-get install sshpass 源代码安装 wget http://sourceforge.net/projects/sshpass/files/sshpas ...

  8. DP单调队列--斜率优化P3195

    题意:https://www.luogu.com.cn/problem/P3195 思路:https://www.luogu.com.cn/problemnew/solution/P3195 #def ...

  9. pthread_cond_t

    条件锁pthread_cond_t (1)pthread_cond_wait的使用 等待线程1. 使用pthread_cond_wait前要先加锁2. pthread_cond_wait内部会解锁,然 ...

  10. shell习题第17题:检测磁盘

    [题目要求] 写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时候,发邮件提醒 [核心要点] df d ...