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 ...
随机推荐
- P3089 [USACO13NOV]POGO的牛Pogo-Cow
P3089 [USACO13NOV]POGO的牛Pogo-Cow FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度. FJ觉得让贝西在一条直线的一维线路上进行练 ...
- spring JMS在接收消息的时候总是报错
spring JMS在接收消息的时候总是报错 org.springframework.jms.UncategorizedJmsException: Uncategorized exception oc ...
- 跨域请求:JSONP
在JavaScript中,有一个很重要的安全性限制,被称为"同源策略".即JavaScript只能访问与包含它的文档在同一域下的内容.然而,当进行一些比较深入的前端编程的时候,不可 ...
- iframe引入网页
<!DOCTYPE html> <html> <body> <iframe src="/example/html/demo_iframe.html& ...
- [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)
[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...
- 第8月第12天 python json.dumps danmu
1.json.dumps return JsonResponse({ 'status': WechatMessage.POST_METHOD_REQUIRED[1], 'status_code': W ...
- MongoDB 之 数据类型 最无聊! But 最有用! MongoDB - 3
MongoDB的新篇章开始啦 - 数据类型 https://www.cnblogs.com/xuzhaocai/p/8048177.html 一.MongoDB 之 丰富多彩的数据类型世界 首先我们要 ...
- EM算法理解
一.概述 概率模型有时既含有观测变量,又含有隐变量,如果概率模型的变量都是观测变量,那么给定数据,可以直接利用极大似然估计法或者贝叶斯估计法估计模型参数.但是,当模型同时又含有隐变量时,就不能简单地使 ...
- 【干货】操纵时间 感受威胁 MAC time时间戳视角
来源:Unit 4: Unix/Linux Forensics Analysis 4.1 Unix/Linux Forensics Analysis MAC Times Sleuthkit工具的MAC ...
- 关于内核中spinlock的一些个人理解 【转】
由于2.6内核可以抢占,应该在驱动程序中使用 preempt_disable() 和 preempt_enable(),从而保护代码段不被抢占(禁止 IRQ 同时也就隐式地禁止了抢占).preempt ...