[gym104076][CCPC2022济南站L] Tree Distance
You are given an unrooted weighted tree \(T\) with vertices \(1,2,…,n\). Please answer some queries.
We define \(dist(i,j)\) as the distance between vertex i and vertex \(j\) in \(T\).
For each query, you are given two integers \(l,r\). Please answer the value of
\]
Input
The first line contains one integer \(n (1≤n≤2×10^5)\), the number of vertices in the tree.
Each of the next n−1 lines describes an edge of the tree. Edge i is denoted by three integers \(a_i,b_i,w_i (1≤a_i,b_i≤n,1≤w_i≤10^9)\), the labels of vertices it connects and its weight.
Then one line contains one integer \(q (1≤q≤10^6)\), the number of queries.
Each of the following q lines contains two integers \(l,r\) \((1≤l≤r≤n)\) describing a query.
It is guaranteed that the given edges form a tree.
Output
For each query, output the answer in one line. If there is no \(i,j\) such that \(1≤i<j≤r\), the answer is \(−1\).
距离问题,考虑点分治
对于一个目前的根 \(x\) 以及若干个连通块中的点,可以把所有点按照编号排序。此时考虑把一些点对设为可能成为答案的点对。
如果此时有 \(i,j,k\) 满足 \(i<j<k\) 且 \(dep_i\ge dep_j,dep_k\),那么 \((i,k)\) 一定不是可以满足要求的点对。因为如果区间包含了 \((i,k)\),一定包含了 \((j,k)\),同时 \(dep_i+dep_j>dep_j+dep_k\)。所以我们不用理他。
由此,我们可以点分治完,对于每个连通块里的点 \(i\),按照编号排序,然后用单调栈找到左右最靠近他的 \(dep\) 小于等于 他的第一个数 \(lst_i,nxt_i\)。容易发现最后得到可能为答案的点对最多只有 \(O(nlogn)\) 个。
把点对按照大的那个数排序,然后扫描线,需要支持单点改,求后缀最小值。树状数组维护即可。对于一个点对 \((l,r,dist(l,r))\),可以当扫到 \(r\) 时把 \(l\) 与 \(dist(l,r)\) 去最小值。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+5;
const LL INF=1e18;
int n,v[N],hd[N],m,rt,k,q,f[N<<3],sz[N],mx,e_num,nwp,st[N],tp,nxt[N],lst[N];
LL tr[N],ans[N<<3];
struct edge{
int v,nxt,w;
}e[N<<1];
struct node{
int l,r;
LL dep;
bool operator<(const node&n)const{
return r<n.r;
}
}p[N<<6];
struct dian{
int x;
LL dep;
bool operator<(const dian&d)const{
return x<d.x;
}
}a[N];
vector<int>g[N];
void add_edge(int u,int v,int w)
{
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
}
void sou(int x,LL dep,int f)
{
a[++m]=(dian){x,dep};
for(int j=hd[x];j;j=e[j].nxt)
if(!v[e[j].v]&&e[j].v^f)
sou(e[j].v,dep+e[j].w,x);
}
void suo(int x,int f,int n)
{
int cnt=0;
sz[x]=1;
for(int i=hd[x];i;i=e[i].nxt)
{
if(!v[e[i].v]&&e[i].v^f)
{
suo(e[i].v,x,n);
sz[x]+=sz[e[i].v];
cnt=max(cnt,sz[e[i].v]);
}
}
cnt=max(cnt,n-sz[x]);
if(cnt<mx)
mx=cnt,rt=x;
}
int findrt(int x,int n)
{
rt=0,mx=2000000000;
suo(x,0,n);
// printf("%d\n",mx);
return rt;
}
void tosz(int x,int f)
{
sz[x]=1;
for(int i=hd[x];i;i=e[i].nxt)
{
if(!v[e[i].v]&&e[i].v^f)
{
tosz(e[i].v,x);
sz[x]+=sz[e[i].v];
}
}
}
void dfs(int x)
{
// printf("hjhyyds:%d\n",x);
sou(x,m=0,0);
tosz(rt,0);
sort(a+1,a+m+1);
// for(int i=1;i<=m;i++)
// printf("%d %d\n",a[i].x,a[i].dep);
tp=0;
// if(x==5)
// puts("chtyyds:");
for(int i=1;i<=m;i++)
{
while(tp&&a[st[tp]].dep>=a[i].dep)
nxt[st[tp--]]=i;
st[++tp]=i;
// if(x==5)
// {
// for(int j=1;j<=tp;j++)
// printf("%d ",st[j]);
// puts("");
// }
}
// puts("qzmyyds");
tp=0;
for(int i=m;i>=1;i--)
{
while(tp&&a[st[tp]].dep>=a[i].dep)
lst[st[tp--]]=i;
st[++tp]=i;
}
for(int i=1;i<=m;i++)
{
if(lst[i])
p[++k]=(node){a[lst[i]].x,a[i].x,a[lst[i]].dep+a[i].dep} ;
if(nxt[i])
p[++k]=(node){a[i].x,a[nxt[i]].x,a[nxt[i]].dep+a[i].dep};
lst[i]=nxt[i]=0;
}
// if(x==5)
// {
// for(int i=1;i<=m;++i)
// printf("%d %d\n",a[i].x,a[i].dep);
// printf("xzd%%%:%d\n",nxt[1]);
// }
v[x]=1;
for(int i=hd[x];i;i=e[i].nxt)
if(!v[e[i].v])
dfs(findrt(e[i].v,sz[e[i].v]));
}
void update(int x,LL y)
{
// printf("%d %lld\n",x,y) ;
for(;x<=n;x+=x&-x)
tr[x]=min(tr[x],y);
}
LL query(int x)
{
// printf("qu:%d\n",x);
LL ret=1e18;
for(;x;x-=x&-x)
ret=min(ret,tr[x]);
// printf("%lld\n",ret);
return ret;
}
signed main()
{
memset(tr,0x7f,sizeof(tr));
scanf("%d",&n);
for(int i=1,u,v,w;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
scanf("%d",&q);
for(int i=1,y;i<=q;i++)
{
scanf("%d%d",f+i,&y);
g[y].push_back(i);
}
dfs(findrt(1,n));
sort(p+1,p+k+1);
// for(int i=1;i<=k;i++)
// printf("%d %d %lld\n",p[i].l,p[i].r,p[i].dep);
nwp=0;
for(int i=1;i<=n;i++)
{
while(nwp^k&&p[nwp+1].r==i)
{
++nwp;
update(n-p[nwp].l+1,p[nwp].dep);
}
for(int j=0;j<g[i].size();j++)
ans[g[i][j]]=query(n-f[g[i][j]]+1);
}
for(int i=1;i<=q;i++)
printf(ans[i]==INF? "-1\n":"%lld\n",ans[i]);
}
[gym104076][CCPC2022济南站L] Tree Distance的更多相关文章
- 2020ICPC济南站 J.Tree Constructer
题目大意:给定一棵N个顶点的树,顶点为1~N,对于一个序列A1,A2,-,An,若Ai | Aj == 2^60-1,则会连一条边(i,j).要求求出一个序列,可以唯一确定所给定的树. 思路:考虑到树 ...
- Distance on the tree
Distance on the tree https://nanti.jisuanke.com/t/38229 DSM(Data Structure Master) once learned abou ...
- 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- POJ 6621: K-th Closest Distance(主席树 + 二分)
K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)
K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
随机推荐
- datetime去除时分秒
datetime.datetime.now().replace(microsecond=0)
- hihocoder offer收割赛。。#1284
好久没刷题,水一水,反正排不上名次..这道题记下 我想着蛋疼的做了质因数分解,然后再算的因子个数..慢的一比,结果导致超时,还不如直接一个for循环搞定..也是醉了 最后其实就是算两个数的gcd,然后 ...
- Ubuntu SVN服务端安装方法
Ubuntu SVN服务端安装方法:https://blog.csdn.net/sm_wang/article/details/78656120https://www.cnblogs.com/myme ...
- Redis沙盒逃逸(CVE-2022-0543)漏洞复现
0x01 概述 Redis 是著名的开源 Key-Value 数据库,其具备在沙箱中执行 Lua 脚本的能力.Debian 以及 Ubuntu 发行版的源在打包 Redis 时,在 Lua 沙箱中遗留 ...
- GORM自定义Gorm.Model实现自动添加时间戳
废话不说直接开始 官网(http://gorm.io)有给出一套默认的gorm.Model模型,定义如下 package gorm import "time" // Model b ...
- 使用MySQL存储过程提高数据库效率和可维护性
MySQL 存储过程是一种强大的数据库功能,它允许你在数据库中存储和执行一组SQL语句,类似于编程中的函数.存储过程可以大幅提高数据库的性能.安全性和可维护性.本文将详细介绍MySQL存储过程的使用. ...
- DEDEBIZ禁止发布重复标题文章的方法
修改文件位置 /admin/article_add.php 找到 if (empty($click)) $click = ($cfg_arc_click == '-1' ? mt_rand(1000, ...
- Solution -「洛谷 P5046」「YunoOI 2019 模拟赛」Yuno loves sqrt technology I
Description Link. 无修改区间求逆序对. Solution 首先有一个显然的 \(\Theta(N\sqrt{N}\log_{2}N)\) 做法,由于过不了所以我就不废话. 其实有了 ...
- 【解惑】时间规划,Linq的Aggregate函数在计算会议重叠时间中的应用
在繁忙的周五,小悦坐在会议室里,面前摆满了各种文件和会议安排表.她今天的工作任务是为公司安排下周的50个小会议,这让她感到有些头疼.但是,她深吸了一口气,决定耐心地一个一个去处理. 首先,小悦仔细地收 ...
- Flask框架——模板、数据库ORM
文章目录 1 模板 1 重定向 1.1 什么是重定向? 1.2 为什么要有重定向? 1.3 如何使用重定向? 1.3.1 暂时性重定向(代码实例): 1.3.2 永久性重定向(代码实例) 2 jinj ...