Problem: Query on the tree(二分+划分树)
题目链接:
Problem: Query on the tree
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).
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.
Each line output a single number, the minimum number of abs(vj-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(二分+划分树)的更多相关文章
- 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 ...
- 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 ...
- hdu4417(Super Mario)—— 二分+划分树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- 【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 ...
- HDU 6191 Query on A Tree(字典树+离线)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- BZOJ3637 Query on a tree VI(树链剖分+线段树)
考虑对于每一个点维护子树内与其连通的点的信息.为了换色需要,记录每个点黑白两种情况下子树内连通块的大小. 查询时,找到深度最浅的同色祖先即可,这可以比较简单的树剖+线段树乱搞一下(似乎就是qtree3 ...
随机推荐
- smarty基础原理
smarty基础原理 一.html模板页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- 关于Python字符编码encode和decode
(注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 记得几天前,部门的一个小姑娘问我,怎么她Python打印出来的中文信息都乱码了?我走过去,略思一二,瞬间给她搞定,其实这是字 ...
- Oracle---------coalesce的用法介绍
COALESCE (expression_1, expression_2, ...,expression_n)依次参考各参数表达式,遇到非null值即停止并返回该值.如果所有的表达式都是空值,最终将返 ...
- 【JavaScript】下大雪
引用[JavaScript]满天星的代码,稍作修改的结果: function drawStars() { for (i = 1; i < 100; ++i) { ctx.fillText(&qu ...
- Apahce 加载模块说明
LoadModule auth_basic_module modules/mod_auth_basic.so #基本认证模块 LoadModule auth_digest_module modules ...
- Madplay移植到mini2440开发板【转】
本文转载自:https://blog.csdn.net/simanstar/article/details/24035379 madplay交叉编译 交叉编译器:arm-linux-gcc 3.4.1 ...
- Latex排版全解【转载】
Latex排版全解 https://www.cnblogs.com/jingwhale/p/4250296.html
- Luogu-1975 [国家集训队]排队
Luogu-1975 [国家集训队]排队 题面 Luogu-1975 题解 题意:给出一个长度为n的数列以及m个交换两个数的操作,问每次操作后逆序对数量 时间,下标和数的大小三维偏序,,,把交换操作看 ...
- RAID独立冗余磁盘阵列
独立冗余磁盘阵列(Redundant Array OF Independent Disks,RAID)开始于20世纪80年代美国加州大学伯克利分校的一个研究项目,当时RAID被称为廉价冗余磁盘阵列(R ...
- java.lang.IllegalArgumentException异常处理的一种方法
我遇到的问题 用spring注解来加载bean,都是一个简单的了,但是还是报了这样一个错java.lang.IllegalArgumentException 网上查了一下,说是jdk版本要1.7的,然 ...