Submission #39362

#TimeUsernameProblemLanguageResultExecution timeMemory
39362FilyanFireworks (APIO16_fireworks)C++14
100 / 100
439 ms114380 KiB
#include<iostream> #include<algorithm> #include<vector> #include<queue> #include<functional> using namespace std; #define sz(x) (int)(x.size()) #define fi(a,b) for(int i=a;i<b;++i) #define fj(a,b) for(int j=a;j<b;++j) #define mp make_pair #define pb push_back typedef long long ll; typedef pair<int, int> pii; ///////////////////////// int const N = 3e5 + 41; int n, m; vector<pii> e[N]; ll ans; struct Fun{ priority_queue<ll> l; priority_queue<ll, vector<ll>, greater<ll> > r; ll addl, addr; ll ans; Fun(){ addl = addr = ans = 0; }; Fun(int c){ addl = addr = ans = 0; l.push(c); r.push(c); } int getq(){ return sz(l) + sz(r); } ll getleftmostmin(){ ll x = (l.top()); return x + addl; } ll getrightmostmin(){ ll x = (r.top()); return x + addr; } ll extractleftmostmin(){ ll x = (l.top()); l.pop(); return x + addl; } ll extractrightmostmin(){ ll x = (r.top()); r.pop(); return x + addr; } void pushrightpoint(ll x){ if(x >= getleftmostmin()){ r.push(x - addr); }else{ ll lx = extractleftmostmin(); ans += lx - x; l.push(x - addl); r.push(lx - addr); } } void pushright(priority_queue<ll, vector<ll>, greater<ll> > &rp, ll addright){ while(!rp.empty()){ ll x = rp.top(); rp.pop(); pushrightpoint(x + addright); } } void pushleftpoint(ll x){ if(x <= getrightmostmin()){ l.push(x - addl); }else{ ll rx = extractrightmostmin(); ans += x - rx; l.push(rx - addl); r.push(x - addr); } } void pushleft(priority_queue<ll> &lp, ll addleft){ while(!lp.empty()){ ll x = lp.top(); lp.pop(); pushleftpoint(x + addleft); } } void shift(ll s){ addr += s; addl += s; ll lx = extractleftmostmin(); addl -= s; l.push(lx - addl); } void relax(){ ll x = r.top(); while(!r.empty()) r.pop(); r.push(x); } }; Fun *f[N]; void dfs(int x, int p = -1, int c = 0){ if(x >= n){ f[x] = new Fun(c); return; } f[x] = new Fun(); int id = x; for(pii e : ::e[x]){ int y = e.first; if(y == p) continue; int c = e.second; dfs(y, x, c); if(f[x]->getq() < f[y]->getq()){ f[x] = f[y]; id = y; } } for(pii e : ::e[x]){ int y = e.first; int c = e.second; if(y == id) continue; if(y == p) continue; f[x]->ans += f[y]->ans; f[x]->pushleft(f[y]->l, f[y]->addl); f[x]->pushright(f[y]->r, f[y]->addr); } if(p != -1){ f[x]->shift(c); f[x]->relax(); } //cerr << x << " " << f[x]->ans << endl; } void solve(){ fi(0, n) f[i] = new Fun(); dfs(0); ans = f[0]->ans; } int main(){ #ifdef _DEBUG freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif scanf("%d %d",&n,&m); fi(1, n+m){ int p, c; scanf("%d %d",&p,&c); --p; e[p].pb(mp(i, c)); e[i].pb(mp(p, c)); } solve(); printf("%lld\n",ans); return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'void dfs(int, int, int)':
fireworks.cpp:139:7: warning: unused variable 'c' [-Wunused-variable]
   int c = e.second;
       ^
fireworks.cpp: In function 'int main()':
fireworks.cpp:166:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
fireworks.cpp:169:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&p,&c);
                       ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...