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次询问,每一次询 ...
随机推荐
- struts2学习
struts2是一种基于mvc模式的轻量级web框架,它本质上相当于一个servlet,在mvc设计模式中,struts2作为控制器(Controller)来建立模型与视图的数据交互,struts2采 ...
- ruby Time类与Date类
Time类用于表示时间.时间除了表示年月日时分秒的信息外,还包含了表示地域时差的时区(time zone)信息.例如我们可以计算中国当前时间是国际协调时间的几点 Date类只用于表示年月日.因此,相对 ...
- hack游戏攻略(梦之光芒黑客小游戏)
2019.2.11 继续玩~~还是黑客游戏闯关类的 地址:http://monyer.com/game/game1/ 直接查看页面代码: first.php就是了: 查看源代码: 这里尝试输入 两个空 ...
- POJ1426 Find The Multiple 解题报告
参考:http://www.cnblogs.com/ACShiryu/archive/2011/07/24/2115356.html #include <iostream> #includ ...
- POJ-2421-Constructing Roads(最小生成树 普利姆)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26694 Accepted: 11720 Description The ...
- WRITE
WRITE - int_format_options 基本形式 ... [LEFT-JUSTIFIED|CENTERED|RIGHT-JUSTIFIED] [NO-GAP] ...
- Ubuntu无法安装vim怎么办?(Ubuntu 出现apt-get: Package has no installation candidate问题)
apt-get install vim 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 有一些软件包无法被安装.如果您用的是不稳定(unstable)发行版, ...
- laravel5.5jwt-auth的使用
laravel5.5 + jwt-auth:dev-develop 安装扩展 composer require tymon/jwt-auth:dev-develop --prefer-source 添 ...
- 2018春季校园招聘笔经面经合集:Java开发岗
2018春季校园招聘笔经面经合集:Java开发岗 以下为精选面经: 美团外卖配送部后台开发面经 nowcoder.com/discuss/76 春招总结,干货满满 nowcoder.com/discu ...
- App间常用的五种通信方式
1.URL Scheme 2.Keychain 3.UIPasteboard剪切板 4.UIDocumentInteractionController 5.local socket 详见: 转自:ht ...