結果

問題 No.399 動的な領主
ユーザー definedefine
提出日時 2020-02-16 15:45:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 751 ms / 2,000 ms
コード長 4,316 bytes
コンパイル時間 2,641 ms
コンパイル使用メモリ 186,972 KB
実行使用メモリ 60,768 KB
最終ジャッジ日時 2024-04-16 03:36:15
合計ジャッジ時間 10,202 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 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 4 ms
5,376 KB
testcase_05 AC 39 ms
7,936 KB
testcase_06 AC 733 ms
50,180 KB
testcase_07 AC 751 ms
50,184 KB
testcase_08 AC 703 ms
50,996 KB
testcase_09 AC 710 ms
50,868 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 30 ms
9,144 KB
testcase_12 AC 463 ms
59,412 KB
testcase_13 AC 453 ms
59,368 KB
testcase_14 AC 222 ms
60,768 KB
testcase_15 AC 313 ms
60,640 KB
testcase_16 AC 342 ms
60,104 KB
testcase_17 AC 717 ms
50,940 KB
testcase_18 AC 714 ms
50,904 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'void HeavyLightDecomposition::make_path(long long int, long long int, long long int)':
main.cpp:140:25: warning: 'mxindex' may be used uninitialized [-Wmaybe-uninitialized]
  140 |                         if(mxindex==i){
      |                         ^~
main.cpp:133:21: note: 'mxindex' was declared here
  133 |                 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)

#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<int>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);
		dat[k]=dat[k*2+1]+dat[k*2+2];
	}
	int 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;
		sz=1;
		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());

	int 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