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 ...
随机推荐
- beego——模型(model)
beego ORM是一个强大的Go语言ORM框架.她的灵感主要来自Django ORM和SQLAlchemy. 已经支持的数据库驱动: MySQL:https://github.com/go-sql- ...
- beego——过滤器
beego支持自定义过滤中间件,例如安全验证.强制跳转等. 过滤器函数如下所示: beego.InsertFilter(pattern string, position int, filter Fil ...
- go——函数
1.定义 函数是结构化编程的最小单元模式.它将复杂的算法过程分解为若干个较小任务,隐藏相关细节,使程序结构更加清晰,易于维护.函数被设计成相对独立,通过接收输入参数完成一段算法指令,输出或存储相关结果 ...
- 官方online ddl
一.5.6版本online DDL 操作 In Place Rebuilds Table Concurrent DML Only Modifies Metadata 说明 add/create sec ...
- 【Flask】Flask Restful api
### 安装: Flask-Restful需要在Flask 0.8以上的版本,在Python2.6或者Python3.3上运行.通过pip install flask-restful即可安装. ### ...
- jQuery单选多选按钮选中美化特效
在线演示 本地下载
- 20145201《Java程序设计》第十周学习总结
教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴. 在发送和接收 ...
- 20145222黄亚奇《网络对抗》- shellcode注入&Return-to-libc攻击深入
20145222黄亚奇<网络对抗>- shellcode注入&Return-to-libc攻击深入 shellcode注入实践过程
- React Native之Fetch简单封装、获取网络状态
1.Fetch的使用 fetch的使用非常简单,只需传入请求的url fetch('https://facebook.github.io/react-native/movies.json'); 当然是 ...
- Contest-hunter 暑假送温暖 SRM01
一直找不到好的题去做...于是想到了srm...回来补题...QAQ 从srm01补起 A 题意:n个数,排成一列,刚开始都是1,如果左右相等就可以合并,问最后的数列长什么样. 思路:比赛的时候直接敲 ...