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条~第 ...
随机推荐
- Java中使用try-catch-finally处理IO流中的异常
我们使用try-catch-finally来接收IO流的异常 finally是最后执行的步骤,非常适合最后存放close来关闭IO流,而且编程中我们不可以随意抛出异常,必须对异常进行处理. 从try- ...
- php上传文件(简易版)
1.前台form表单处添加 enctype="multipart/form-data" 2. //上传文件保存到目录 header("content-type:text/ ...
- MySql中创建存储过程
MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...
- 73 QT编程入门
0 引言 最近开始在QT下编程,记录一下遇到的问题以及解决方法. 1 安装下载及学习资料 (1)安装下载链接 安装链接: https://blog.csdn.net/qq_23473839/artic ...
- Thymeleaf 模板布局三种区别
- mongodb副本集(选举,节点设置,读写分离设置)
1.相对于传统主从模式的优势 传统的主从模式,需要手工指定集群中的Master.如果Master发生故障,一般都是人工介入,指定新的Master.这个过程对于应用一般不是透明的,往往伴随着应用重新修改 ...
- mysql查询相关的命令解析
特:不重启mysql 更新配置文件方法(不允许重启mysql实例或连接不上msyql服务器): gdb -p $(pidof mysqld) -ex "set max_connections ...
- idea中如何查看jar包中的源码(非maven),以oracle的ojdbc为例
文章目录 背景 解决 背景 工作需要查看oracle的部分源码(ojdbc.jar),maven并没有这个依赖,单纯的导入jar包无法查看. 解决 将ojdbc.jar 安装到本地仓库,maven从本 ...
- python_way day15 HTML-DAY2、 回顾HTML-CSS 标签(css强制生效),JS(数据类型,时间处理,作用域)
python_way day15 HTML-DAY2 html-css回顾 javascript 一.html-css回顾 增加默认值: . 强制生效,就算在上面,被覆盖上了也会生效 解决缩小页面混乱 ...
- mysql动态列--统计报表信息对比
SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'MAX(IF(tmp.summary = ''', tp.summary, ''', tm ...