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 < ...
随机推荐
- centOS 安装 npm
下载 cd /usr/local/node wget https://npm.taobao.org/mirrors/node/v10.14.1/node-v10.14.1-linux-x64.tar. ...
- SQL FIND_IN_SET() 判断某一个数是否存在于数据表某个以逗号分隔开字段数据中
数据表中的字段存储的是以逗号分隔开的字符串, 例如 (1,2,6,8) 以前不知道这个用法, 碰到比如 8 是否包含在改字符串里面只能一个个取出来, 然后解析成数组,再判断是否在该数组中,效率极低: ...
- Encoding.GetEncoding 编码列表
代码页 名称 显示名称 37 IBM037 IBM EBCDIC(美国 - 加拿大) 437 IBM437 OEM 美国 500 IBM500 IBM EBCDIC(国际) 708 A ...
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
- 《MIT 6.828 Lab 1 Exercise 4》实验报告
本实验链接:mit 6.828 lab1 Exercise 4. 题目 Exercise 4. Read about programming with pointers in C. The best ...
- [转帖CCIX]
业界七巨头联手,数据中心通过PCIe实现25Gbps数据通信! 2017-06-07 17:31 CCIX(Cache Coherent Interconnect for Accelerators,针 ...
- 运行servlet跳转页面变成了下载界面,或者中文乱码
1.是这个地方的问题,敲错了Setvlet不能识别HTML文件,所以变成了下载.2.这个也是防止中文乱码 //设置响应内容类型response.setContentType("text/ht ...
- Pycharm 误删文件夹
在Linux下操作时误删除了Pycharm项目中的文件夹,打开垃圾桶,居然找不到,立马上网查Linux下怎么恢复文件, 冷静一下,不是还有个Ctrl + Z吗,对着Pycharm 文件浏览器 按一下, ...
- DP_Wooden Sticks
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st ...
- Photon Server初识(四) --- 部署自己的服务Photon Server
准备工作: 1.一台 window 虚拟机(本机是window也行) 2.下载SDK : https://www.photonengine.com/zh-CN/sdks#server 一:SDK介绍 ...