結果

問題 No.1215 都市消滅ビーム
ユーザー 👑 tute7627tute7627
提出日時 2020-08-30 16:11:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 6,718 bytes
コンパイル時間 3,151 ms
コンパイル使用メモリ 236,608 KB
実行使用メモリ 48,944 KB
最終ジャッジ日時 2024-11-15 10:52:13
合計ジャッジ時間 15,474 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 RE -
testcase_10 AC 2 ms
6,820 KB
testcase_11 WA -
testcase_12 AC 2 ms
6,816 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 RE -
testcase_20 RE -
testcase_21 WA -
testcase_22 RE -
testcase_23 WA -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 WA -
testcase_28 RE -
testcase_29 WA -
testcase_30 RE -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 AC 2 ms
6,820 KB
testcase_41 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG

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

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
struct LCA{
  ll datasize = 18;
  ll n,root;//rootを根とする頂点数nの根付き木
  vector<ll>depth;//0から
  vector<vector<ll>>G,data;
  LCA(vector<vector<ll>>g,ll r):G(g),n(g.size()),root(r){
    data.assign(n,vector<ll>(datasize,-1));
    depth.assign(n,-1LL);
    build();
  }
  void build(){
    dfs(root,0);
    for(ll i=1;i<datasize;i++){
      for(ll j=0;j<n;j++){
        if(data[j][i-1]==-1)data[j][i]=-1;
        else data[j][i]=data[data[j][i-1]][i-1];
      }
    }
  }
  void dfs(ll k,ll d){
    depth[k]=d;
    for(ll i=0;i<G[k].size();i++){
      if(depth[G[k][i]]==-1){
        data[G[k][i]][0]=k;
        dfs(G[k][i],d+1);
      }
    }
  }
  ll deep(ll x){
    return x>=0?depth[x]:-1;
  }
  ll query(ll x,ll y){
    if(depth[x]<depth[y])swap(x,y);
    ll tmp = datasize - 1;
    while(depth[x]!=depth[y]){
      while(tmp>=1&&depth[y]>=deep(data[x][tmp]))tmp--;
      x = data[x][tmp];
    }
    tmp = datasize - 1;
    while(x!=y){
      while(tmp>=1&&data[x][tmp]==data[y][tmp])tmp--;
      x = data[x][tmp];
      y = data[y][tmp];
    }
    return x;
  }
  ll path(ll x,ll y){
    return deep(x)+deep(y)-2*deep(query(x,y));
  }
  ll par(ll x){
    return data[x][0];
  }
};
template<typename T>
struct BIT{
  ll n;
  ll k=1;
  vector<T>data;
  BIT() = default;
  BIT(ll size):n(size){
    data.assign(n,0);
    while(k*2<=n)k*=2;
  }
  void add(ll a,T w){
    for(ll i=a+1;i<=n;i+=i&-i)data[i-1]+=w;
  }
  T sum(ll a){
	if(a<0)return 0;
  if(a>=n)a=n-1;
    T ret = 0;
    for(ll i=a+1;i>0;i-=i&-i)ret+=data[i-1];
    return ret;
  }
  T sum(ll a,ll b){return a>b?0:sum(b)-sum(a-1);}
  T operator[](ll pos){
    return sum(pos,pos);
  }
  ll lower_bound(ll x){
    ll ret=0;    
    for(ll i=k;i>0;i/=2){
      if(ret+i<=n&&data[ret+i-1]<x){
        x-=data[ret+i-1];
        ret+=i;
      }
    }
    return ret;
  }
  void print(){
    for(ll i=0;i<n;i++){
      if(i!=0)cout<<" ";
      cout<<(*this)[i];
    }
    cout<<endl;
  }
};
template<typename T>
map<T,ll> compress(vector<T> &v){
  sort(ALL(v));
  v.erase(unique(ALL(v)),v.end());
  map<T, ll> ret;
  for(ll i=0;i<(ll)v.size();i++)
    ret[v[i]]=i;
  return ret;
}
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
  ll n,k;cin>>n>>k;
  vector<ll>c(k),d(k);
  rep(i,0,k)cin>>c[i],c[i]--;
  rep(i,0,k)cin>>d[i];
  vector<ll>v(k+1);
  rep(i,0,k)v[i+1]=v[i]+d[i];
  vector<vector<ll>>g(n);
  rep(i,0,n-1){
    ll a,b;cin>>a>>b;a--;b--;
    g[a].PB(b);
    g[b].PB(a);
  }
  LCA lca(g,0);
  vector<ll>cl(k),cr(k),cld(k),crd(k);
  {
    cl[0]=c[0],cr[k-1]=c[k-1];
    rep(i,0,k-1)cl[i+1]=lca.query(cl[i],c[i+1]);
    rrep(i,0,k-1)cr[i]=lca.query(cr[i+1],c[i]);
    rep(i,0,k)cld[i]=lca.depth[cl[i]];
    rep(i,0,k)crd[i]=lca.depth[cr[i]];
  }
  auto val=v;
  auto mp=compress(val);
  ll sz=mp.size();
  struct zz{
    ll d,v,lr;
  };
  vector<zz>ord;
  {
    ll l=0,r=k-1;
    ll now=lca.query(c[l],c[r]);
    ord.PB({lca.depth[now],l,0});
    ord.PB({lca.depth[now],r,1});
    while(l+1<r){
      if(lca.depth[lca.query(now,c[l]+1)]>lca.depth[lca.query(now,c[r]-1)]){
        l++;
        now=lca.query(now,c[l]);
        ord.PB({lca.depth[now],l,0});
      }
      else{
        r--;
        now=lca.query(now,c[r]);
        ord.PB({lca.depth[now],r,1});
      }
    }
  }
  ll sum=0;
  rep(i,0,k)sum+=d[i];
  //ll ok=INF,ng=-INF;
  ll ok=INF,ng=-INF;
  ll lim=(k*(k+1)/2+1+1)/2-1;
  //cout<<lim<<endl;
  //debug(v,k+1);
  //cout<<sum<<endl;
  while(ok-ng>=2){
    ll mid=(ok+ng)/2;
    ll m=mid-sum;
    ll num=0;
    {
      ll s=0;
      rep(i,0,k-1){
        s+=d[i];
        if(s+cld[i]<=mid)num++;
      }
      s=0;
      rrep(i,1,k){
        s+=d[i];
        if(s+crd[i]<=mid)num++;
      }
    }
    //cout<<num<<endl;
    BIT<ll>bl(sz),br(sz);
    for(auto [d,i,lr]:ord){
      //cout<<d spa i spa lr<<endl;
      if(lr==0){
        ll idx=lower_bound(ALL(val),v[i+1]+d-m)-val.begin();
        num+=br.sum(idx,sz-1);      
        bl.add(mp[v[i+1]],1);                 
      }
      else{
        ll idx=upper_bound(ALL(val),v[i]+m-d)-val.begin()-1; 
        //debug(val,sz);
        //cout<<idx spa v[i] spa m spa d spa val[idx]<<endl;
        num+=bl.sum(idx); 
        br.add(mp[v[i]],1);                 
      }
    }
    //cout<<mid spa num spa lim<<endl;
    if(num>=lim)ok=mid;
    else ng=mid;
  }
  cout<<ok<<endl;
  return 0;
}
0