結果

問題 No.1216 灯籠流し/Lanterns
ユーザー beetbeet
提出日時 2020-09-01 20:40:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,357 ms / 4,500 ms
コード長 13,821 bytes
コンパイル時間 4,019 ms
コンパイル使用メモリ 267,352 KB
実行使用メモリ 37,164 KB
最終ジャッジ日時 2024-04-30 18:52:05
合計ジャッジ時間 34,045 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 49 ms
16,512 KB
testcase_05 AC 1,169 ms
30,164 KB
testcase_06 AC 89 ms
6,940 KB
testcase_07 AC 1,063 ms
22,868 KB
testcase_08 AC 821 ms
14,132 KB
testcase_09 AC 434 ms
14,164 KB
testcase_10 AC 454 ms
11,904 KB
testcase_11 AC 215 ms
15,024 KB
testcase_12 AC 44 ms
13,312 KB
testcase_13 AC 261 ms
21,120 KB
testcase_14 AC 99 ms
14,336 KB
testcase_15 AC 175 ms
16,192 KB
testcase_16 AC 169 ms
22,400 KB
testcase_17 AC 565 ms
10,692 KB
testcase_18 AC 199 ms
15,916 KB
testcase_19 AC 961 ms
29,280 KB
testcase_20 AC 441 ms
23,316 KB
testcase_21 AC 238 ms
19,168 KB
testcase_22 AC 692 ms
17,084 KB
testcase_23 AC 75 ms
19,964 KB
testcase_24 AC 96 ms
6,944 KB
testcase_25 AC 48 ms
6,944 KB
testcase_26 AC 540 ms
10,760 KB
testcase_27 AC 191 ms
15,104 KB
testcase_28 AC 118 ms
17,664 KB
testcase_29 AC 292 ms
21,888 KB
testcase_30 AC 465 ms
19,280 KB
testcase_31 AC 808 ms
14,672 KB
testcase_32 AC 437 ms
23,312 KB
testcase_33 AC 103 ms
13,952 KB
testcase_34 AC 1,224 ms
30,996 KB
testcase_35 AC 871 ms
36,032 KB
testcase_36 AC 349 ms
26,176 KB
testcase_37 AC 1,357 ms
31,120 KB
testcase_38 AC 1,089 ms
29,968 KB
testcase_39 AC 823 ms
28,952 KB
testcase_40 AC 767 ms
31,784 KB
testcase_41 AC 759 ms
31,788 KB
testcase_42 AC 756 ms
31,796 KB
testcase_43 AC 754 ms
31,924 KB
testcase_44 AC 764 ms
31,660 KB
testcase_45 AC 1,307 ms
37,032 KB
testcase_46 AC 1,275 ms
37,164 KB
testcase_47 AC 1,298 ms
37,164 KB
testcase_48 AC 1,293 ms
37,036 KB
testcase_49 AC 1,282 ms
37,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/4852"

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

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
/*
 * @docs docs/binaryindexedtree.md
 */
//BEGIN CUT HERE
template<typename T>
struct BIT{
  int n;
  vector<T> bit;
  // 1-indexed
  BIT(int n_):n(n_+1),bit(n+1,0){}

