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 ...
随机推荐
- 标准误(Standard Error)
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- Hadoop生态圈-Azkaban实战之Command类型执行指定脚本
Hadoop生态圈-Azkaban实战之Command类型执行指定脚本 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.服务端测试代码(别忘记添加权限哟!) [yinzh ...
- Dubbo学习笔记10:Dubbo服务消费方启动流程源码分析
同理我们看下服务消费端启动流程时序图: 在<Dubbo整体架构分析>一文中,我们提到服务消费方需要使用ReferenceConfig API来消费服务,具体是调用代码(1)get()方法来 ...
- Lua程序设计(一)面向对象概念介绍
完整代码 local mt = {} mt.__add = function(t1,t2) print("两个Table 相加的时候会调用我") end local t1 = {} ...
- javascript沙箱模式
沙箱模式解决了命名空间模式的如下几个缺点: 1.对单个全局变量的依赖变成了应用程序的全局变量依赖.在命名空间模式中,是没有办法使同一个应用程序或库的2个版本运行在同一个页面中.2.对这种以点分割的名字 ...
- shell 流程结构
if 判断语句 if [ $a == $b ] then echo "等于" else echo "不等于" fi case分支选择 case $xs in ) ...
- Gitlab的安装汉化及问题解决
Gitlab的安装汉化及问题解决(2017/12/14目前版本为10.2.4) 一.前言 Gitlab需要安装的包太TM多了,源码安装能愁死个人,一直出错,后来发现几行命令就装的真是遇到的新大陆一样. ...
- Nginx报错:upstream timed out (110: Connection timed out)和client intended to send too large body【转】
nginx日志报错 2018/01/26 16:36:49 [error] 23327#0: *54953 upstream timed out (110: Connection timed out) ...
- 网络抓包神器-Charles使用指南
http://blog.csdn.net/liulanghk/article/details/46342205 目录 概述 安装 显示模式 PC端抓包 移动应用抓包 其他技能 charles使用问题汇 ...
- Kotlin 语言下设计模式的不同实现
偶然在 Github 上看到 dbacinski 写的 Kotlin 语言下设计模式的不同实现(这里的不同是相对于 Java 语言的),有些实现非常好,但是有些实现的例子不是很赞同.所以自己写了 Ko ...