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条~第 ...
随机推荐
- vue 页面回退mounted函数不执行的问题及解决方法
前言 最近做项目碰到一个很头大的问题--从a页面跳到b页面进行编辑,编辑完再返回a页面,却没走a页面的钩子函数mounted,数据没有更新 经过一番面向百度研究,终于找到了问题所在.接下来就记录一下这 ...
- springboot集成使用rabbitmq笔记(2.rabbitmq使用)
使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.引入包 <dependencies> <dependency> <groupId& ...
- vue项目base64转img
最近一个vue项目中,有个需求是, 发送消息是base64时,转换成图片预览发送出去. 输入框: <el-input class="input-box" ref=" ...
- lct 模版题 bzoj 2002 2049
很早就有人给我推荐的模版题,然后我最近才刷的(' ' ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊! bzoj 2002 弹飞绵羊 重点:这道题的cut和link 由于这道题链 ...
- Tyvj 1518 CPU监控(线段树)
题目描述: Bob需要一个程序来监视CPU使用率.这是一个很繁琐的过程,为了让问题更加简单,Bob会慢慢列出今天会在用计算机时做什么事. Bob会干很多事,除了跑暴力程序看视频之外,还会做出去玩玩和用 ...
- (转)OpenFire源码学习之十:连接管理(上)
转:http://blog.csdn.net/huwenfeng_2011/article/details/43415827 关于连接管理分为上下两部分 连接管理 在大并发环境下,连接资源 需要随着用 ...
- LInux多线程编程----线程属性pthread_attr_t
1.每个POSIX线程有一个相连的属性对象来表示属性.线程属性对象的类型是pthread_attr_t,pthread_attr_t 在文件/usr/include/bits/pthreadtypes ...
- java Logger 类
package org.rx.common; import org.slf4j.LoggerFactory; import java.util.Collections; import java.uti ...
- 22、继续javascript,左边选中的跳到右边
1. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...
- JVM简介及类加载机制(一)
JVM介绍: 目标:JVM运行字节码文件,根据JVM的日志调节程序,对于底层原理有一定的了解 1. 类加载 在JAVA代码中,类型的加载,连接与初始化都是在程序运行期间完成的,提供了灵活性增加了更多的 ...