  T sum(int i){
    T s(0);
    for(int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }

  void add(int i,T a){
    if(i==0) return;
    for(int x=i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }

  // [l, r)
  T query(int l,int r){
    return sum(r-1)-sum(l-1);
  }

  int lower_bound(int w){
    if(w<=0) return 0;
    int x=0,r=1;
    while(r<n) r<<=1;
    for(int k=r;k>0;k>>=1){
      if(x+k<=n&&bit[x+k]<w){
        w-=bit[x+k];
        x+=k;
      }
    }
    return x+1;
  }

  // 0-indexed
  T sum0(int i){return sum(i+1);}
  void add0(int i,T a){add(i+1,a);}
  T query0(int l,int r){return sum(r)-sum(l);}
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif


#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif

//BEGIN CUT HERE
template<typename TV, const int N> void read_tuple_impl(TV&) {}
template<typename TV, const int N, typename Head, typename... Tail>
void read_tuple_impl(TV& ts) {
  get<N>(ts).emplace_back(*(istream_iterator<Head>(cin)));
  read_tuple_impl<TV, N+1, Tail...>(ts);
}
template<typename... Ts> decltype(auto) read_tuple(size_t n) {
  tuple<vector<Ts>...> ts;
  for(size_t i=0;i<n;i++) read_tuple_impl<decltype(ts), 0, Ts...>(ts);
  return ts;
}
//END CUT HERE

#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct Sack{
  using F = function<void(int)>;
  int n;
  vector<int> sz,hvy,big;
  vector<vector<int> > G,Q;
  F expand,shrink,query,reset;

  Sack(int n,F expand,F shrink,F query,F reset):
    sz(n,1),hvy(n,-1),big(n,0),G(n),Q(n),
    expand(expand),shrink(shrink),
    query(query),reset(reset){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void add_query(int v,int k){
    Q[v].emplace_back(k);
    sz[v]++;
  }
  void add(int v,int p,int x){
    if(x==1) expand(v);
    else shrink(v);
    for(int u:G[v])
      if(u!=p&&!big[u]) add(u,v,x);
  }

  void dfs(int v=0,int p=-1,bool k=0){
    for(int u:G[v])
      if(u!=p&&u!=hvy[v]) dfs(u,v,0);
    if(~hvy[v]){
      dfs(hvy[v],v,1);
      big[hvy[v]]=1;
    }
    add(v,p,1);
    for(int k:Q[v]) query(k);
    if(~hvy[v]) big[hvy[v]]=0;
    if(!k) add(v,p,0),reset(v);
  }

  void build(int v=0,int p=-1){
    for(int u:G[v]){
      if(u==p) continue;
      build(u,v);
      if(hvy[v]<0||sz[hvy[v]]<sz[u]) hvy[v]=u;
      sz[v]+=sz[u];
    }
    if(p==-1) dfs(v,p);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed ECR002_E(){
  using ll = long long;
  int n;
  scanf("%d",&n);
  vector<int> c(n);
  for(int i=0;i<n;i++) scanf("%d",&c[i]);
  vector<int> cnt(n+1,0);
  vector<ll> ans(n,0);
  int mx=0;
  ll res=0;
  auto expand=
    [&](int v){
      cnt[c[v]]++;
      if(mx<cnt[c[v]]){
        mx=cnt[c[v]];
        res=0;
      }
      if(mx==cnt[c[v]]) res+=c[v];
    };
  auto shrink=[&](int v){cnt[c[v]]--;};
  auto query =[&](int v){ans[v]=res;};
  auto reset =[&](int v){(void)v;mx=res=0;};
  Sack sc(n,expand,shrink,query,reset);
  for(int i=1;i<n;i++){
    int x,y;
    scanf("%d %d",&x,&y);
    x--;y--;
    sc.add_edge(x,y);
  }
  for(int i=0;i<n;i++) sc.add_query(i,i);
  sc.build();
  // sc.dfs();
  for(int i=0;i<n;i++){
    if(i) printf(" ");
    printf("%lld",ans[i]);
  }
  puts("");
  return 0;
}
/*
  verified on 2019/11/23
  http://codeforces.com/contest/600/problem/E
*/

signed CFR316_D(){
  int n,q;
  scanf("%d %d",&n,&q);

  vector<int> c(n),dep(n);
  vector<int> vs(q),hs(q),ans(q);
  vector<int> cnt(n+1,0),odd(n+1,0);

  auto flip=
    [&](int v){
      odd[dep[v]]-=(cnt[dep[v]]>>c[v])&1;
      cnt[dep[v]]^=(1<<c[v]);
      odd[dep[v]]+=(cnt[dep[v]]>>c[v])&1;
    };

  auto query =[&](int k){ans[k]=odd[hs[k]]<=1;};
  auto reset =[&](int v){(void)v;};

  Sack sc(n,flip,flip,query,reset);
  for(int i=1;i<n;i++){
    int p;
    scanf("%d",&p);
    p--;
    sc.add_edge(p,i);
  }

  {
    auto &G=sc.G;
    function<void(int,int,int)> dfs=
      [&](int v,int p,int d){
        dep[v]=d;
        for(int u:G[v]){
          if(u==p) continue;
          dfs(u,v,d+1);
        }
      };
    dfs(0,-1,1);
  }

  char* buf = new char[500500];
  scanf("%s",buf);
  for(int i=0;i<n;i++) c[i]=buf[i]-'a';

  for(int i=0;i<q;i++){
    scanf("%d %d",&vs[i],&hs[i]);
    vs[i]--;
    sc.add_query(vs[i],i);
  }

  sc.build();
  // sc.dfs();
  for(int i=0;i<q;i++)
    puts(ans[i]?"Yes":"No");
  return 0;
}
/*
  verified on 2019/11/23
  http://codeforces.com/contest/570/problem/D
*/

signed CFR130_E(){
  int n;
  scanf("%d",&n);

  vector<int> dep(n),rs;
  vector<int> vs,hs,ans;
  vector<int> cnt(n+1,0);

  auto expand=[&](int v){cnt[dep[v]]++;};
  auto shrink=[&](int v){cnt[dep[v]]--;};
  auto query =[&](int k){ans[k]=cnt[dep[vs[k]]+hs[k]]-1;};
  auto reset =[&](int v){(void)v;};

  Sack sc(n,expand,shrink,query,reset);
  for(int i=0;i<n;i++){
    int p;
    scanf("%d",&p);
    p--;
    if(~p) sc.add_edge(p,i);
    else rs.emplace_back(i);
  }
  sc.build();

  vector<vector<int> > ps(20,vector<int>(n,-1));
  {
    auto &G=sc.G;
    function<void(int,int,int)> dfs=
      [&](int v,int p,int d){
        ps[0][v]=p;
        dep[v]=d;
        for(int u:G[v]){
          if(u==p) continue;
          dfs(u,v,d+1);
        }
      };
    for(int r:rs) dfs(r,-1,1);
  }

  int q;
  scanf("%d",&q);
  vs.assign(q,0);
  hs.assign(q,0);
  ans.assign(q,0);

  for(int k=0;k+1<20;k++)
    for(int i=0;i<n;i++)
      ps[k+1][i]=ps[k][i]<0?-1:ps[k][ps[k][i]];

  auto calc=[&](int v,int k){
              for(int i=0;~v&&i<20;i++)
                if((k>>i)&1) v=ps[i][v];
              return v;
            };

  for(int i=0;i<q;i++){
    scanf("%d %d",&vs[i],&hs[i]);
    vs[i]--;
    vs[i]=calc(vs[i],hs[i]);
    if(~vs[i]) sc.add_query(vs[i],i);
  }

  for(int r:rs) sc.dfs(r,-1,0);
  for(int i=0;i<q;i++){
    if(i) printf(" ");
    printf("%d",ans[i]);
  }
  puts("");
  return 0;
}
/*
  verified on 2019/11/23
  https://codeforces.com/contest/208/problem/E
*/

signed main(){
  //ECR002_E();
  //CFR316_D();
  //CFR130_E();
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
class EulerTourForVertex{
private:
  vector<int> ls,rs;
  int pos;

  void dfs(int v,int p){
    ls[v]=pos++;
    for(int u:G[v])
      if(u!=p) dfs(u,v);
    rs[v]=pos;
  }

public:
  vector< vector<int> > G;

  EulerTourForVertex(){}
  EulerTourForVertex(int n):ls(n),rs(n),G(n){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void build(int r=0){
    pos=0;
    dfs(r,-1);
  }

  int idx(int v){return ls[v];}

  template<typename F>
  void exec(int v,F f){
    f(ls[v],rs[v]);
  }
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct LowestCommonAncestor{
  int n,h;
  vector< vector<int> > G,par;
  vector<int> dep;
  LowestCommonAncestor(){}
  LowestCommonAncestor(int n):n(n),G(n),dep(n){
    h=1;
    while((1<<h)<=n) h++;
    par.assign(h,vector<int>(n,-1));
  }

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void dfs(int v,int p,int d){
    par[0][v]=p;
    dep[v]=d;
    for(int u:G[v])
      if(u!=p) dfs(u,v,d+1);
  }

  void build(int r=0){
    dfs(r,-1,0);
    for(int k=0;k+1<h;k++)
      for(int v=0;v<n;v++)
        if(~par[k][v])
          par[k+1][v]=par[k][par[k][v]];
  }

  int lca(int u,int v){
    if(dep[u]>dep[v]) swap(u,v);
    for(int k=0;k<h;k++)
      if((dep[v]-dep[u])>>k&1)
        v=par[k][v];

    if(u==v) return u;

    for(int k=h-1;k>=0;k--)
      if(par[k][u]!=par[k][v])
        u=par[k][u],v=par[k][v];

    return par[0][u];
  }

  int distance(int u,int v){
    return dep[u]+dep[v]-dep[lca(u,v)]*2;
  }
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#include "eulertourforvertex.cpp"
#include "lowestcommonancestor.cpp"
#undef call_from_test

#endif
/**
 * @see https://smijake3.hatenablog.com/entry/2019/09/15/200200
 */
//BEGIN CUT HERE
struct AuxiliaryTree : EulerTourForVertex{
  using super = EulerTourForVertex;
  LowestCommonAncestor lca;

  vector<vector<int>> T;
  AuxiliaryTree(){}
  AuxiliaryTree(int n):super(n),lca(n),T(n){}

  void build(int r=0){
    super::build(r);
    lca.G=super::G;
    lca.build(r);
  }

  void add_aux_edge(int u,int v){
    T[u].emplace_back(v);
    T[v].emplace_back(u);
  }

  using super::idx;
  void query(vector<int> &vs){
    assert(!vs.empty());
    sort(vs.begin(),vs.end(),
         [&](int a,int b){return idx(a)<idx(b);});
    vs.erase(unique(vs.begin(),vs.end()),vs.end());

    int k=vs.size();
    stack<int> st;
    st.emplace(vs[0]);
    for(int i=0;i+1<k;i++){
      int w=lca.lca(vs[i],vs[i+1]);
      if(w!=vs[i]){
        int l=st.top();st.pop();
        while(!st.empty()&&lca.dep[w]<lca.dep[st.top()]){
          add_aux_edge(st.top(),l);
          l=st.top();st.pop();
        }
        if(st.empty()||st.top()!=w){
          st.emplace(w);
          vs.emplace_back(w);
        }
        add_aux_edge(w,l);
      }
      st.emplace(vs[i+1]);
    }

    while(st.size()>1){
      int c=st.top();st.pop();
      add_aux_edge(st.top(),c);
    }
  }

  void clear(const vector<int> &ws){
    for(int w:ws) T[w].clear();
  }
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif


#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif

//BEGIN CUT HERE
template<typename V>
V compress(V vs){
  sort(vs.begin(),vs.end());
  vs.erase(unique(vs.begin(),vs.end()),vs.end());
  return vs;
}
template<typename T>
map<T, int> dict(const vector<T> &vs){
  map<T, int> res;
  for(int i=0;i<(int)vs.size();i++)
    res[vs[i]]=i;
  return res;
}
map<char, int> dict(const string &s){
  return dict(vector<char>(s.begin(),s.end()));
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  const char newl = '\n';
  using ll = long long;
  using P = pair<int, ll>;

  int n,q;
  cin>>n>>q;
  vector<vector<P>> G(n+1);
  AuxiliaryTree H(n+1);
  for(int i=1;i<n;i++){
    int a,b;
    ll c;
    cin>>a>>b>>c;
    G[a].emplace_back(b,c);
    G[b].emplace_back(a,c);
    H.add_edge(a,b);
  }
  // add 0 for root
  H.add_edge(0,1);
  H.build(0);

  const ll INF = 1e15;
  G[0].emplace_back(1,INF);

  vector<ll> dep(n+1);
  MFP([&](auto dfs,int v,int p,ll d)->void{
    dep[v]=d;
    for(auto [u,c]:G[v])
      if(u!=p) dfs(u,v,d+c);
  })(0,-1,0);

  auto [type,vs,ts,ls]=read_tuple<int, int, ll, ll>(q);

  // vanish vertices
  vector<int> rs(q);
  for(int i=0;i<q;i++){
    if(type[i]!=0) continue;
    int r=vs[i];
    ll d=dep[vs[i]]-ls[i];
    for(int k=H.lca.h-1;k>=0;k--){
      int p=H.lca.par[k][r];
      if(~p and d<=dep[p]) r=p;
    }
    rs[i]=H.lca.par[0][r];
  }

  vector<ll> pos;
  for(int i=0;i<q;i++)
    pos.emplace_back(ts[i]+dep[vs[i]]);
  pos=compress(pos);

  BIT<int> bit(pos.size());
  vector<int> cs(q);
  for(int i=0;i<q;i++)
    cs[i]=lower_bound(pos.begin(),pos.end(),ts[i]+dep[vs[i]])-pos.begin();

  // inverse of ss
  vector<int> rev(n+1);

  queue<P> que;
  que.emplace(0,q);

  vector<int> ans(q);
  while(!que.empty()){
    auto [L,R]=que.front();que.pop();
    if(L+1==R) continue;
    int M=(L+R)>>1;

    vector<int> ss;
    for(int i=L;i<M;i++){
      if(type[i]==0){
        ss.emplace_back(vs[i]);
        ss.emplace_back(rs[i]);
      }
    }

    for(int i=M;i<R;i++)
      if(type[i]==1) ss.emplace_back(vs[i]);

    if(ss.empty()){
      que.emplace(L,M);
      que.emplace(M,R);
      continue;
    }

    ss.emplace_back(0);

    H.query(ss);

    int m=ss.size();
    for(int i=0;i<m;i++) rev[ss[i]]=i;

    vector<vector<int>> add(m),sub(m);
    for(int i=L;i<M;i++){
      if(type[i]==0){
        add[rev[vs[i]]].emplace_back(i);
        sub[rev[rs[i]]].emplace_back(i);
      }
    }

    auto expand=[&](int v){
      for(int i:add[v]) bit.add0(cs[i],+1);
      for(int i:sub[v]) bit.add0(cs[i],-1);
    };
    auto shrink=[&](int v){
      for(int i:add[v]) bit.add0(cs[i],-1);
      for(int i:sub[v]) bit.add0(cs[i],+1);
    };
    auto query=[&](int i){
      ans[i]+=bit.sum0(cs[i]);
    };
    auto reset=[&](int){};

    Sack S(m,expand,shrink,query,reset);

    for(int v:ss)
      for(int u:H.T[v])
        if(v<u) S.add_edge(rev[u],rev[v]);

    for(int i=L;i<M;i++){
      if(type[i]==0){
        S.add_query(rev[vs[i]],i);
        S.add_query(rev[rs[i]],i);
      }
    }

    for(int i=M;i<R;i++)
      if(type[i]==1) S.add_query(rev[vs[i]],i);

    S.build(rev[0]);

    H.clear(ss);

    que.emplace(L,M);
    que.emplace(M,R);
  }

  for(int i=0;i<q;i++)
    if(type[i]==1) cout<<ans[i]<<newl;

  return 0;
}
0