結果

問題 No.898 tri-βutree
ユーザー kotatsugamekotatsugame
提出日時 2019-10-12 02:11:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 398 ms / 4,000 ms
コード長 1,771 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 84,468 KB
実行使用メモリ 33,752 KB
最終ジャッジ日時 2023-08-08 16:26:31
合計ジャッジ時間 9,622 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 315 ms
33,752 KB
testcase_01 AC 3 ms
6,584 KB
testcase_02 AC 4 ms
6,508 KB
testcase_03 AC 4 ms
6,448 KB
testcase_04 AC 3 ms
6,624 KB
testcase_05 AC 4 ms
6,548 KB
testcase_06 AC 4 ms
6,452 KB
testcase_07 AC 390 ms
26,456 KB
testcase_08 AC 397 ms
26,512 KB
testcase_09 AC 394 ms
26,472 KB
testcase_10 AC 398 ms
26,616 KB
testcase_11 AC 392 ms
26,508 KB
testcase_12 AC 396 ms
26,548 KB
testcase_13 AC 394 ms
26,428 KB
testcase_14 AC 398 ms
26,596 KB
testcase_15 AC 388 ms
26,416 KB
testcase_16 AC 397 ms
26,420 KB
testcase_17 AC 388 ms
26,552 KB
testcase_18 AC 394 ms
26,364 KB
testcase_19 AC 393 ms
26,452 KB
testcase_20 AC 391 ms
26,508 KB
testcase_21 AC 391 ms
26,500 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:65:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-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);
		cout<<weighth[x]+weighth[y]+weighth[z]-weighth[xy]-weighth[yz]-weighth[zx]<<endl;
	}
}
0