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条~第 ...
随机推荐
- jQuery与Vue的区别、从jQuery到Vue框架优点总结
一.两者的区别 1.数据与视图分离 2.数据驱动视图 二.从jQuery到Vue框架的总结 1.数据与视图分离,解耦 2.数据驱动视图,只关心数据,DOM操作已经被框架封装
- 问题 |无法找到Python路径,需手动配置环境变量
问题: 在命令行cmd输入Python,如果出现以下无法识别命令行的报错,说明在系统环境变量中无法找到对应之前安装的Python的路径,则需手动配置一下 怎么配置? 1.打开我的电脑——右键——属性— ...
- ubuntu终端仿真程序和文件管理程序
1.SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单的说是Windows下登录UNIX或Linux服务器主机的软件.可以理解为ubuntu下的Terminal. 如果Sec ...
- div + css 边框 虚线
div + css 边框 虚线 dotted:[点线|有点的|点线式边框|点虚线] .introduce { border:1px dotted gray; margin:8px 5px 8px 10 ...
- pycharm中evaluate expression的用法
pycharm中evaluate expression的用法 首先要用debug调试模式运行程序,在代码编辑处右键debug,或着选择右上角的小虫子图标点击. 然后保证整个程序运行的时候可以中断,然后 ...
- Java8向后兼容
toInstant()方法被添加到可用于将它们转换到新的日期时间的API原始日期和日历对象.使用ofInstant(Insant,ZoneId)方法得到一个LocalDateTime或ZonedDat ...
- 使ie8正常支持placeholder
在IE8下测试,发现一个问题placeholder不被支持,下面是解决IE支持placeholder的方法,本文引用的jquery是1.12.0测试通过,先引用jquery <script ty ...
- win10命令行kill进程
1. 查:netstat -ano | findstr "8080" 2. 杀:taskkill -PID 8082 -F
- c# mvc 简洁大气官网---源码
结构
- 标准 IO fread 与 fwrite 的使用(可以实现二进制流的读写)
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void ...