1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * ޱ BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n,m,q; vector<int>v[300005]; int st[20][300005],fa[300005],dep[300005],dis[300005],dfn[300005]; int tot; int getmax(int x,int y){return dep[x]<dep[y]? x:y;} void dfs(int x,int pre,int d){ st[0][dfn[x]=tot++]=pre;dep[x]=d;fa[x]=pre; for(auto i:v[x])if(i!=pre)dfs(i,x,d+1); } int getlca(int x,int y){ if(x==y)return x; if((x=dfn[x])>(y=dfn[y]))swap(x,y); int s=__lg(y-(++x)+1); return getmax(st[s][x],st[s][y-(1<<s)+1]); } int sum[300005]; int add[300005]; int val[300005]; void dfs2(int x,int pre){ val[x]=add[x]-sum[x]; dis[x]+=sum[x]; for(auto i:v[x])if(i!=pre){ dis[i]=dis[x]; dfs2(i,x); val[x]+=val[i]-sum[i]; } } void Main() { cin>>n>>m>>q; REP(i,1,n){ int x,y; cin>>x>>y; --x,--y; v[x].pb(y);v[y].pb(x); } dfs(0,-1,0); REP(j,0,__lg(n-1)){ REP(i,1,n-(1<<(j+1))+1)st[j+1][i]=getmax(st[j][i],st[j][i+(1<<j)]); } while(m--){ int x,y,z; cin>>x>>y>>z; --x,--y; int lca=getlca(x,y); sum[lca]+=z; add[x]+=z;add[y]+=z; } dfs2(0,-1); while(q--){ int x,y;cin>>x>>y; --x,--y; int lca=getlca(x,y); cout<<dis[x]+dis[y]-2*dis[lca]+val[lca]<<'\n'; } } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("soong.in","r",stdin); freopen("soong.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 3105
- Time
- 3000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By