CF893F Subtree Minimum Query 解题报告
CF893F Subtree Minimum Query
输入输出格式
输入格式:
The first line contains two integers \(n\) and \(r\) ( \(1<=r<=n<=100000\) ) — the number of vertices in the tree and the index of the root, respectively.
The second line contains n integers \(a_{1},a_{2},...,a_{n}\) ( \(1<=a_{i}<=10^{9}\) ) — the numbers written on the vertices.
Then \(n-1\) lines follow, each containing two integers \(x\) and \(y\) ( \(1<=x,y<=n\) ) and representing an edge between vertices \(x\) and \(y\) . It is guaranteed that these edges form a tree.
Next line contains one integer \(m\) ( \(1<=m<=10^{6}\) ) — the number of queries to process.
Then m lines follow, \(i\) -th line containing two numbers \(p_{i}\) and \(q_{i}\) , which can be used to restore \(i\) -th query ( \(1<=p_{i},q_{i}<=n\) ).
\(i\) -th query can be restored as follows:
Let last last be the answer for previous query (or \(0\) if \(i=1\) ). Then \(x_{i}=((p_{i}+last) \bmod n)+1\), and \(k_{i}=(q_{i}+last) \bmod n\) .
输出格式:
Print \(m\) integers. \(i\) -th of them has to be equal to the answer to \(i\) -th query.
题意大概就是给你一个有跟有点权的树,边权均为\(1\),每次询问一个点子树中距离Ta不超过\(k\)距离的点的最小点权。
发现\(dfs\)序限定子树是一个区间,可以放在线段树上,然后深度确定另一个区间,套一颗平衡树,就可以了。
事实上这道题还有一个高妙的做法,我并不会。


