結果

問題 No.901 K-ary εxtrεεmε
ユーザー snow39snow39
提出日時 2020-03-06 15:51:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 6,007 bytes
コンパイル時間 2,036 ms
コンパイル使用メモリ 126,620 KB
実行使用メモリ 28,032 KB
最終ジャッジ日時 2024-04-22 03:58:17
合計ジャッジ時間 10,155 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
28,032 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 276 ms
25,216 KB
testcase_08 AC 277 ms
25,216 KB
testcase_09 AC 268 ms
25,088 KB
testcase_10 AC 276 ms
25,344 KB
testcase_11 AC 275 ms
25,216 KB
testcase_12 AC 242 ms
25,216 KB
testcase_13 AC 242 ms
25,216 KB
testcase_14 AC 237 ms
25,216 KB
testcase_15 AC 248 ms
25,284 KB
testcase_16 AC 247 ms
25,216 KB
testcase_17 AC 361 ms
25,272 KB
testcase_18 AC 371 ms
25,216 KB
testcase_19 AC 360 ms
25,088 KB
testcase_20 AC 361 ms
25,216 KB
testcase_21 AC 368 ms
25,216 KB
testcase_22 AC 187 ms
25,216 KB
testcase_23 AC 188 ms
25,088 KB
testcase_24 AC 186 ms
25,216 KB
testcase_25 AC 179 ms
25,216 KB
testcase_26 AC 186 ms
25,088 KB
testcase_27 AC 279 ms
25,216 KB
testcase_28 AC 272 ms
25,216 KB
testcase_29 AC 273 ms
25,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>
#include <climits>

//SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX);
template< typename T>
class SegmentTree{
public:

  using F = function<T(T,T)>;

  int n;
  vector<T> tree;
  F operation;
  T def;

  SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  SegmentTree(){}

  void embody(int size,F _operation,T _def){
    operation=_operation;
    def=_def;
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  void initialize(vector<T> v){
    int vSize=v.size();
    n=1;
    while(n<vSize) n*=2;
    tree.resize(2*n-1,def);

    for(int i=0;i<vSize;i++) tree[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]);
  }

  void update(int index,T value){
    index+=n-1;

    tree[index]=value;
    while(index>0){
      index=(index-1)/2;
      tree[index]=operation(tree[2*index+1],tree[2*index+2]);
    }
  }

  T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b),getMin(a,b,0,0,-1)
    if(r<0) r=n;

    if(r<=a||b<=l) return def;
    else if(a<=l&&r<=b) return tree[k];
    else{
      T lval=query(a,b,2*k+1,l,(l+r)/2);
      T rval=query(a,b,2*k+2,(l+r)/2,r);
      return operation(lval,rval);
    }
  }

  int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal)
    if(r<0) r=n;
    if(r-l==1) return k-(n-1);
    if(tree[2*k+1]>=x) return find(x,2*k+1,l,(l+r)/2);
    else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r);
  }
};

struct Edge{
    int to,id;
    ll dist;
    Edge(int to,ll dist=1,int id=0):to(to),dist(dist),id(id){}
};

// this class's update method is empty.
struct HeavyLightDecomposition{
    vector<vector<Edge>> g;
    vector<int> in,out,head,par,dep,sz;
    int times;
    int root;
    vector<int> mad;

    HeavyLightDecomposition(int V,vector<vector<Edge>> &G,int root=0):
        g(G),in(V),out(V),head(V),par(V),dep(V),sz(V),root(root){
            times=0;
            sz_dfs(root,-1);
            hld_dfs(root,-1);
    }

    void init(){
        mad.resize((int)g.size());
        rep(i,mad.size()) mad[i]=-1;
    }
    
    void sz_dfs(int now,int p){
        par[now]=p;
        sz[now]=1;
        if(p==-1) dep[now]=0;
        else dep[now]=dep[p]+1;

        for(auto &e:g[now]){
            if(e.to==p) continue;
            sz_dfs(e.to,now);
            sz[now]+=sz[e.to];
            if(sz[e.to]>sz[g[now][0].to]) swap(e,g[now][0]);
        }
    }

