SPOJ 10707 COT2 - Count on a tree II
思路
树上莫队的题目
每次更新(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的更多相关文章
- bzoj2589【 Spoj 10707】 Count on a tree II
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor lastans和v这两个节点间有多少种不同的点权.其中lastans是上一个询问的答案,初始为0,即第一 ...
- SPOJ:COT2 Count on a tree II
题意 给定一个n个节点的树,每个节点表示一个整数,问u到v的路径上有多少个不同的整数. n=40000,m=100000 Sol 树上莫队模板题 # include <bits/stdc++.h ...
- 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 ...
- 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 ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 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(树上莫队)
题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...
- SPOJ COT2 Count on a tree II (树上莫队)
题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...
- spoj COT2 - Count on a tree II 树上莫队
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
随机推荐
- outlook寻找/删除指定日期范围内的邮件
总是收到很多系统预警邮件,时间久了攒了好多垃圾邮件.实际上只需保存近期预警邮件,之前的完全可以删除. 上网找了一圈也没找到方法,然后自己想到了一种,步骤如下: 使用outlook规则,将指定日期范围内 ...
- navicat破解版的下载与激活
原文链接:http://www.cnblogs.com/djwhome/p/9289295.html 以前一直使用的老版的破解版的navicat,但是最近老是报错 而且连接还特别慢,今天终于不忙了额, ...
- ssh出现公钥错误问题的解决方法
问题:主机app1推送公钥时,公钥判定错误 原因:之前推过公钥,用的是ip而不是主机名(即hosts文件中的对应关系不对),导致app1的~/.ssh/known_hosts中的公钥对不上. ...
- centos7:ssh免密登陆设置及常见错误
目录 一.免密登录设置 二.常见错误 三.CentOS7再ssh-copy-id时的错误 一.免密登录设置 1.使用root用户登录,进入到目录/root/.ssh 2.执行命令:ssh-keygen ...
- LRU算法简介
LRU是什么? 按照英文的直接原义就是Least Recently Used,最近最久未使用法,它是按照一个非常注明的计算机操作系统基础理论得来的:最近使用的页面数据会在未来一段时期内仍然被使用,已经 ...
- Neo4j
Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做 ...
- Selenium工具爬取商品
selenium是一个优秀的自动化测试工具,支持多种语言,具体介绍参考官方文档:https://www.seleniumhq.org/docs/. 下面我们使用selenium工具模拟用户点击商品详情 ...
- SpringBoot热启动让开发更便捷
在开发过程中,当写完一个功能我们需要运行应用程序测试,可能这个小功能中存在多个小bug,我们需要改正后重启服务器,这无形之中拖慢了开发的速度增加了开发时间,SpringBoot提供了spring-bo ...
- oracle用户解锁,rename管理
---查看命令:用户默认表空间 SYS@ACE >select username,default_tablespace,temporary_tablespace,created from dba ...
- 怎样获取所有style节点
通过 document.styleSheets 获取所有的样式表节点. document.styleSheets instanceof StyleSheetList; // true 注意: 1. 返 ...