結果

問題 No.399 動的な領主
ユーザー definedefine
提出日時 2020-02-16 15:06:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,296 bytes
コンパイル時間 2,818 ms
コンパイル使用メモリ 192,616 KB
実行使用メモリ 42,376 KB
最終ジャッジ日時 2024-04-16 03:35:54
合計ジャッジ時間 6,742 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,760 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 73 ms
7,928 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'void HeavyLightDecomposition::make_path(int, int, int)':
main.cpp:139:25: warning: 'mxindex' may be used uninitialized [-Wmaybe-uninitialized]
  139 |                         if(mxindex==i){
      |                         ^~
main.cpp:132:21: note: 'mxindex' was declared here
  132 |                 int mxindex,mx=0;
      |                     ^~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define all(v) v.begin(),v.end()
#define inf (int)(3e18)
#define P pair<int,int>
#define mod (int)(1e9+7)
#define ll long long

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

template<class T> inline void chmin(T &a, T b) {
	a = min(a, b);
}
template<class T> inline void chmax(T &a, T b) {
	a = max(a, b);
}

struct Tree{
	int root,N;
	vector<vector<int>>G,doubling;
	vector<int>depth;
	Tree(int x,int r){
		N=x;G.resize(N);root=r;
		doubling.resize(20);
		rep(i,20)doubling[i].resize(N);
		depth.resize(N,-1);depth[root]=0;
	}
	void unite(int x,int y){
		G[x].push_back(y);G[y].push_back(x);
	}
	void dfs(int x){
		for(int i:G[x]){
			if(depth[i]==-1){
				depth[i]=depth[x]+1;
				doubling[0][i]=x;
				dfs(i);
			}
		}
	}
	void init(){
		dfs(root);
		rep(i,19){
			rep(j,N){
				if(doubling[i][j]==-1)doubling[i+1][j]=-1;
				else doubling[i+1][j]=doubling[i][doubling[i][j]];
			}
		}
	}
	int lca(int x,int y){
		if(depth[x]>depth[y])swap(x,y);
		rep(i,20){
			if((depth[y]-depth[x])>>i&1){
				y=doubling[i][y];
			}
		}
		if(x==y)return x;
		for(int i=19;i>=0;i--){
			if(doubling[i][x]!=doubling[i][y]){
				x=doubling[i][x];y=doubling[i][y];
			}
		}
		return doubling[0][x];
	}
};

//RAQ-RSQ
struct Segtree{
	vector<ll>dat,lazy;
	int size=1;
	Segtree(int x){
		while(size<x)size*=2;
		dat.resize(size*2-1);
		lazy.resize(size*2-1);
	}
	void eval(int k,int l,int r){
		dat[k]+=lazy[k]*(r-l);
		if(r-l>1){
			lazy[k*2+1]+=lazy[k];
			lazy[k*2+2]+=lazy[k];
		}
		lazy[k]=0;
	}
	void update(int a,int b,int x,int k=0,int l=0,int r=-1){
		if(r==-1)r=size;
		eval(k,l,r);
		if(r<=a||b<=l)return;
		if(a<=l&&r<=b){
			lazy[k]+=x;
			eval(k,l,r);
			return;
		}
		update(a,b,x,k*2+1,l,(l+r)/2);
		update(a,b,x,k*2+2,(l+r)/2,r);
	}
	ll query(int a,int b,int k=0,int l=0,int r=-1){
		if(r==-1)r=size;
		eval(k,l,r);
		if(r<=a||b<=l)return 0;
		if(a<=l&&r<=b){
			return dat[k];
		}
		int lv=query(a,b,k*2+1,l,(l+r)/2);
		int rv=query(a,b,k*2+2,(l+r)/2,r);
		return lv+rv;
	}
};

struct HeavyLightDecomposition{
	struct heavy_set{
		vector<int>element;
		int parent;
		heavy_set(int v,int par):element(1,v),parent(par){}
	};
	vector<vector<int>>G;
	vector<heavy_set>S;
	vector<int>subtree_size,set_index,ele_index;

	int subtree(int v,int par){
		int &sz=subtree_size[v];
		if(sz>0)return sz;
		for(int i:G[v])if(i!=par)sz+=subtree(i,v);
		return sz;
	}
	void make_path(int v,int par,int id){
		set_index[v]=id;
		ele_index[v]=S[id].element.size()-1;
		int mxindex,mx=0;
		for(int i:G[v])if(i!=par){
			if(mx<subtree(i,v)){
				mx=subtree(i,v);mxindex=i;
			}
		}
		for(int i:G[v])if(i!=par){
			if(mxindex==i){
				S[id].element.push_back(i);
				make_path(i,v,id);
			}else {
				S.emplace_back(i,v);
				make_path(i,v,S.size()-1);
			}
		}
	}
	void init(int root){
		subtree_size.resize(G.size());
		set_index.resize(G.size());
		ele_index.resize(G.size());
		S.emplace_back(root,root);
		make_path(root,root,0);
		subtree_size.clear();
	}
	HeavyLightDecomposition(vector<vector<int>>&G,int root=0):G(G){
		init(root);
	}
	P get(int v){
		return {set_index[v],ele_index[v]};
	}
};

signed main() {
	int N;
	cin>>N;
	vector<vector<int>>G(N);
	Tree tree(N,0);
	rep(i,N-1){
		int u,v;cin>>u>>v;u--;v--;
		G[u].push_back(v);G[v].push_back(u);
		tree.unite(u,v);
	}
	tree.init();
	HeavyLightDecomposition hld(G);
	vector<Segtree>segtree;
	rep(i,hld.S.size())segtree.emplace_back(hld.S[i].element.size());

	ll ans=0;
	int Q;cin>>Q;
	while(Q--){
		int u,v;cin>>u>>v;u--;v--;
		int lca=tree.lca(u,v);
		while(hld.get(v).first!=hld.get(lca).first){
			P tmp=hld.get(v);
			segtree[tmp.first].update(0,tmp.second+1,1);
			ans+=segtree[tmp.first].query(0,tmp.second+1);
			v=hld.S[tmp.first].parent;
		}
		while(hld.get(u).first!=hld.get(lca).first){
			P tmp=hld.get(u);
			segtree[tmp.first].update(0,tmp.second+1,1);
			ans+=segtree[tmp.first].query(0,tmp.second+1);
			u=hld.S[tmp.first].parent;
		}
		P tu=hld.get(u),tv=hld.get(v);
		int mn=min(tu.second,tv.second),mx=max(tu.second,tv.second);
		segtree[tu.first].update(mn,mx+1,1);
		ans+=segtree[tu.first].query(mn,mx+1);
	}
	cout<<ans<<endl;
}
0