結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-12 02:11:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 429 ms / 4,000 ms |
| コード長 | 1,771 bytes |
| コンパイル時間 | 1,130 ms |
| コンパイル使用メモリ | 84,908 KB |
| 実行使用メモリ | 33,956 KB |
| 最終ジャッジ日時 | 2024-11-08 23:06:21 |
| 合計ジャッジ時間 | 10,049 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
コンパイルメッセージ
main.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main()
| ^~~~
ソースコード
#include<iostream>
#include<vector>
using namespace std;
//Disjoint Sparse Table
#include<vector>
#include<functional>
template<typename T>
struct DST{
function<T(T,T)>calcfn;
int n;
vector<vector<T> >dat;
DST(const vector<T>&v={},
function<T(T,T)>calcfn_=[](T a,T b){return a<b?a:b;}
):calcfn(calcfn_)
{
n=v.size();
dat.push_back(v);
for(int i=2;i<n;i<<=1)
{
dat.push_back(vector<T>(n));
for(int j=i;j<n;j+=i<<1)
{
dat.back()[j-1]=dat[0][j-1];
for(int k=2;k<=i;k++)
{
dat.back()[j-k]=calcfn(dat[0][j-k],dat.back()[j-k+1]);
}
dat.back()[j]=dat[0][j];
for(int k=2;k<=i&&j+k<=n;k++)
{
dat.back()[j+k-1]=calcfn(dat[0][j+k-1],dat.back()[j+k-2]);
}
}
}
}
T query(int l,int r)const//[l,r)
{
if(l<0)l=0;
if(r>n)r=n;
r--;
if(l==r)return dat[0][l];
int k=31-__builtin_clz(l^r);
return calcfn(dat[k][l],dat[k][r]);
}
};
int N;
vector<pair<int,int> >G[1<<17];
int depth[1<<17];
long weighth[1<<17];
int id[1<<17];
vector<int>euler;
void dfs(int u,int p,int d,long wei)
{
depth[u]=d;
weighth[u]=wei;
id[u]=euler.size();
euler.push_back(u);
for(pair<int,int>q:G[u])
{
if(q.first==p)continue;
dfs(q.first,u,d+1,wei+q.second);
euler.push_back(u);
}
}
main()
{
cin>>N;
for(int i=1;i<N;i++)
{
int u,v,w;cin>>u>>v>>w;
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
}
dfs(0,-1,0,0);
DST<int>P(euler,[](int x,int y){return depth[x]<depth[y]?x:y;});
int Q;
cin>>Q;
for(;Q--;)
{
int x,y,z;cin>>x>>y>>z;
int xy=P.query(min(id[x],id[y]),max(id[x],id[y])+1);
int yz=P.query(min(id[y],id[z]),max(id[y],id[z])+1);
int zx=P.query(min(id[z],id[x]),max(id[z],id[x])+1);
cout<<weighth[x]+weighth[y]+weighth[z]-weighth[xy]-weighth[yz]-weighth[zx]<<endl;
}
}