# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
105730 | 2019-04-14T07:46:46 Z | Pro_ktmr | Fireworks (APIO16_fireworks) | C++14 | 18 ms | 14480 KB |
#include"bits/stdc++.h" using namespace std; #define LL long long #define REP(i, n) for(int (i)=0; (i)<(n); (i)++) #define PB push_back #define MP make_pair #define MOD 1000000007 int N, M; vector<pair<int, LL>> edge[300001]; int par[300001]; vector<pair<int, LL>> chi[300001]; vector<pair<int, int>> node; LL a[300001] ={}, b[300001] ={}; void dfs(int now, int p){ for(int i=0; i<edge[now].size(); i++){ if(edge[now][i].first == p) continue; chi[now].PB(MP(edge[now][i].first, edge[now][i].second)); par[edge[now][i].first] = now; dfs(edge[now][i].first, now); } } int d[300001] = {}; int depth(int now){ if(now == 0) return 0; if(d[now] != 0) return d[now]; return d[now] = depth(par[now]) + 1; } LL my_binary_search(int now, LL ans, LL ok, LL ng){ while(abs(ok-ng) > 1){ LL m = (ok+ng) / 2; LL tmp = 0; for(int i=0; i<chi[now].size(); i++){ if(a[chi[now][i].first] > m) tmp += a[chi[now][i].first] - m; else if(b[chi[now][i].first] < m) tmp += m - b[chi[now][i].first]; } if(tmp == ans) ok = m; else ng = m; } return ok; } int main(){ scanf("%d%d", &N, &M); for(int i=0; i<N+M-1; i++){ int P; LL C; scanf("%d%lld", &P, &C); P--; edge[i+1].PB(MP(P, C)); edge[P].PB(MP(i+1, C)); } par[0] = -1; dfs(0, -1); for(int i=0; i<N+M; i++){ node.PB(MP(depth(i), i)); } sort(node.begin(), node.end()); reverse(node.begin(), node.end()); LL ans = 0; for(int i=0; i<N+M; i++){ int now = node[i].second; if(chi[now].size() == 0){ a[now] = 0; b[now] = 0; continue; } for(int j=0; j<chi[now].size(); j++){ a[chi[now][j].first] += chi[now][j].second; b[chi[now][j].first] += chi[now][j].second; } vector<LL> v; for(int j=0; j<chi[now].size(); j++){ v.PB(b[chi[now][j].first]); } sort(v.begin(), v.end()); LL tmp = v[v.size()/2]; LL nowAns = 0; for(int j=0; j<chi[now].size(); j++){ if(tmp < a[chi[now][j].first]) nowAns += a[chi[now][j].first] - tmp; else if(tmp > b[chi[now][j].first]) nowAns += tmp - b[chi[now][j].first]; } ans += nowAns; a[now] = my_binary_search(now, nowAns, tmp, -1); b[now] = my_binary_search(now, nowAns, tmp, v.back()+1); } cout << ans << endl; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 14464 KB | Output is correct |
2 | Correct | 15 ms | 14464 KB | Output is correct |
3 | Correct | 16 ms | 14464 KB | Output is correct |
4 | Correct | 16 ms | 14464 KB | Output is correct |
5 | Correct | 16 ms | 14480 KB | Output is correct |
6 | Correct | 18 ms | 14464 KB | Output is correct |
7 | Correct | 16 ms | 14464 KB | Output is correct |
8 | Correct | 16 ms | 14464 KB | Output is correct |
9 | Correct | 18 ms | 14464 KB | Output is correct |
10 | Correct | 16 ms | 14464 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 15 ms | 14464 KB | Output is correct |
2 | Incorrect | 16 ms | 14464 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 14464 KB | Output is correct |
2 | Correct | 15 ms | 14464 KB | Output is correct |
3 | Correct | 16 ms | 14464 KB | Output is correct |
4 | Correct | 16 ms | 14464 KB | Output is correct |
5 | Correct | 16 ms | 14480 KB | Output is correct |
6 | Correct | 18 ms | 14464 KB | Output is correct |
7 | Correct | 16 ms | 14464 KB | Output is correct |
8 | Correct | 16 ms | 14464 KB | Output is correct |
9 | Correct | 18 ms | 14464 KB | Output is correct |
10 | Correct | 16 ms | 14464 KB | Output is correct |
11 | Correct | 15 ms | 14464 KB | Output is correct |
12 | Incorrect | 16 ms | 14464 KB | Output isn't correct |
13 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 14464 KB | Output is correct |
2 | Correct | 15 ms | 14464 KB | Output is correct |
3 | Correct | 16 ms | 14464 KB | Output is correct |
4 | Correct | 16 ms | 14464 KB | Output is correct |
5 | Correct | 16 ms | 14480 KB | Output is correct |
6 | Correct | 18 ms | 14464 KB | Output is correct |
7 | Correct | 16 ms | 14464 KB | Output is correct |
8 | Correct | 16 ms | 14464 KB | Output is correct |
9 | Correct | 18 ms | 14464 KB | Output is correct |
10 | Correct | 16 ms | 14464 KB | Output is correct |
11 | Correct | 15 ms | 14464 KB | Output is correct |
12 | Incorrect | 16 ms | 14464 KB | Output isn't correct |
13 | Halted | 0 ms | 0 KB | - |