結果

問題 No.898 tri-βutree
ユーザー kotatsugamekotatsugame
提出日時 2019-10-12 02:03:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,844 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 84,716 KB
実行使用メモリ 33,824 KB
最終ジャッジ日時 2024-05-04 17:10:24
合計ジャッジ時間 10,315 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
33,824 KB
testcase_01 AC 3 ms
6,656 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   65 | main()
      | ^~~~

ソースコード

diff #

#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);
		int xyz=P.query(min(id[xy],id[yz]),max(id[xy],id[yz])+1);
		cout<<weighth[x]+weighth[y]+weighth[z]-weighth[xy]-weighth[yz]-weighth[zx]+weighth[xyz]<<endl;
	}
}
0