結果
問題 | No.1215 都市消滅ビーム |
ユーザー | 👑 tute7627 |
提出日時 | 2020-08-30 17:35:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,921 bytes |
コンパイル時間 | 3,097 ms |
コンパイル使用メモリ | 236,200 KB |
実行使用メモリ | 51,764 KB |
最終ジャッジ日時 | 2024-11-15 14:02:45 |
合計ジャッジ時間 | 35,091 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | WA | - |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | WA | - |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | WA | - |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | AC | 2 ms
5,248 KB |
testcase_41 | AC | 2 ms
5,248 KB |
ソースコード
//#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 = 19; 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 kl,kr,kd; { 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}); } if(l+1==r)ord.back().lr=2; } kl=l,kr=r,kd=lca.depth[now]; } ll sum=0; rep(i,0,k)sum+=d[i]; ll ok=INF,ng=-INF; //ll ok=50,ng=-50; ll lim=(k*(k+1)/2+1+1)/2-1; //cout<<lim<<endl; //debug(v,k+1); //cout<<sum<<endl; auto f=[&](ll mid){ //if(mid==30)cout<<endl; ll m=mid-sum; ll num=0; { ll s=0; rep(i,0,k-1){ s+=d[i]; //if(mid==30)cout<<s spa cld[i] spa mid<<endl; if(s+cld[i]<=mid)num++; } s=0; rrep(i,1,k){ s+=d[i]; //if(mid==30)cout<<s spa crd[i] spa mid<<endl; if(s+crd[i]<=mid)num++; } s+=d[0]; if(s+crd[0]<=mid)num--; } //cout<<num<<endl; BIT<ll>bl(sz),br(sz); for(auto [d,i,lr]:ord){ //if(mid==30)cout<<d spa i spa lr spa num<<endl; if(lr==0||lr==2){ ll idx=lower_bound(ALL(val),v[i+1]+d-m)-val.begin(); num+=br.sum(idx,sz-1); //if(mid==10)cout<<idx spa v[i] spa m spa d spa val[idx]<<endl; if(lr==0)bl.add(mp[v[i+1]],1); } if(lr==1||lr==2){ ll idx=upper_bound(ALL(val),v[i]+m-d)-val.begin()-1; //debug(val,sz); //if(mid==30)bl.print(); //if(mid==30)cout<<idx spa v[i] spa m spa d spa val[idx]<<endl; num+=bl.sum(idx); //if(mid==30)cout<<num<<endl; br.add(mp[v[i]],1); } } { BIT<ll>bit(sz); rep(i,2,kl){ bit.add(mp[v[i-1]],1); ll idx=upper_bound(ALL(val),v[i+1]+m-kd)-val.begin()-1; num+=bit.sum(idx); } bit=BIT<ll>(sz); rep(i,kr+1,k-3){ bit.add(mp[v[i+2]],1); ll idx=lower_bound(ALL(val),v[i]+kd-m)-val.begin(); num+=bit.sum(idx,sz-1); } } return num; }; while(ok-ng>=2){ ll mid=(ok+ng)/2; ll num=f(mid); //cout<<mid spa num spa lim<<endl; if(num>=lim)ok=mid; else ng=mid; } //auto cand={-10,9,10,11,12,19,20,21,22,29,30,31,32,39,40,41,10000000}; //auto cand={3,4,5,10,11,12,110,111,112,1002,1003,1004,1010,1011,1012,1101,1102,1103,1104,1109,1110,1111,1112}; //auto cand={2,3,4,12,13,14,100,101,102,109,110,111,112}; //auto cand={1,2,3,4}; //for(auto z:cand)cout<<z spa f(z)<<endl; cout<<ok<<endl; return 0; }