[cf contest 893(edu round 33)] F - Subtree Minimum Query
[cf contest 893(edu round 33)] F - Subtree Minimum Query
time limit per test6 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You are given a rooted tree consisting of n vertices. Each vertex has a number written on it; number ai is written on vertex i.
Let's denote d(i, j) as the distance between vertices i and j in the tree (that is, the number of edges in the shortest path from i to j). Also let's denote the k-blocked subtree of vertex x as the set of vertices y such that both these conditions are met:
- x is an ancestor of y (every vertex is an ancestor of itself);
- d(x, y) ≤ k.
You are given m queries to the tree. i-th query is represented by two numbers xi and ki, and the answer to this query is the minimum value of aj among such vertices j such that j belongs to ki-blocked subtree of xi.
Write a program that would process these queries quickly!
Note that the queries are given in a modified way.
InputThe 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 a1, a2, ..., an (1 ≤ ai ≤ 109) — 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 ≤ 106) — the number of queries to process.
Then m lines follow, i-th line containing two numbers pi and qi, which can be used to restore i-th query (1 ≤ pi, qi ≤ n).
i-th query can be restored as follows:
Let last be the answer for previous query (or 0 if i = 1). Then xi = ((pi + last) mod n) + 1, and ki = (qi + last) mod n.
OutputPrint m integers. i-th of them has to be equal to the answer to i-th query.
Exampleinput5 21 3 2 3 52 35 13 44 121 22 3output25
我们对于树上的每一个节点开一个线段树(其实是可持久化的)。
每棵线段树都是一个[1..maxdepth]的按照绝对深度来建的线段树。
那么,对于每一个节点x,可以把所以它的子节点的线段树合并到它上面去。
但是这需要可持久化。因为当前节点的信息也要用到。如果不可持久化,可能会导致当前节点的地址在其他线段树里面存在。
实际上,每个地址只存在与一棵线段树中。
然后,由于父子节点之间的线段树只有1条链不同,所以要新建logn个节点。
总共有n-1对父子关系,所以空间为O(nlogn)。
时间复杂度为O(mlogn)。注意强制在线。
code:
#include <cstdio> #include <cstring> #include <algorithm> typedef long long LL; using namespace std; void OJ() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif } namespace fastIO { #define gec(c) getchar(c) #define puc(c) putchar(c) char ch; inline int read() { ,f=; ch=getchar(); ') { if (ch=='-') f=-f; ch=gec(); } ') { x=(x<<)+(x<<)+(ch^); ch=gec(); } return x*f; } ]; template <class T> inline void write(T x) { ) { puc('); return; } ) x=-x,puc('-'); ; x; x/=) nnn[++ttt]=x%; ); } inline void newline () { puc('\n'); } } using namespace fastIO; ,inf=2e9; int n,q,tot,lim; ],son[N<<],dep[N]; struct node { node* l,* r; int v; node () { l=r=; v=inf; } } *ro[N]; #define M ((l)+(r)>>1) inline void refresh (node* c) { c->v=inf; if (c->l) c->v=min(c->v,c->l->v); if (c->r) c->v=min(c->v,c->r->v); } inline void setup (node* &c,int l,int r,int x,int v) { c=new node(); if (l==r) { c->v=v; return; } if (x<=M) setup(c->l,l,M,x,v); ,r,x,v); refresh(c); } inline node* merge (node* x,node* y) { if (!x||!y) return x?x:y; node* ret=new node(); ret->l=merge(x->l,y->l); ret->r=merge(x->r,y->r); ret->v=min(x->v,y->v); return ret; } inline int reply (node* u,int l,int r,int x,int y) { if (!u) return inf; if (x<=l&&r<=y) return u->v; if (y<=M) return reply(u->l,l,M,x,y); else ,r,x,y); else ,r,x,y)); } void add (int x,int y) { nxt[++tot]=lnk[x],lnk[x]=tot,son[tot]=y; } void dfs (int x,int p) { setup(ro[x],,n,dep[x]=dep[p]+,a[x]); lim=max(lim,dep[x]); for (int j=lnk[x]; j; j=nxt[j]) { if (son[j]==p) continue; dfs(son[j],x); ro[x]=merge(ro[x],ro[son[j]]); } } int main() { OJ(); ; n=read(),rot=read(); ; i<=n; ++i) { a[i]=read(),ro[i]=; } ; i<n; ++i) { x=read(),y=read(); add(x,y),add(y,x); } dep[]=lim=,dfs(rot,); q=read(); for ( ; q; --q) { x=read(),y=read(); x=(x+ans)%n+,y=(y+ans)%n; ans=reply(ro[x],,n,dep[x],min(lim,dep[x]+y)); write(ans),newline(); } ; }
[cf contest 893(edu round 33)] F - Subtree Minimum Query的更多相关文章
- EC Round 33 F. Subtree Minimum Query 主席树/线段树合并
这题非常好!!! 主席树版本 很简单的题目,给一个按照指定节点的树,树上有点权,你需要回答给定节点的子树中,和其距离不超过k的节点中,权值最小的. 肯定首先一想,按照dfs序列建树,然后按照深度为下标 ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Codeforces 893F - Subtree Minimum Query
893F - Subtree Minimum Query 题意 给出一棵树,每次询问 \(x\) \(k\),求以 \(x\) 为根结点的子树中的结点到结点 \(x\) 的距离小于等于 \(k\) 的 ...
- CF893F Subtree Minimum Query 解题报告
CF893F Subtree Minimum Query 输入输出格式 输入格式: The first line contains two integers \(n\) and \(r\) ( \(1 ...
- [CF893F] Subtree Minimum Query
Description: 给定一棵树,每次询问某点子树中到其不超过k的所有点的最小点权 强制在线 Hint: \(n,m\le 10^5\) Solution: 看到题目第一反应是以深度为下标,dfs ...
- CF893F Subtree Minimum Query 主席树
如果是求和就很好做了... 不是求和也无伤大雅.... 一维太难限制条件了,考虑二维限制 一维$dfs$序,一维$dep$序 询问$(x, k)$对应着在$dfs$上查$[dfn[x], dfn[x] ...
- Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)
题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...
- 2019.01.19 codeforces893F.Subtree Minimum Query(线段树合并)
传送门 线段树合并菜题. 题意简述:给一棵带点权的有根树,多次询问某个点ppp子树内距离ppp不超过kkk的点的点权最小值,强制在线. 思路: 当然可以用dfsdfsdfs序+主席树水过去. 然而线段 ...
- CF893F:Subtree Minimum Query(线段树合并)
Description 给你一颗有根树,点有权值,m次询问,每次问你某个点的子树中距离其不超过k的点的权值的最小值.(边权均为1,点权有可能重复,k值每次询问有可能不同,强制在线) Input 第一行 ...
随机推荐
- windows杀进程
netstat -aon|findstr 1099 tasklist|findstr 1008 taskkill /pid 10084 -f
- Dio添加Cookie
在使用Options添加headers时,Map没有定义内部类型: Dio dio = new Dio(); Map headers = new Map(); headers['Cookie'] = ...
- 【Diary】
[写日记是好习惯] 前记 很随意的日记.想什么写什么,没有限制. 希望以后看到曾经,努力的自己比摸鱼的自己多. 2019.3 2019.3.29 第24次请假打卡 xzh:那些理科男以后都会当IT工作 ...
- linux常用英文单词记录
1.skip 跳过忽略 2.next 下一步3.hostname 主机名4.password 密码5.complete 完成6.network 网络7.conf config configuratio ...
- mac pycharm快捷键
Mac键盘符号和修饰键说明 ⌘ Command ⇧ Shift ⌥ Option ⌃ Control ↩︎ Return/Enter ⌫ Delete ⌦ 向前删除键(Fn+Delete) ↑ 上箭头 ...
- CentOS下使用tcpdump网络抓包
tcpdump是Linux下的截获分析网络数据包的工具,对优化系统性能有很大参考价值. 安装 tcpdump不是默认安装的,在CentOS下安装: yum install tcpdump 在Ubunt ...
- 详解Bootstrap实现基本布局的方法
看到了一篇 20 分钟打造 Bootstrap 站点的文章,内容有点老,重新使用bootstrap教程实现一下,将涉及的内容也尽可能详细说明. 1. 创建基本的页面我们先创建一个基本的 HTML 模板 ...
- fmt.Sprintf格式化使用
格式化写法: fmt.Sprintf(格式化样式, 参数列表-) 参数说明: 格式化样式:字符串形式,格式化动词以%开头. 参数列表:多个参数以逗号分隔,个数必须与格式化样式中的动词个数对应,否则运行 ...
- JS高程13.3事件对象的学习笔记
1.事件流 事件流描述的是页面中元素接收事件的顺序.比如你单击了某个按钮,他们都认为单击事件不仅仅发生在按钮上,换句话说,在单击按钮的同时,你也单击了按钮的容器元素,甚至还单击了整个页面.那么你到底是 ...
- HackerRank-Python攻城歷程-3.List( Find the Second Largest Number )
if __name__ == '__main__': n = int(input()) arr = map(int, input().split()) print(sorted(list(set(ar ...