結果
問題 | No.1524 Upward Mobility |
ユーザー | 沙耶花 |
提出日時 | 2021-05-13 23:21:47 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,098 bytes |
コンパイル時間 | 7,822 ms |
コンパイル使用メモリ | 294,516 KB |
実行使用メモリ | 499,992 KB |
最終ジャッジ日時 | 2024-10-08 20:56:08 |
合計ジャッジ時間 | 34,131 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,714 ms
279,384 KB |
testcase_01 | AC | 1,722 ms
281,116 KB |
testcase_02 | AC | 2,273 ms
449,388 KB |
testcase_03 | AC | 649 ms
198,504 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 150 ms
58,256 KB |
testcase_12 | AC | 153 ms
58,120 KB |
testcase_13 | AC | 168 ms
58,184 KB |
testcase_14 | AC | 549 ms
196,988 KB |
testcase_15 | AC | 537 ms
196,560 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | WA | - |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 2 ms
5,248 KB |
testcase_29 | AC | 821 ms
337,188 KB |
testcase_30 | AC | 832 ms
337,316 KB |
testcase_31 | AC | 936 ms
337,176 KB |
testcase_32 | AC | 2,041 ms
499,992 KB |
ソースコード
#include <stdio.h> #include <bits/stdc++.h> #include <atcoder/all> using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000 template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct dynamic_lazy_segtree { struct node{ int nxt[2]; S v; F lz; node(){ nxt[0] = -1; nxt[1] = -1; v = e(); lz = id(); } }; vector<node> nodes; dynamic_lazy_segtree() : dynamic_lazy_segtree(0) {} dynamic_lazy_segtree(long long n){ log = 0; while((1LL<<log) < n){ log++; } _n = n; nodes.resize(1,node()); } int get(int cur,int ind){ if(nodes[cur].nxt[ind]==-1){ nodes[cur].nxt[ind] = nodes.size(); nodes.push_back(node()); } return nodes[cur].nxt[ind]; } void set(long long p, S x) { assert(0 <= p && p < _n); vector<int> t(1,0); for(int i=log-1;i>=0;i--){ t.push_back(get(t.back(),(p>>i)&1)); } rep(i,t.size()-1){ push(t[i]); } reverse(t.begin(),t.end()); rep(i,t.size()){ if(i==0)nodes[t[i]].v = x; else{ update(t[i]); } } } S get(long long p) { assert(0 <= p && p < _n); int cur = 0; for(int i=log-1;i>=0;i--){ push(cur); cur = get(cur,(p>>i)&1); } return nodes[cur].v; } S prod(long long l,long long r,int cur,int depth){ if(cur==-1||r-l==0)return e(); if(r-l==(1LL<<depth))return nodes[cur].v; push(cur); r--; if((l>>(depth-1))&1){ l ^= 1LL<<(depth-1); r ^= 1LL<<(depth-1); return prod(l,r+1,nodes[cur].nxt[1],depth-1); } if(((r>>(depth-1))&1)==0){ return prod(l,r+1,nodes[cur].nxt[0],depth-1); } r ^= 1LL<<(depth-1); return op(prod(l,1LL<<(depth-1),nodes[cur].nxt[0],depth-1),prod(0,r+1,nodes[cur].nxt[1],depth-1)); } S prod(long long l, long long r) { assert(0 <= l && l <= r && r <= _n); return prod(l,r,0,log); } S all_prod() { return nodes[0].v; } void apply(long long p,F f){ assert(0 <= p && p < _n); vector<int> t(1,0); for(int i=log-1;i>=0;i--){ t.push_back(get(t.back(),(p>>i)&1)); } rep(i,t.size()-1){ push(t[i]); } reverse(t.begin(),t.end()); rep(i,t.size()){ if(i==0)nodes[t[i]].v = mapping(f,nodes[t[i]].v); else{ update(t[i]); } } } void apply(long long l,long long r,int cur,int depth,F f){ if(r-l==0)return; if(r-l==(1LL<<depth)){ all_apply(cur,f); return; } push(cur); r--; if((l>>(depth-1))&1){ l ^= 1LL<<(depth-1); r ^= 1LL<<(depth-1); apply(l,r+1,get(cur,1),depth-1,f); } else if(((r>>(depth-1))&1)==0){ apply(l,r+1,get(cur,0),depth-1,f); } else{ r ^= 1LL<<(depth-1); apply(l,1LL<<(depth-1),get(cur,0),depth-1,f); apply(0,r+1,get(cur,1),depth-1,f); } update(cur); return; } void apply(long long l,long long r,F f){ assert(0 <= l && l <= r && r <= _n); apply(l,r,0,log,f); } void update(int k){ nodes[k].v = e(); rep(i,2){ if(nodes[k].nxt[i]==-1)continue; nodes[k].v = op(nodes[k].v,nodes[nodes[k].nxt[i]].v); } } void all_apply(int k, F f) { nodes[k].v = mapping(f, nodes[k].v); nodes[k].lz = composition(f, nodes[k].lz); } void push(int k) { rep(i,2){ int t = get(k,i); all_apply(t,nodes[k].lz); } nodes[k].lz = id(); } long long _n,log; }; int N; vector<vector<int>> E; vector<int> a; vector<long long> b; vector<int> sz; long long op(long long a,long long b){ return max(a,b); } long long e(){ return 0; } long long mapping(long long a,long long b){ return a+b; } long long composition(long long a,long long b){ return a+b; } long long id(){ return 0; } using Segtree = dynamic_lazy_segtree<long long,op,e,long long,mapping,composition,id>; vector<set<int>> ps; vector<Segtree> seg; void dfs0(int cur){ sz[cur] = 1; rep(i,E[cur].size()){ int to = E[cur][i]; dfs0(to); sz[cur] += sz[to]; } sort(E[cur].rbegin(),E[cur].rend(),[&](int a,int b){ return sz[a]<sz[b]; }); } void dfs2(int cur){ if(E[cur].size()==0){ seg[cur].set(a[cur],b[cur]); seg[cur].set(N,0); ps[cur].insert(a[cur]); ps[cur].insert(N); return; } if(E[cur].size()==1){ int to = E[cur][0]; dfs2(to); swap(ps[cur],ps[to]); swap(seg[cur],seg[to]); seg[cur].set(a[cur],seg[cur].prod(a[cur]+1,N)+b[cur]); return; } rep(i,E[cur].size()){ int to = E[cur][i]; dfs2(to); } vector<int> pp; rep(i,E[cur].size()){ if(i==0)continue; int to = E[cur][i]; for(auto j:ps[to]){ pp.push_back(j); } } pp.push_back(a[cur]); sort(pp.begin(),pp.end()); pp.erase(unique(pp.begin(),pp.end()),pp.end()); vector<long long> imos(pp.size(),0LL); rep(i,E[cur].size()){ if(i==0)continue; int to = E[cur][i]; int d0 = -1; int d1; for(auto j:ps[to]){ d1 = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),j)); long long v = seg[to].prod(j,N); imos[d1] += v; if(d0>=0)imos[d0] -= v; d0 = d1; } } for(int i=imos.size()-1;i>=1;i--){ imos[i-1] += imos[i]; } vector<long long> nv; rep(i,pp.size()){ long long D = imos[i]; int to = E[cur][0]; D += seg[to].prod(pp[i],N); if(pp[i]==a[cur])D += b[cur]; nv.push_back(D); } swap(seg[cur],seg[E[cur][0]]); swap(ps[cur],ps[E[cur][0]]); rep(i,E[cur].size()){ if(i==0)continue; int to = E[cur][i]; int d = 0; for(auto j:ps[to]){ long long v = seg[to].prod(j,N); seg[cur].apply(d,j,v); ps[cur].insert(j); d = j; } } rep(i,pp.size()){ seg[cur].set(pp[i],nv[i]); } ps[cur].insert(a[cur]); } int main(){ cin>>N; E.resize(N); rep(i,N-1){ int p; scanf("%d",&p); p--; E[p].push_back(i+1); } a.resize(N); b.resize(N); rep(i,N){ scanf("%d",&a[i]); a[i]--; } rep(i,N)scanf("%lld",&b[i]); sz.resize(N); dfs0(0); seg.resize(N,dynamic_lazy_segtree<long long,op,e,long long,mapping,composition,id>(N+1)); ps.resize(N); dfs2(0); cout<<seg[0].all_prod()<<endl; return 0; }