    void hld_dfs(int now,int p){
        in[now]=times++;
        for(auto e:g[now]){
            if(e.to==p) continue;
            head[e.to]=(e.to == g[now][0].to ? head[now] : e.to);
            hld_dfs(e.to,now);
        }
        out[now]=times;
    }


    int lca(int u,int v){
        for(;;v=par[head[v]]){
            if(in[u]>in[v]) swap(u,v);
            if(head[u]==head[v]) return u;
        }
    }
    int distance(int u,int v){
        return dep[u]+dep[v]-2*dep[lca(u,v)];
    }

    /*ex)
        hld.update(u,x,[&](int a,int b,ll x){
            seg.add(a,b,x);
        });
    */
    template<class F>
    void update(int u,int v,const F &f){//辺の時は0を使わない(INFのまま)
        for(;;v=par[head[v]]){
            if(in[u]>in[v]) swap(u,v);
            if(head[u]==head[v]) break;
            if(mad[head[v]]!=-1){
                if(mad[head[v]]<in[v]) f(mad[head[v]]+1,in[v]+1);
                chmax(mad[head[v]],in[v]);
                return;
            }
            f(in[head[v]],in[v]+1);
            mad[head[v]]=in[v];
        }
        if(u==v) return;
        if(mad[head[u]]!=-1){
            if(mad[head[u]]<in[v]) f(mad[head[u]]+1,in[v]+1);
            chmax(mad[head[u]],in[v]);
            return;
        }
        f(in[u]+1,in[v]+1);
        mad[head[u]]=in[v];
    }

    /*ex)
        ll ans=0;
        hld.query(u,v,[&](int a,int b){
            ans+=seg.getSum(a,b);
        },true);
    */
    template<class F>
    void query(int u,int v,const F &f,bool isedge){
        for(;;v=par[head[v]]){
            if(in[u]>in[v]) swap(u,v);
            if(head[u]==head[v]) break;
            f(in[head[v]],in[v]+1);
            chmax(mad[head[v]],in[v]);
        }
        if(isedge&&u==v) return;
        f(in[u]+isedge,in[v]+1);
        chmax(mad[head[u]],in[v]);
    }

    void reset(int v){
        while(v!=-1){
            mad[head[v]]=-1;
            v=par[head[v]];
        }
    }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  vector<int> U(N-1),V(N-1),W(N-1);
  rep(i,N-1) cin>>U[i]>>V[i]>>W[i];

  vector<vector<Edge>> g(N);
  rep(i,N-1){
      g[U[i]].push_back({V[i],W[i]});
      g[V[i]].push_back({U[i],W[i]});
  }
  HeavyLightDecomposition hld(N,g,0);
  hld.init();

  vector<ll> v(N,0);
  rep(i,N-1){
      int a=U[i],b=V[i];
      if(hld.dep[a]>hld.dep[b]) swap(a,b);
      v[hld.in[b]]=W[i];
  }
  SegmentTree<ll> seg(N,[](ll a,ll b){return a+b;},0);
  seg.initialize(v);

  int Q;
  cin>>Q;
  rep(q,Q){
      int K;cin>>K;
      vector<int> C(K);
      rep(i,K) cin>>C[i];

      int lc=C[0];
      ll ans=0;
      for(int i=1;i<K;i++){
          int nelc=hld.lca(lc,C[i]);
          if(lc==nelc){
              hld.update(lc,C[i],[&](int a,int b){
                  ans+=seg.query(a,b);
              });
          }else{
              hld.query(lc,C[i],[&](int a,int b){
                  ans+=seg.query(a,b);
              },true);
          }
          lc=nelc;
      }
      cout<<ans<<endl;

      rep(i,K) hld.reset(C[i]);
  }

  return 0;
}
0