Problem: Query on the tree(二分+划分树)

题目链接:

Problem: Query on the tree

Time limit: 1s     Mem limit: 64 MB     
Problem Description

There is a tree with n node, labeled from 1 to n, and the root of the tree is 1.

For every node i, if its father is j, its value vi=vj*i%20161119, the value of the root is 1.

Now monster has q queries, for each query, monster give you a node i and a number k (0<=k<20161119),

 

you should find a node j in i’s subtree to minimize abs(k-vj).

Input

Multiple cases.

In first line there is a number n (n<=1e5), then next n-1 lines, each describes an edge of

the tree.

Next line there is a number q(q<=1e5), then next q lines, each contains two numbers i and k.

 

 

Output

Each line output a single number, the minimum number of abs(vj-k).

 

 

Sample Input
7 1 2 1 3 2 4 2 5 3 6 3 7 5 1 4 2 7 3 30 6 0 7 7
Sample Output
1 1 9 18 14
题意:给出一棵树和每个节点的点权,现在m个询问,每个询问是问在一个节点的子树上找到一个节点是得那个式子的值最小;
思路:dfs序之后可以得到每个点的子树对应的区间,然后再在这个区间里面找到一个最接近k的节点值,可以二分第x大,然后找到k附近的那一个或者两个数求得答案;
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int n,m,first[maxn],last[maxn],cnt;
int val[maxn];
vector<int>ve[maxn];
int a[maxn],tree[32][maxn],sorted[maxn],num[32][maxn];template<class T> void read(T&num) {char CH; bool F=false;for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {if(!p) { puts("0"); return; }while(p) stk[++ tp] = p%10, p/=10;while(tp) putchar(stk[tp--] + '0');putchar('\n');
}void dfs(int cur,int fa)
{val[cur]=(int)((LL)val[fa]*cur%20161119);int len=ve[cur].size();a[++cnt]=(int)val[cur];first[cur]=cnt;for(int i=0;i<len;i++){int x=ve[cur][i];if(x==fa)continue;dfs(x,cur);}last[cur]=cnt;
}
void build(int dep,int l,int r)
{if(l>=r)return ;int mid=(l+r)>>1;int lp=l,rp=mid+1,sum=mid-l+1;for(int i=l;i<=r;i++){if(tree[dep][i]<sorted[mid])sum--;}for(int i=l;i<=r;i++){if(i!=l)num[dep][i]=num[dep][i-1];else num[dep][i]=0;if(tree[dep][i]<sorted[mid]){tree[dep+1][lp++]=tree[dep][i];num[dep][i]++;}else if(tree[dep][i]==sorted[mid]&&sum>0){tree[dep+1][lp++]=tree[dep][i];num[dep][i]++;sum--;}else{tree[dep+1][rp++]=tree[dep][i];}}build(dep+1,l,mid),build(dep+1,mid+1,r);
}
int query(int dep,int l,int r,int ql,int qr,int k)
{if(l>=r)return tree[dep][l];int mid=(l+r)>>1,s,ss;if(l!=ql)s=num[dep][ql-1],ss=num[dep][qr]-num[dep][ql-1];else s=0,ss=num[dep][qr];if(k<=ss)return query(dep+1,l,mid,l+s,l+s+ss-1,k);else return query(dep+1,mid+1,r,mid+1+ql-l-s,mid+1+qr-l-s-ss,k-ss);
}
inline int solve(int L,int R,int k)
{int ans=1e9;int l=1,r=R-L+1;while(l<=r){int mid=(l+r)>>1;if(query(0,1,cnt,L,R,mid)<=k)l=mid+1;else r=mid-1;}int pos=l-1;if(pos){ans=min(ans,abs(k-query(0,1,cnt,L,R,pos)));if(pos<=R-L)ans=min(ans,abs(k-query(0,1,cnt,L,R,pos+1)));}else ans=min(ans,abs(k-query(0,1,cnt,L,R,pos+1)));return ans;
}
int main()
{while(scanf("%d",&n)!=EOF){int u,v;cnt=0;val[0]=1;for(int i=0;i<=n;++i){ve[i].clear();for(int j=0;j<32;++j)tree[j][i]=num[j][i]=0;}for(int i=1;i<n;++i){read(u);read(v);ve[u].push_back(v);ve[v].push_back(u);}dfs(1,0);for(int i=1;i<=cnt;++i)sorted[i]=tree[0][i]=a[i];sort(sorted+1,sorted+cnt+1);build(0,1,cnt);read(m);while(m--){read(u);read(v);int L=first[u],R=last[u];print(solve(L,R,v));}}return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/6588024.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/371276.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

day04_09 while循环03

练习题: 3.如何输入一个如下的直角三角形,用户指定输出行数:(如果上下反转,右如何实现?) ********** 以下是自己的思路,没有按照上课老师的思路,反正经过不断的测试改进得出的算法 num int(input("请输入行数")) line 1 while line < num1:lie 1 while lie &l…

idal 创建springboot 项目_手把手的SpringBoot教程,SpringBoot创建web项目(四)

在实际的开发过程中&#xff0c;我们需要前端页面向Java端提交请求&#xff0c;这些请求一般分为get方式和post方式&#xff0c;不管是哪一种方式&#xff0c;一般都会携带一些参数。这一节&#xff0c;我们来演示一下如何给Controller传递参数。代码&#xff1a;RestControlle…

JavaOne 2012:Lambda之路

我最热切期待的JavaOne 2012演讲之一是Brian Goetz的“通往Lambda的道路”。 昨晚的技术主题演讲中的Lambda味道仅增加了预期。 这是在希尔顿广场A / B举行的&#xff0c;距离我上次在金门大桥A / B / C参加的演讲仅几步之遥。 我原本希望打包相对较大的Plaza A / B&#xff08…

沉浸式go-cache源码阅读!

大家好&#xff0c;我是豆小匠。 这期来阅读go-cache的源码&#xff0c;了解本地缓存的实现方式&#xff0c;同时掌握一些阅读源码的技巧~ 1. 源码获取 git clone https://github.com/patrickmn/go-cache.git用Goland打开可以看到真正实现功能的也就两个go文件&#xff0c;ca…

CoreAnimation 变换

CoreAnimation 变换 CoreAnimation 目录 博客园MakeDown支持不佳,如有需要请进GitHub 本片博客主要内容: 仿射变换 - CGAffineTransform3D变换 - CATransform3D仿射变换 - CGAffineTransform CGAffineTransform 是用于二维空间的旋转,缩放和平移的属性.首先展示一个简单的样例,…

20170907wdVBA_GetCellsContentToExcel

WORD 加载项 代码模板 Dim cmdBar As CommandBar, cmdBtn As CommandBarControl Const cmdBtnCap As String "批量提取操作步骤"Sub AutoExec()Call DelCmdBtnCall AddCmdBtnEnd Sub Sub AutoExit()Call DelCmdBtn End SubSub AddCmdBtn()Set cmdBar Application.C…

mysql 5.7 mirror_Centos7 Docker离线部署Mysql5.7

1 环境信息查看系统内核[rootlocalhost /]# cat /etc/redhat-releaseCentOS Linux release 7.5.1804 (Core)2 虚拟机拉取镜像此处资源获取在虚拟机中进行&#xff0c;完成后上传到服务器安装2.1 拉取mysql5.7镜像[rootlocalhost /]# docker pull mysql:5.72.2 导出镜像[rootloc…

Java中的简单REST客户端

如今&#xff0c;大多数用于与某些服务器通信的移动应用程序都使用REST服务。 这些服务也是与JavaScript或jQuery一起使用的常见做法。 现在&#xff0c;我知道在Java中为REST服务创建客户端的2种方法&#xff0c;在本文中&#xff0c;我将尝试演示这两种方法&#xff0c;希望它…

3.20 下午

阅读《艺术学概论》 戏剧冲突是戏剧的灵魂 冲突包括&#xff1a;人物性格的冲突、行为的冲突、 思想感情的冲突乃至心理状态的冲突等等 转载于:https://www.cnblogs.com/bgd140206110/p/6590005.html

华为root工具_华为Mate9解锁后无法ROOT 需要手动刷入Recovery怎么办【解决方法】...

很多朋友手机到手之后&#xff0c;都希望能够ROOT使用更多的系统功能。近日有网友向小编询问&#xff0c;为何华为Mate9解锁后无法ROOT&#xff0c;明明已经通过官方的解锁教程解锁的&#xff0c;但是之后使用“大师”等第三方刷机工具&#xff0c;无法ROOT。其实ROOT的关键就在…

JAX-WS入门

JAX-WS代表XML Web Services的Java API。 它是一种Java编程语言API&#xff0c;用于创建Web服务和使用XML进行通信的客户端。 这篇文章是JAX-WS的快速入门。 先决条件 GlassFish与Eclipse集成在一起 。 创建JAX-WS Web服务 1.在Eclipse中创建一个名为“ com.eviac.blog.jax…

canvas 图片反色

代码实例&#xff1a; <!DOCTYPE HTML> <html> <head><meta charset"utf-8"><title>图片反色</title><style type"text/css">body{ background:black;}#c1{ background:white;}</style><script type&q…

python中的文件父路径怎么表达_python中的文件父路径怎么表达_如何在Python中访问父目录...

所以我有一个朋友给我的Python脚本&#xff0c;但是我没有Python的经验。代码如下&#xff1a;from os import path, chdir, listdir, mkdir, getcwdfrom sys import argvfrom zipfile import ZipFilefrom time import sleep#Defines what extensions to look for within the f…

Maven的中央仓库地址

www.mvnrepository.com转载于:https://www.cnblogs.com/j-liu3323/p/6590435.html

Spring–添加AOP支持

我听到了一个有关一位高级&#xff08;且酬劳颇丰&#xff09;软件工程师的故事。 他的任务是记录他正在研究的项目中每个控制器中的每个方法。 工程师重写了所有控制器方法&#xff0c;因此使用如下代码&#xff1a; RequestMapping(method RequestMethod.GET)public String …

vscode python第三方库检测_VSCode中使用Pylint检查python代码

为什么使用lint在日常开发中&#xff0c;不同开发人员会写下不同风格的代码&#xff0c;导致代码可维护性变差&#xff0c;为了解决风格不一致问题&#xff0c;我们可以制定代码规范&#xff0c;让开发人员都遵守同样的规范编写代码。在开发过程中&#xff0c;部分代码存在质量…

Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)

以下内容翻译自&#xff1a;https://www.tutorialspoint.com/springmvc/springmvc_log4j.htm 说明&#xff1a;示例基于Spring MVC 4.1.6。 以下示例说明如何使用Spring Web MVC框架来触发LOG4J。首先&#xff0c;让我们使用Eclipse IDE&#xff0c;并按照以下步骤使用Spring W…

NUMA架构和Java

是时候部署您的应用程序了&#xff0c;期待着采购最适合负载要求的硬件。 如今&#xff0c;具有40核或80核的包装盒非常普遍。 总体概念是更多的内核&#xff0c;更多的处理能力&#xff0c;更多的吞吐量。 但是我看到了一些相反的结果&#xff0c;表明小型的CPU密集型测试运行…

存储过程常用技巧

我们在进行pl/sql编程时打交道最多的就是存储过程了。存储过程的结构是非常的简单的&#xff0c;我们在这里除了学习存储过程的基本结构外&#xff0c;还会学习编写存储过程时相关的一些实用的知识。如&#xff1a;游标的处理&#xff0c;异常的处理&#xff0c;集合的选择等等…

vue是用a标签打开新页面_vue 在新窗口打开页面并设置不同的背景

开发一个新系统&#xff0c;前端用的vue&#xff0c;vue是单体应用&#xff0c;所有页面都在一个窗口里实现&#xff0c;但项目要求在点button链接后要新打开一个浏览器页面&#xff0c;解决方法如下&#xff1a;1. 给此button设置新事件 click"createdefect"提交缺陷…