SPOJ:COT2 Count on a tree II
题意
给定一个n个节点的树,每个节点表示一个整数,问u到v的路径上有多少个不同的整数。
n=40000,m=100000
Sol
树上莫队模板题
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
const int _(1e5 + 5);
typedef long long ll;
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, m, first[_], cnt, w[_], o[_], len;
int dfn[_], st[20][_], lg[_], deep[_], idx, fa[_];
int S[_], bl[_], blo, num, ans[_], sum[_], vis[_], Ans;
struct Edge{
int to, next;
} edge[_];
struct Query{
int l, r, id;
IL int operator <(RG Query B) const{
return bl[l] == bl[B.l] ? dfn[r] < dfn[B.r] : bl[l] < bl[B.l];
}
} qry[_];
IL void Add(RG int u, RG int v){
edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}
IL void Dfs(RG int u){
dfn[u] = ++idx, st[0][idx] = u; RG int l = S[0];
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(dfn[v]) continue;
deep[v] = deep[u] + 1, fa[v] = u;
Dfs(v);
if(S[0] - l >= blo) for(++num; S[0] != l; --S[0]) bl[S[S[0]]] = num;
st[0][++idx] = u;
}
S[++S[0]] = u;
}
IL void Chk(RG int &x, RG int u, RG int v){
x = deep[u] < deep[v] ? u : v;
}
IL int LCA(RG int u, RG int v){
u = dfn[u], v = dfn[v];
if(u > v) swap(u, v);
RG int log2 = lg[v - u + 1], t;
Chk(t, st[log2][u], st[log2][v - (1 << log2) + 1]);
return t;
}
IL void Update(RG int x){
if(vis[x]) --sum[w[x]], Ans -= (!sum[w[x]]);
else Ans += (!sum[w[x]]), ++sum[w[x]];
vis[x] ^= 1;
}
IL void Modify(RG int u, RG int v){
while(u != v){
if(deep[u] > deep[v]) swap(u, v);
Update(v), v = fa[v];
}
}
int main(RG int argc, RG char* argv[]){
len = n = Input(), m = Input(), blo = sqrt(n);
for(RG int i = 1; i <= n; ++i) o[i] = w[i] = Input(), first[i] = -1;
sort(o + 1, o + len + 1), len = unique(o + 1, o + len + 1) - o - 1;
for(RG int i = 1; i <= n; ++i) w[i] = lower_bound(o + 1, o + len + 1, w[i]) - o;
for(RG int i = 1, u, v; i < n; ++i)
u = Input(), v = Input(), Add(u, v), Add(v, u);
Dfs(1);
if(S[0]) for(++num; S[0]; --S[0]) bl[S[S[0]]] = num;
for(RG int i = 2; i <= idx; ++i) lg[i] = lg[i >> 1] + 1;
for(RG int j = 1; j <= lg[idx]; ++j)
for(RG int i = 1; i + (1 << j) - 1 <= idx; ++i)
Chk(st[j][i], st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
for(RG int i = 1; i <= m; ++i){
qry[i] = (Query){Input(), Input(), i};
if(dfn[qry[i].l] > dfn[qry[i].r]) swap(qry[i].l, qry[i].r);
}
sort(qry + 1, qry + m + 1);
RG int lca = LCA(qry[1].l, qry[1].r);
Modify(qry[1].l, qry[1].r);
Update(lca), ans[qry[1].id] = Ans, Update(lca);
for(RG int i = 2; i <= m; ++i){
Modify(qry[i - 1].l, qry[i].l), Modify(qry[i - 1].r, qry[i].r);
lca = LCA(qry[i].l, qry[i].r);
Update(lca), ans[qry[i].id] = Ans, Update(lca);
}
for(RG int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
return 0;
}
SPOJ:COT2 Count on a tree II的更多相关文章
- SPOJ 10707 COT2 - Count on a tree II
思路 树上莫队的题目 每次更新(u1,u2)和(v1,v2)(不包括lca)的路径,最后单独统计LCA即可 代码 #include <cstdio> #include <cstrin ...
- 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 ...
- 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 ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 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 树上莫队算法
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...
- SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)
题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...
随机推荐
- Mac下显示和隐藏“隐藏文件”
命令行方式:显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写): 显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFi ...
- 【C/C++】10分钟教你用C++写一个贪吃蛇附带AI功能(附源代码详解和下载)
C++编写贪吃蛇小游戏快速入门 刚学完C++.一时兴起,就花几天时间手动做了个贪吃蛇,后来觉得不过瘾,于是又加入了AI功能.希望大家Enjoy It. 效果图示 AI模式演示 imageimage 整 ...
- 如何调用另一个包中的Application
在项目中要集成Xabber,将它作为一个Lib, ..... Xabber 原有代码 /** * Base entry point. * * @author alexander.ivanov */ p ...
- 运算符重载入门demo
#include "pch.h" #include <iostream> using namespace std; class A { public: A(int x, ...
- JavaScript实现自定义alert弹框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO
- es xxx_by_query
xxx_by_query包括_delete_by_query和_update_by_query,下面分开讲 _delete_by_query 相当于sql中的delete from a where . ...
- Java框架-mybatis02基本的crud操作
1.搭建mybatis框架 1)导入相关jar包 2)编写核心配置文件(配置数据库连接的相关信息以及配置mapper映射文件) 3)编写dao操作 4)编写mapper映射文件 5)编写实体类 2.执 ...
- VS2010,VS2012,VS2015等的自动提示不能默认选中的功能解决办法
很简单,只需要按 ctrl+alt+space 即可切换. 蛋疼,我到底为什么总会不小心切换过去,而且每次都记不住这个快捷键切换回来...
- 高效的JS数组操作
1.向数组的末尾添加元素 var arr=[1,2,3]; arr[arr.length]=1; 2.向数组的头部添加元素 var arr=[1,2,3]; [0].concat(arr); 3.向数 ...
- n皇后问题(分析)
这道题需要用到回溯算法,现在在这里先简单的介绍一下这个算法: 回溯算法也叫试探法,它是一种系统地搜索问题的解的方法.回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试.用回溯 ...