「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II
传送门
树上莫队板子题。
锻炼基础,没什么好说的。
参考代码:
#include <algorithm>
#include <cstdio>
#include <cmath>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 40005, __ = 1e5 + 5;
int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1];
inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }; head[u] = tot; }
int n, q, a[_], X0, X[_];
int fir[_], las[_], vis[_], dep[_], fa[17][_];
int len, ord[_ << 1], m, pos[_ << 1];
int ans, cnt[_], res[__];
struct node { int l, r, lca, id; } t[__];
inline bool cmp(const node& x, const node& y)
{ return pos[x.l] != pos[y.l] ? pos[x.l] < pos[y.l] : ((pos[x.l] & 1) ? x.r < y.r : y.r < x.r); }
inline void dfs(int u, int f) {
fir[u] = ++len, ord[len] = u;
dep[u] = dep[f] + 1, fa[0][u] = f;
for (rg int i = 1; i <= 16; ++i) fa[i][u] = fa[i - 1][fa[i - 1][u]];
for (rg int i = head[u]; i; i = edge[i].nxt) if (edge[i].ver != f) dfs(edge[i].ver, u);
las[u] = ++len, ord[len] = u;
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (rg int i = 16; ~i; --i) if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
if (x == y) return x;
for (rg int i = 16; ~i; --i) if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
return fa[0][x];
}
inline void calc(int x) { vis[x] ? ans -= !--cnt[a[x]] : ans += !cnt[a[x]]++, vis[x] ^= 1; }
int main() {
read(n), read(q);
for (rg int i = 1; i <= n; ++i) read(a[i]), X[i] = a[i];
sort(X + 1, X + n + 1);
X0 = unique(X + 1, X + n + 1) - X - 1;
for (rg int i = 1; i <= n; ++i) a[i] = lower_bound(X + 1, X + X0 + 1, a[i]) - X;
for (rg int x, y, i = 1; i < n; ++i) read(x), read(y), Add_edge(x, y), Add_edge(y, x);
dfs(1, 0);
for (rg int x, y, lca, i = 1; i <= q; ++i) {
read(x), read(y), lca = LCA(x, y);
if (fir[x] > fir[y]) swap(x, y);
if (x == lca)
t[i].l = fir[x], t[i].r = fir[y], t[i].lca = 0;
else
t[i].l = las[x], t[i].r = fir[y], t[i].lca = lca;
t[i].id = i;
}
m = sqrt(1.0 * len);
for (rg int i = 1; i <= len; ++i) pos[i] = (i - 1) / m + 1;
sort(t + 1, t + q + 1, cmp);
for (rg int l = 1, r = 0, i = 1; i <= q; ++i) {
while (l > t[i].l) calc(ord[--l]);
while (r < t[i].r) calc(ord[++r]);
while (l < t[i].l) calc(ord[l++]);
while (r > t[i].r) calc(ord[r--]);
if (t[i].lca) calc(t[i].lca);
res[t[i].id] = ans;
if (t[i].lca) calc(t[i].lca);
}
for (rg int i = 1; i <= q; ++i) printf("%d\n", res[i]);
return 0;
}
「SPOJ10707」Count on a tree II的更多相关文章
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- SPOJ10707 COT2 - Count on a tree II 【树上莫队】
题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以 ...
- 【SPOJ10707】 COT2 Count on a tree II
SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...
- 【BZOJ2589】[SPOJ10707]Count on a tree II
[BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 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 ...
- AC日记——Count on a tree II spoj
Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...
- 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 ...
随机推荐
- 1、json背景
教程链接 链接:http://pan.baidu.com/s/1mil4M1M 密码:vwsn JSON (JavaScript对象表示法)是一种轻量级的基于文本的开放标准,被设计用于可读的数据交换, ...
- SpringCloud全家桶学习之断路器---Hystrix(五)
目前我也在摸索着学习Spring Cloud,本节主要摸索的是服务熔断.服务降级.Hystrix服务监控. 一.Hystrix概述 (1)服务雪崩 服务雪崩:多个微服务之间调用的时候,假设微服务A调用 ...
- Mysql 中使用 utfmb4 需要注意的问题
查资料时看到一个前人的经验总结,非常有用: http://seanlook.com/2016/10/23/mysql-utf8mb4/
- beyond compare 用法
1.过滤器用法:点击小眼睛(回话-->回话设置)打开过滤器界面----设置多个过滤文件或目录直接使用回车键 2.比较时最好先设置编码以防出现乱码问题 工具-->文件格式. 3.比较时出现乱 ...
- 洛谷 P3808 【模板】AC自动机(简单版) (AC自动机优化板子)
题中有一个坑点,就是模式串可以相同,并且全部计数. #include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; ...
- Nexus-配置vPC 实验三
配置EvPC(增强的vPC),下面两个FEX可以同时被两个N5K管理.注意:FEX只支持静态的Channel-group(mode on) N5K-1配置:配置FEXN5K-1(config)#fea ...
- Java 石家庄铁道大学软件工程系 学生学籍管理系统 2019 版
本系统的作用是简单实现一些学生成绩管理操作:录入,修改,绩点计算,以及系统退出等. 首先建一个主函数实现界面的实现,然后建一个数据类用来定义存放数据.之后建立一个工具类,用来实现所有要进行的操作.首先 ...
- C++11常用特性介绍——decltype关键字
一.decltype的意义 有时我们只想从表达式的类型推断出要定义的变量类型,但是不想用其值进行初始化的时候,C++11新标准引入了decltype类型说明符,它的作用是选择并返回操作数的数据类型,在 ...
- linux mysql 查看数据库大小
SELECT CONCAT(TRUNCATE(SUM(data_length)//,),'MB') AS data_size, CONCAT(TRUNCATE(SUM(max_data_length) ...
- 区分git ,github,github桌面版,gitbash、gitshell
推荐链接:https://www.runoob.com/git/git-tutorial.html https://www.zhihu.com/question/34582452?sort=c ...