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 解题报告的更多相关文章

  1. [CF893F] Subtree Minimum Query

    Description: 给定一棵树,每次询问某点子树中到其不超过k的所有点的最小点权 强制在线 Hint: \(n,m\le 10^5\) Solution: 看到题目第一反应是以深度为下标,dfs ...

  2. CF893F:Subtree Minimum Query(线段树合并)

    Description 给你一颗有根树,点有权值,m次询问,每次问你某个点的子树中距离其不超过k的点的权值的最小值.(边权均为1,点权有可能重复,k值每次询问有可能不同,强制在线) Input 第一行 ...

  3. CF893F Subtree Minimum Query 主席树

    如果是求和就很好做了... 不是求和也无伤大雅.... 一维太难限制条件了,考虑二维限制 一维$dfs$序,一维$dep$序 询问$(x, k)$对应着在$dfs$上查$[dfn[x], dfn[x] ...

  4. Codeforces 893F - Subtree Minimum Query

    893F - Subtree Minimum Query 题意 给出一棵树,每次询问 \(x\) \(k\),求以 \(x\) 为根结点的子树中的结点到结点 \(x\) 的距离小于等于 \(k\) 的 ...

  5. [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 ...

  6. 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...

  7. 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...

  8. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

  9. Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)

    题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...

随机推荐

  1. PHP一些常用魔术方法

    魔术方法                          调用方法                                     作用__set                   有两个 ...

  2. vuejs中的计算属性和监视

    计算属性 1.在computed属性对象中定义计算属性的方法,在页面上使用{{方法名}}来显示计算结果 2.通过getter/setter实现对属性数据的显示和监视 3.计算属性存在缓存,多次读取只执 ...

  3. 2018 ccpc final I. Cockroaches

    I. Cockroaches time limit per test6. s memory limit per test256 MB inputstandard input outputstandar ...

  4. shell重温---基础篇(shell变量&字符串以及git GUI运行shell脚本方式)

    既然是基础篇那肯定是需要对shell的各种需要注意的基本点进行说明了.接下来就是show time...    shell呢,是一个用C语言编写的应用程序,是用户使用linux的桥梁.所以呢,他既是一 ...

  5. 教你如何更改xshell中的转发规则

    使用不同的类型转发,与之对应的端口,所以如果想要使用不同类型的转发就要更改端口使其与之一一对应.本集xshell专栏文章将为大家讲解如何更改转发规则. 更改转发规则操作如下: 1.打开会话对话框. 2 ...

  6. linux基础重要命令小节

    此为L005&&L006课程内容的一个总结. 命令: 基本形式 命令 [参数] [路径或文件] 例:ls -ld /data pwd 目前所在目录 [root@moban /]# pw ...

  7. 推荐5个机器学习Python 库,国内外评价超高

    机器学习令人无比神往,但从事这个工作的人可能并不这么想. 机器学习的工作内容往往复杂枯燥又困难——通过大量重复工作进行提升必不可少: 汇总工作流及传输渠道.设置数据源以及在内部部署和云部署的资源之间来 ...

  8. Struts2(三.用户登录状态显示及Struts2标签)

    1.编写main.jsp /WebContent/main.jsp 之前用户登录时已把用户存入session <%@ page language="java" content ...

  9. android问题笔记集

    开发工具:android studio2.2 调试:手机直连调试,版本(android6) 问题1: Error:Unable to start the daemon process. This pr ...

  10. 用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1

    package com.ljn.base; /** * @author lijinnan * @date:2013-9-12 上午9:55:32 */ public class IncDecThrea ...