XXX on tree
%了发树上莫队
nlognsqrt(n)
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9')c = getchar();
while(c <= '9' && c >= '0')x = x * 10 + c - '0',c= getchar();
return x * f;
}
const int maxn = 100007;
int n ,block,fa[maxn],siz[maxn],son[maxn],num = 0,a[maxn],out[maxn];
vector<int>v[maxn];
int dfn[maxn];
void dfs(int x,int f) {
fa[x] = f; siz[x] = 1;
dfn[x] = ++ num;
for(int i = 0;i < v[x].size();++ i) {
int V = v[x][i];
if(siz[V]) continue;
dfs(V,x);
siz[x] += siz[V];
if(siz[V] > siz[son[x]]) son[x] = V;
}
}
int happen[maxn],belong[maxn];
int ans = 0,totq = 0;
struct Query {
int l,r,id,ans;
Query(int L = 0,int R = 0,int Id = 0,int Ans = 0): l(L) , r(R), id(Id),ans(Ans) {};
bool operator < (const Query &a) const {
return belong[l] == belong[a.l] ? r < a.r : belong[l] < belong[a.l];
}
}q[maxn];
void delet(int x) {
if(-- happen[x] == 0) ans --;
}
void add(int x) {
if(++ happen[x] == 1) ans ++;
}
void mo() {
sort(q + 1,q + totq + 1);
int l = 1,r = 0,fuck = 0;
for(int i = 1;i <= totq;++ i) {
while(l < q[i].l) delet(a[l ++]);
while(l > q[i].l) add(a[-- l]);
while(r < q[i].r) add(a[++ r]);
while(r > q[i].r) delet(a[r --]);
q[i].ans = ans;
}
for(int i = 1;i <= totq;++ i)
out[q[i].id] = q[i].ans;
}
int que,ask[maxn];
void dealask() {
que = read();
for(int i = 1;i <= que;++ i) {
ask[i] = read();
for(int j = 0;j < v[ask[i]].size();++ j) {
int V = v[ask[i]][j];
if(V == fa[ask[i]]) continue;
q[++ totq] = Query(dfn[V],dfn[V] + siz[V] - 1,V,0);
}
}
}
int data[maxn];
int main() {
n = read();
block = sqrt(n);
for(int i = 1;i <= n;++ i) a[i] = data[i] = read(), belong[i] = i / block + 1;
sort(data + 1,data + n + 1);
num = unique(data + 1,data + n + 1) - data - 1;
for(int i = 1;i <= n;++ i) a[i] = lower_bound(data + 1,data + num + 1,a[i]) - data;
for(int u,V,i = 1;i < n;++ i) {
u = read(),V = read() ;
v[u].push_back(V);
v[V].push_back(u);
}
num = 0;
dfs(1,0);
dealask();
mo();
for(int i = 1;i <= que;++ i) {
int mx = 0,id = 0;
for(int j = 0 ;j < v[ask[i]].size(); ++ j) {
int to = v[ask[i]][j];
if(to == fa[ask[i]]) continue;
if(out[to] > mx || (siz[to] > siz[id] && out[to] == mx)) mx = out[to];
} printf("%d\n",siz[id]);
}
return 0;
}
XXX on tree的更多相关文章
- paip.tree 生成目录树到txt后的折叠查看
paip.tree 生成目录树到txt后的折叠查看 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.ne ...
- easyui之combotree
这几天时间比较空闲的我把easyui中比较难的控件回顾一遍 这次的总结是easyui中的combotree easyui的中文文档上说:combotree结合选择控制和下拉树,类似于combobox只 ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 混合开发 Hybird Cordova PhoneGap web 跨平台 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- docker 镜像详解
镜像的大小不等于通过docker images 看到的每个镜像大小的合集,docker镜像采用了分层的机制.上层使用共同下层,各自不同部门构建各自的独立分层. docker的镜像通过联合文件系统(un ...
- hdu KiKi's K-Number 主席树
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Go语言环境安装详细介绍
工具链介绍 go有两套编译工具链,分别是从plant9移植过来的gc和依赖gcc的gccgo. 官方为gc工具链提供了二进制安装包和源码, 可以根据需要选择一种安装方式.gc工具链对操作系统和CPU类 ...
- Git下载GitHub仓库里的某一个文件夹或某一个文件
从Github上下载github上的整个项目,可以用下面指令: git clone https://github.com/XXX/xxxxx.git 其中:XXX是用户在Github上的用户名 xxx ...
随机推荐
- *p++、(*p)++、*++p、++*p 的区别
int a[5]={1,2,3,4,5};int *p = a; *p++ 先取指针p指向的值(数组第一个元素1),再将指针p自增1: cout << *p++; // 结果为 1 cou ...
- SPOJ 8222 NSUBSTR - Substrings
http://www.spoj.com/problems/NSUBSTR/ 题意: F(x)定义为字符串S中所有长度为x的子串重复出现的最大次数 输出F[1]~F[len(S)] 用字符串S构建后缀自 ...
- bzoj千题计划260:bzoj2940: [Poi2000]条纹
http://www.lydsy.com/JudgeOnline/problem.php?id=2940 SG 博弈入门推荐张一飞的<由感性认识到理性认识 ——透析一类搏弈游戏的解答过程> ...
- LaTeX符号和图片
\documentclass{article} \usepackage{ctex} %中文处理 \begin{document} \section{空白符号} Are you wiser than o ...
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
- BFS简单题套路_Codevs 1215 迷宫
BFS 简单题套路 1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明 ; struct Status { int r, c; Status(, ) : r(r), c(c) {} // ...
- Excel VBA保护工作表
'设定可编辑区域 ActiveSheet.Protection.AllowEditRanges.Add Title:="区域1", Range:=Range("E5:H1 ...
- [转载]Supporting OData $inlinecount with the new Web API OData preview package
http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-pac ...
- SQL Server修改默认端口号1433
方法1: 1) SqlServer服务使用两个端口:TCP-1433.UDP-1434. 其中1433用于供SqlServer对外提供服务,1434用于向请求者返回SqlServer使用了那个TCP/ ...
- Spring bean 配置
1.传统的创建对象的方式:JedisMall tardition=new JedisMall(); 这样是在程序运行时创建,表示当前模块已经不知不觉和new出的对象耦合了,而我们通常都是更高层次的抽象 ...