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 ...
随机推荐
- xen list_domains stat 解析
XenServer中可以使用list_domains命令来查看所有VM以及Dom0的运行状态以及简单的资源消耗,如下: [root@xenserver ~]# list_domains id | uu ...
- python---django中模板渲染(csrf令牌使用,自定义模板函数)
使用终端,可以更方便的去实验,但是没有提示信息: 在项目目录下: D:\MyPython\day23\HelloWorld>python manage.py shell 开始实验: >&g ...
- vue ssr服务端渲染
SSR:Server Side Rendering(服务端渲染) 目的是为了解决单页面应用的 SEO 的问题,对于一般网站影响不大,但是对于论坛类,内容类网站来说是致命的,搜索引擎无法抓取页面相关内容 ...
- Spark记录-Scala类与对象小例子
//基类-Person class Person(val na: String, val ag: Int) { //属性 var name: String = na var age: Int = ag ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams
http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...
- android studio run的时候一直卡在waiting for debug
原因如下: 选择ok就可以,同时我们也可以从这里找到 平常遇到跟真机有关的问题,三步大法,1,插拔手机,2.adb kill-server;adb start-server 3.重启as
- 面板支持单个,多个元素的jQuery图片轮播插件
一.先附上demo <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- sql server查询某年某月有多少天
sql语句如下: ),) date from (),,)+'-01' day) t1, ( ) t2 ),) ),,)+'%' 查询结果如下: 2017年2月共有28天,查询出28条记录.
- HBase笔记之namespace
一.什么是namespace 在RDBMS中有database的概念,用来对table进行分组,那么在HBase中当表比较多的时候如何对表分组呢,就是namespace,可以简单的把namespace ...
- RPM Database
RPM Database RPM 不仅在安装.升级.卸载方面工作出色,而且在查询和验证方面也表现非凡.你很久前安装了一个数据库软件,但现在忘记了它的版本号,也不知道它的说明文档的位置,可以通过 RPM ...