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. 【tp5.1】composer安装PHPExcel以及导入\导出Excel

    一.安装PHPExcel 1.下载:PHPExcel  https://github.com/PHPOffice/PHPExcel 2.解压后:Classes文件夹改名为PHPExcel 3.把文件夹 ...

  2. 【Js】Jquery遍历-each(function(e){})中的e和$(this)的区别

    $("selector").each(function(e){ console.log($(e)); console.log($(this)); console.log(e); c ...

  3. python 排列组合

    笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...

  4. java简单界面实现

    import javax.swing.JFrame; import javax.swing.JPanel; public class DemoFrame extends JFrame{ public ...

  5. mysql学习第三天练习(多表连接)

    -- 多表连接 -- 写一条查询语句,查询员工姓名.部门名称.工作地点 select ename,dname,loc from emp,dept where emp.deptno = dept.dep ...

  6. DDL失败案例

    问题描述 今天对线上某个业务的大表120G进行重建表操作时遇到报错,该表有个比较显著的特征是*写入量比较大,每天写入加更新的频率在数千万级别.大致的环境 1 版本:Percona 5.6.24 2 操 ...

  7. 7.Mongodb复制(副本集)

    1.复制 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备份 数据灾难恢复 ...

  8. P3527 [POI2011]MET-Meteors

    P3527 [POI2011]MET-Meteors 链接 整体二分! 代码 #include<bits/stdc++.h> using namespace std; typedef lo ...

  9. MapRudecer

    MapReducer基本概念 Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架: Mapreduce核心功能是将用户编写的业务逻辑代码和自带默认 ...

  10. LINUX目录的意思

    Linux系统/目录下的文件夹里面分别是以下内容: /usr 包含所有的命令和程序库.文档和其他文件,还包括当前linux发行版的主要应用程序 /var 包含正在操作的文件,还有记录文件.加密文件.临 ...