XJOI网上同步训练DAY3 T2

时间:2023-12-31 20:18:38

XJOI网上同步训练DAY3 T2

XJOI网上同步训练DAY3 T2

考试的时候已经想出来怎么做了,但是没有时间打了T_T

思路:我们考虑将询问以lim排序,然后树链剖分,把边作为线段树的节点,然后随着询问lim的增大,改变线段树中节点的信息,然后每次询问我们用树链剖分询问,复杂度是O(nlogn),又get一种新的树链剖分打法

 #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
struct node{
int x,y,lim,id;
}q[];
int V[],dep[],t[],dfn[],num,n,m;
struct Data{
int l,r,s,v;
Data(){}
Data(int a,int b,int c,int d):l(a),r(b),s(c),v(d){}
}s1[],s2[];
Data operator +(Data a,Data b){
Data ret(a.l,b.r,a.s+b.s,a.v+b.v);
if (a.r&&b.l) ret.v=a.v+b.v-V[a.r]-V[b.l]+V[a.r+b.l];
if (b.l==b.s) ret.r=a.r+b.s;
if (a.l==a.s) ret.l=b.l+a.s;
return ret;
}
int tot,go[],next[],first[];
int size[],son[],top[],fa[];
int ans[],val[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y,int z){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
val[tot]=z;
}
void add(int x,int y,int z){
insert(x,y,z);insert(y,x,z);
}
bool cmp(node a,node b){
return a.lim<b.lim;
}
bool cmp1(int x,int y){
return val[x]<val[y];
}
void build(int k,int l,int r){
if (l==r){
s1[k]=s2[k]=Data(,,,V[]);
return;
}
int mid=(l+r)/;
build(k*,l,mid);
build(k*+,mid+,r);
s1[k]=s1[k*]+s1[k*+];
s2[k]=s2[k*+]+s2[k*];
}
void dfs1(int x,int f){
size[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur!=f){
dep[pur]=dep[x]+;
t[++t[]]=i;
dfs1(pur,x);
size[x]+=size[pur];
if (size[pur]>size[son[x]]) son[x]=pur;
}
}
}
void dfs2(int x,int f){
dfn[x]=++num;
if (son[x]) top[son[x]]=top[x],dfs2(son[x],x);
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==f||pur==son[x]) continue;
top[pur]=pur;
fa[pur]=x;
dfs2(pur,x);
}
}
void modify(int k,int l,int r,int pos){
if (l==r){
s1[k]=s2[k]=Data(,,,);
return;
}
int mid=(l+r)/;
if (pos<=mid) modify(k*,l,mid,pos);
else modify(k*+,mid+,r,pos);
s1[k]=s1[k*]+s1[k*+];
s2[k]=s2[k*+]+s2[k*];
}
Data ask1(int k,int l,int r,int x,int y){
if (l==x&&r==y){
return s1[k];
}
int mid=(l+r)/;
if (y<=mid) return ask1(k*,l,mid,x,y);
else
if (x>mid) return ask1(k*+,mid+,r,x,y);
else return ask1(k*,l,mid,x,mid)+ask1(k*+,mid+,r,mid+,y);
}
Data ask2(int k,int l,int r,int x,int y){
if (l==x&&r==y){
return s2[k];
}
int mid=(l+r)/;
if (y<=mid) return ask2(k*,l,mid,x,y);
else
if (x>mid) return ask2(k*+,mid+,r,x,y);
else return ask2(k*+,mid+,r,mid+,y)+ask2(k*,l,mid,x,mid);
}
int work(int x,int y){
Data ans1(,,,),ans2(,,,);
while (top[x]!=top[y]){
if (dep[top[x]]<dep[top[y]]){
ans2=ask1(,,n,dfn[top[y]],dfn[y])+ans2;
y=fa[top[y]];
}else{
ans1=ans1+ask2(,,n,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
}
if (x!=y){
if (dep[x]<dep[y]){
ans2=ask1(,,n,dfn[son[x]],dfn[y])+ans2;
}else{
ans1=ans1+ask2(,,n,dfn[son[y]],dfn[x]);
}
}
return (ans1+ans2).v;
}
int main(){
freopen("tx.in","r",stdin);
n=read();
for (int i=;i<n;i++) V[i]=read();
for (int i=;i<n;i++){
int x=read()+,y=read()+,v=read();
add(x,y,v);
}
m=read();
for (int i=;i<=m;i++){
q[i].x=read()+,q[i].y=read()+,q[i].lim=read();q[i].id=i;
}
dfs1(,);dfs2(,);
std::sort(q+,q++m,cmp);
std::sort(t+,t++t[],cmp1);
int h=;build(,,n);
for (int i=;i<=m;i++){
while (val[t[h]]<=q[i].lim&&h<=t[]){
modify(,,n,dfn[go[t[h]]]);
h++;
}
ans[q[i].id]=work(q[i].x,q[i].y);
}
for (int i=;i<=m;i++) printf("%d\n",ans[i]);
}