说不定以后会看一看呐
Code:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ls ch[now][0]
#define rs ch[now][1]
const int N=1e5+10;
int ch[N*30][2],dep[N*30],dat[N*30],mx[N*30],val[N*30],root[N<<2],tot;
int min(int x,int y){return x<y?x:y;}
void updata(int now){mx[now]=min(dat[now],min(mx[ls],mx[rs]));}
void split(int now,int k,int &x,int &y)
{
if(!now){x=y=0;return;}
if(dep[now]<=k)
x=now,split(rs,k,rs,y);
else
y=now,split(ls,k,x,ls);
updata(now);
}
int Merge(int x,int y)
{
if(!x||!y) return x+y;
if(val[x]<val[y])
{
ch[x][1]=Merge(ch[x][1],y);
updata(x);
return x;
}
else
{
ch[y][0]=Merge(x,ch[y][0]);
updata(y);
return y;
}
}
int New(int d,int de)
{
val[++tot]=rand(),dat[tot]=mx[tot]=d,dep[tot]=de;
return tot;
}
void Insert(int id,int d,int de)
{
int x,y;
split(root[id],de,x,y);
root[id]=Merge(x,Merge(New(d,de),y));
}
int ask(int id,int de)
{
int x,y,z;
split(root[id],de,x,y);
z=mx[x];
root[id]=Merge(x,y);
return z;
}
int query(int id,int L,int R,int l,int r,int de)
{
if(l==L&&r==R)
return ask(id,de);
int Mid=L+R>>1;
if(r<=Mid) return query(id<<1,L,Mid,l,r,de);
else if(l>Mid) return query(id<<1|1,Mid+1,R,l,r,de);
else return min(query(id<<1,L,Mid,l,Mid,de),query(id<<1|1,Mid+1,R,Mid+1,r,de));
}
int Next[N<<1],to[N<<1],head[N],cnt;
void add(int u,int v)
{
to[++cnt]=v,Next[cnt]=head[u],head[u]=cnt;
}
int dfn[N],low[N],Dep[N],ha[N],dfs_clock,n,m,rt,a[N];
void dfs(int now,int fa)
{
dfn[now]=++dfs_clock;
ha[dfs_clock]=now;
for(int i=head[now];i;i=Next[i])
{
int v=to[i];
if(v!=fa)
Dep[v]=Dep[now]+1,dfs(v,now);
}
low[now]=dfs_clock;
}
void build(int id,int l,int r)
{
for(int i=l;i<=r;i++)
Insert(id,a[ha[i]],Dep[ha[i]]);
if(l==r) return;
int mid=l+r>>1;
build(id<<1,l,mid),build(id<<1|1,mid+1,r);
}
int main()
{
memset(dat,0x3f,sizeof(dat));
memset(mx,0x3f,sizeof(mx));
scanf("%d%d",&n,&rt);
for(int i=1;i<=n;i++) scanf("%d",a+i);
for(int u,v,i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs(rt,0);
build(1,1,n);
scanf("%d",&m);
int las=0;
for(int p,q,i=1;i<=m;i++)
{
scanf("%d%d",&p,&q);
p=(p+las)%n+1,q=(q+las)%n;
printf("%d\n",las=query(1,1,n,dfn[p],low[p],Dep[p]+q));
}
return 0;
}
2018.10.13
CF893F Subtree Minimum Query 解题报告的更多相关文章
- [CF893F] Subtree Minimum Query
Description: 给定一棵树,每次询问某点子树中到其不超过k的所有点的最小点权 强制在线 Hint: \(n,m\le 10^5\) Solution: 看到题目第一反应是以深度为下标,dfs ...
- CF893F:Subtree Minimum Query(线段树合并)
Description 给你一颗有根树,点有权值,m次询问,每次问你某个点的子树中距离其不超过k的点的权值的最小值.(边权均为1,点权有可能重复,k值每次询问有可能不同,强制在线) Input 第一行 ...
- CF893F Subtree Minimum Query 主席树
如果是求和就很好做了... 不是求和也无伤大雅.... 一维太难限制条件了,考虑二维限制 一维$dfs$序,一维$dep$序 询问$(x, k)$对应着在$dfs$上查$[dfn[x], dfn[x] ...
- Codeforces 893F - Subtree Minimum Query
893F - Subtree Minimum Query 题意 给出一棵树,每次询问 \(x\) \(k\),求以 \(x\) 为根结点的子树中的结点到结点 \(x\) 的距离小于等于 \(k\) 的 ...
- [cf contest 893(edu round 33)] F - Subtree Minimum Query
[cf contest 893(edu round 33)] F - Subtree Minimum Query time limit per test 6 seconds memory limit ...
- 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...
- 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)
题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...
随机推荐
- Spring笔记2
Bean生命周期 1 实例化 2 注入属性 3 BeanNameAware 4 BeanFactoryAware 5 ApplicationContextAware 6 BeanPostProcess ...
- FastDFS轻量级分布式文件系统部署
FastDFS介绍 FastDFS 是一个由 C 语言实现的开源轻量级分布式文件系统,作者余庆,支持 Linux.FreeBSD.AID 等 Unix 系统,解决了大数据存储和读写负载均衡等问题,适合 ...
- python应用:经纬度匹配
需要安装第三方包:requests 本次经纬度匹配采用高德地图api,首先将gps坐标转化为高德地图的经纬度坐标,然后再根据转化后的坐标进行匹配. 本次匹配主要是获取距离给定经纬度最近的poi点地址信 ...
- liunx下搭建python开发环境
=============================================================================注意: 在linux下安装新的版本的pytho ...
- Leecode刷题之旅-C语言/python-100相同的树
/* * @lc app=leetcode.cn id=100 lang=c * * [100] 相同的树 * * https://leetcode-cn.com/problems/same-tree ...
- rhel6.4扩充swap分区
状况:Red hat 6.4 swap分区不足 解决:扩充swap ================================================================== ...
- ABAP CDS - 字符串函数
下表显示了ABAP CDS中CDS视图中字符串的潜在SQL函数,以及对参数的要求.函数的含义可以在字符串的SQL函数下找到. 函数 参数类型 返回类型 CONCAT(arg1, arg2) See b ...
- Oracle错误记录
1 SQLPlus无法登陆oracle,PLSql可以登陆,报错ORA-12560 环境变量 右击计算机属性-->高级系统设置-->高级-->环境变量-->系统变量--> ...
- Moodle的安装和登陆(使用Https)
之前使用默认的配置和安装,到中间检测组件是,总是提示 site no https.所以重新安装,用:https://localhost来登陆,结果不再提示,所以建议大家在使用时,还是用https来登 ...
- 超强排序JavaScript插件
Sortable:http://rubaxa.github.io/Sortable/