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 ...
随机推荐
- jsp页面向后台传递 不赋值 传递的默认值
<input type="hidden" name="leaderIdentity" value="${subject.leaderId }&q ...
- python之路 RabbitMQ、SQLAlchemy
一.RabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列 ...
- 关于Webpage Not Found问题解决~~~
还是外文网站好,以下解决办法: IIS6.0 UI vs. IIS 7.x UI Series: More about Web Service Extensions This week in the ...
- LigerUI v1.2.4 LigerGrid默认样式 工具条背景白色
修改Aqua的ligerui-grid.css .l-panel-topbar 样式 修改为: .l-panel-topbar{padding: 0;background: #CEDFEF url(' ...
- ES6 随记(3.3)-- 数组的拓展
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...
- cache工作原理
转:http://www.360doc.com/content/11/0307/21/3791508_99049437.shtml
- jQuery图片分组切换焦点图
在线演示 本地下载
- Maven:Eclipse上Maven的配置
Eclipse上Maven的配置: 步骤: ①Maven下载地址: http://maven.apache.org/download.cgi# ②解压apache-maven-3.5.0-bin.zi ...
- Eclipse 启动tomcat 访问主页报错404
问题 tomcat用startup.sh启动,访问localhost:8080能正常访问,用Eclipse service启动tomcat,访问localhost:8080报错404 解决方法 1. ...
- SFTP无法连接 Connection closed by server with exitcode 127
命令: Pass: ************状态: Connected to 66.77.88.99错误: Connection closed by server with exitcode 127错 ...