题目链接:

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;
}

  

Problem: Query on the tree(二分+划分树)的更多相关文章

  1. SPOJ PT07J - Query on a tree III(划分树)

    PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...

  2. BZOJ_1803_Spoj1487 Query on a tree III_主席树+dfs序

    BZOJ_1803_Spoj1487 Query on a tree III_主席树 Description You are given a node-labeled rooted tree with ...

  3. hdu4417(Super Mario)—— 二分+划分树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu 4836 The Query on the Tree(线段树or树状数组)

    The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  6. SP1487 PT07J - Query on a tree III (主席树)

    SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...

  7. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  8. HDU 6191 Query on A Tree(字典树+离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  9. BZOJ3637 Query on a tree VI(树链剖分+线段树)

    考虑对于每一个点维护子树内与其连通的点的信息.为了换色需要,记录每个点黑白两种情况下子树内连通块的大小. 查询时,找到深度最浅的同色祖先即可,这可以比较简单的树剖+线段树乱搞一下(似乎就是qtree3 ...

随机推荐

  1. 请求库之requests

    一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内 ...

  2. Spring 知识点总结

    一.Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Sprin ...

  3. hadoop10---消息队列

    java消息队列 BlockingQueue也是java.util.concurrent下的主要用来控制线程同步的工具.锁也是用来控制线程同步的,释放锁的时候谁拿到了锁是没有预测的,所以谁拿到了锁是不 ...

  4. pyqt4学习之一:搭建环境和入门

    还在继续写Python小工具,想起之前用Tkinter被坑得半死,决定换个框架写UI,又想顺便了解一下qt,就学习一下pyqt4 搭建环境 win:现在安装包 http://www.riverbank ...

  5. camera corder profile

    /system/etc/ 其中的qulity high 必须与 最大的支持的分辨率相同. 不然cts 不过. 这里的配置必须在报告给app的数据匹配.

  6. java/kotlin 读取文件、写入文件

    package dh.btb.backend.utils import java.io.*object FileUtil { /** * 创建文件 * @param filePath 文件路径(不要以 ...

  7. [BZOJ1018]堵塞的交通traffic

    Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一 ...

  8. Buildbot初探

    什么是Buildbot Buildbot是一个持续集成和自动化测试框架,我在毕业刚进VMware不久的一个和以色列人合作的项目中接触到Buildbot,当时我真的恨死它了...经常随意的提交了一些代码 ...

  9. 有谁知道什么工具测试IOS手机上APP的性能软件啊?

    有谁知道什么工具测试IOS手机上APP的性能软件啊?

  10. Action<T>和Func<T>

    Action<T>和Func<T>都是泛型委托. Action<T>表示委托可以引用一个viod返回类型的方法,至于方法是带几个参数,什么类型的参数,由后面的泛型决 ...