結果

問題 No.399 動的な領主
ユーザー tko919tko919
提出日時 2020-12-06 15:14:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 2,373 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 181,980 KB
実行使用メモリ 16,528 KB
最終ジャッジ日時 2023-10-17 15:25:00
合計ジャッジ時間 6,066 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 17 ms
4,348 KB
testcase_06 AC 202 ms
11,512 KB
testcase_07 AC 199 ms
11,512 KB
testcase_08 AC 202 ms
11,776 KB
testcase_09 AC 205 ms
11,776 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 16 ms
4,348 KB
testcase_12 AC 177 ms
12,040 KB
testcase_13 AC 174 ms
12,040 KB
testcase_14 AC 131 ms
16,528 KB
testcase_15 AC 134 ms
16,528 KB
testcase_16 AC 153 ms
14,152 KB
testcase_17 AC 203 ms
11,776 KB
testcase_18 AC 202 ms
11,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

struct HLD{
   using P=pair<int,int>;
   vector<vector<int>> g; vector<int> sz,in,out,hs,par;
   void dfs(int v,int p){
      par[v]=p; sz[v]=1;
      if(!g[v].empty() and g[v][0]==p)swap(g[v][0],g[v].back());
      for(auto& to:g[v])if(to!=p){
         dfs(to,v); sz[v]+=sz[to];
         if(sz[g[v][0]]<sz[to])swap(g[v][0],to);
      }
   }
   void dfs2(int v,int p,int& k){
      in[v]=k++;
      for(auto& to:g[v])if(to!=p){
         hs[to]=(g[v][0]==to?hs[v]:to);
         dfs2(to,v,k);
      }
      out[v]=k;
   }
   HLD(int _n):g(_n),sz(_n),in(_n),out(_n),hs(_n),par(_n){}
   void add_edge(int u,int v){
      g[u].emplace_back(v); g[v].emplace_back(u);
   }
   void build(){dfs(0,-1); int k=0; dfs2(0,-1,k);}
   int lca(int u,int v){
      for(;;v=par[hs[v]]){
         if(in[u]>in[v])swap(u,v);
         if(hs[u]==hs[v])return u;
      }
   }
   vector<P> ps(int u,int v,bool es=0){
      vector<P> res;
      for(;;v=par[hs[v]]){
         if(in[u]>in[v])swap(u,v);
         if(hs[u]==hs[v])break;
         res.emplace_back(in[hs[v]],in[v]+1);
      }
      res.emplace_back(in[u]+es,in[v]+1);
      return res;
   }
};

template<typename T>struct BIT{
   int n; T m=0; vector<T> val;
   BIT(int _n):n(_n),val(_n+10){}
   void clear(){val.assign(n+10,0); m=0;}
   void add(int i,T x){
      for(i++;i<=n;i+=(i&-i))val[i]+=x;
      m+=x;
   }
   T sum(int i){
      T res=0;
      for(i++;i;i-=(i&-i))res+=val[i];
      return res;
   }
};

int main(){
   int n; cin>>n;
   HLD hld(n);
   rep(_,0,n-1){
      int x,y; cin>>x>>y; x--; y--;
      hld.add_edge(x,y);
   }
   hld.build();
   BIT<ll> bit(n);
   int q; cin>>q;
   while(q--){
      int a,b; cin>>a>>b; a--; b--;
      auto ps=hld.ps(a,b);
      for(auto& p:ps){
         bit.add(p.second,-1);
         bit.add(p.first,1);
      }
   }
   ll res=0;
   rep(i,0,n){
      ll x=bit.sum(i);
      res+=x*(x+1)/2;
   }
   cout<<res<<endl;
   return 0;
}
0