hdu6110
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
struct fastio{
char s[100005];
int it,len;
fastio(){it=len=0;}
inline char get(){
if(it<len)return s[it++];it=0;
len=fread(s,1,100000,stdin);
if(len==0)return EOF;else return s[it++];
}
bool notend(){
char c=get();
while(c==' '||c=='\n')c=get();
if(it>0)it--;
return c!=EOF;
}
}BUFF;
#define read(x) x=getnum()
#define write(x) putnum(x),putchar(' ')
#define writeln(x) putnum(x),putchar('\n')
inline LL getnum(){
LL r=0;bool ng=0;char c;c=BUFF.get();
while(c!='-'&&(c<'0'||c>'9'))c=BUFF.get();
if(c=='-')ng=1,c=BUFF.get();
while(c>='0'&&c<='9')r=r*10+c-'0',c=BUFF.get();
return ng?-r:r;
}
template<class T> inline void putnum(T x){
if(x<0)putchar('-'),x=-x;
register short a[20]={},sz=0;
while(x)a[sz++]=x%10,x/=10;
if(sz==0)putchar('0');
for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=BUFF.get();while(c<=32)c=BUFF.get();return c;}
const int MXN = 5e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, m, s;
int head[MXN], tot;
int up[MXN][21], dep[MXN], sum[MXN];
struct lp{
int v, nex, w;
}cw[MXN*3];
void add_1(int a,int b,int c){
cw[++tot].v=b;cw[tot].nex=head[a];cw[tot].w=c;
head[a]=tot;
cw[++tot].v=a;cw[tot].nex=head[b];cw[tot].w=c;
head[b]=tot;
}
void search(int u,int ba,int d) {
dep[u] = d;
up[u][0] = ba;
for(int i = 1; i < 20; ++i) {
up[u][i] = up[up[u][i-1]][i-1];
}
for(int i = head[u]; ~i; i = cw[i].nex) {
int v = cw[i].v;
if(v == ba) continue;
up[v][0] = u;
sum[v] = sum[u] + cw[i].w;
search(v, u, d + 1);
}
}
int LCA(int x,int y) {
if(dep[x] < dep[y]) swap(x, y);
int k = dep[x] - dep[y];
for(int i = 0; i < 20; ++i) {
if((1<<i)&k) {
x = up[x][i];
}
}
if(x == y) return x;
for(int i = 19; i >= 0; --i) {
if(up[x][i] != up[y][i]) {
x = up[x][i];
y = up[y][i];
}
}
return up[x][0];
}
pair<int,int> tree[MXN<<2];
bool cmp(int a,int b) {
return dep[a] > dep[b];
}
pii UN(pii x, pii y) {
int a[4];
a[0] = LCA(x.fi,y.fi), a[1] = LCA(x.fi,y.se);
a[2] = LCA(x.se,y.fi), a[3] = LCA(x.se,y.se);
sort(a, a + 4, cmp);
return pii(a[0], a[1]);
}
void build(int l,int r,int rt) {
if(l == r) {
scanf("%d%d",&tree[rt].fi,&tree[rt].se);
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);
tree[rt] = UN(tree[rt<<1], tree[rt<<1|1]);
}
pii query(int L,int R,int l,int r,int rt) {
if(L <= l && r <= R) {
return tree[rt];
}
int mid = (l + r) >> 1;
if(L > mid) return query(L, R, mid+1, r, rt<<1|1);
else if(R <= mid) return query(L,R,l,mid,rt<<1);
return UN(query(L,mid,l,mid,rt<<1),query(mid+1,R,mid+1,r,rt<<1|1));
}
int main(){
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; ++i) {
sum[i] = 0;dep[i] = 0;
for(int j = 0; j < 20; ++j) up[i][j] = 0;
head[i] = -1;
}
tot = -1;
for(int i = 1, a, b, c; i < n; ++i) {
scanf("%d%d%d", &a, &b, &c);
add_1(a, b,c);
}
search(1, 1, 1);
scanf("%d", &m);
build(1,m,1);
scanf("%d", &s);
for(int tim = 0, l, r; tim < s; ++ tim) {
scanf("%d%d", &l, &r);
pii ans = query(l,r,1,m,1);
printf("%d\n", sum[ans.fi]+sum[ans.se]-2*sum[LCA(ans.fi,ans.se)]);
}
}
return 0;
}
hdu6110的更多相关文章
- hdu6110:路径交
$n \leq 500000$的树给$m \leq 500000$个路径,$q \leq 500000$个询问每次问一个区间的路径交. 路径交口诀:(前方高能) 判有交,此链有彼祖: 取其交,最深两两 ...
- hdu6110(线段树+lca)
题目 http://acm.hdu.edu.cn/showproblem.php?pid=6110 分析 注意到,若干条路径的交一定也是条路径 我们可以维护一个线段树,seg[l..r]存着第l条~第 ...
随机推荐
- String的static方法
//String concat(String str) 拼接字符串 String concat_str0 = "abc"; String concat_str1 = "b ...
- 【leetcode】951. Flip Equivalent Binary Trees
题目如下: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the ...
- Shell基础(一):Shell基础应用、简单Shell脚本的设计、使用Shell变量、变量的扩展应用
一.Shell基础应用 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 切换用户的Shell环境 2> 练习命令历史.命令别名 3 ...
- 在使用element-ui搭建的表格中,实现点击"定位"按钮后,屏幕滚动到对应行的位置
背景: 一个后台管理系统,当管理员登录之后,会存在一个自己的id值, 在一个表格中,当点击"定位"按钮后,屏幕滚动到拥有管理员id的这一行,并且给设置一个高亮的背景 相关知识点: ...
- Linux/UNIX上安装Mysql
接下来我们在 Centos 系统下使用 yum 命令安装 MySql: 检测系统是否自带安装 mysql: rpm -qa | grep mysql 如果你系统有安装,那可以选择进行卸载: rpm - ...
- matlab中的 ndims(a)、length(a)、size(a) 分别是什么意思?
size(a)表示矩阵每个维度的长度比如size([1 2 3;4 5 6])等于[2 3]表示他有2行3列size([1 2 3])等于[1 3]表示他有1行3列另外size(a,n)表示矩阵a在第 ...
- HTML-参考手册: Px、Em 换算工具
ylbtech-HTML-参考手册: Px.Em 换算工具 1.返回顶部 1. Px.Em 换算工具 以下工具提供了em和px的换算工具. 第一个输入框:设置了网页默认的字体像素 (通常 16px) ...
- hexo的next主题博客中加入分类页面的js,实现多级目录,并且能够点击展开,隐藏下级目录~(不知道算不算深度优化~~~)
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 多级标题 在自己的xxxx.md文件中做如下修 ...
- vim与ctags/cscope的完美结合
1. 安装vim/ctags/cscope ctag 2. 在源码根目录下执行 sudo ctags -R . 会生成tags文件,里面包含着整个源码目录下的符号信息. 3. 直接到达某个符号(比 ...
- PAT_A1025#PAT Ranking
Source: PAT A1025 PAT Ranking Description: Programming Ability Test (PAT) is organized by the Colleg ...