답안 #470369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
470369 2021-09-03T15:37:09 Z fefe Fireworks (APIO16_fireworks) C++17
0 / 100
5 ms 7244 KB
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pil;

const int MAXN = 300005;
int n, m;
int par[MAXN];
LL val[MAXN];
pil mid[MAXN];
vector<LL> lst[MAXN];
void udt(int i) {
	lst[par[i]].pb(mid[i].fi + val[i]);
	lst[par[i]].pb(mid[i].se + val[i]);
}
int main() {
	scanf("%d %d", &n, &m);
	for (int i = 2; i <= n + m; i++) {
		int p;
		LL v;
		scanf("%d %lld", &p, &v);
		par[i] = p;
		val[i] = v;
	}

	for (int i = n + m; i > n; i--) {
		mid[i] = pil(0, 0);
		udt(i);
	}

	for (int i = n; i >= 1; i--) {
		sort(all(lst[i]));
		mid[i].fi = lst[i][lst[i].size() / 2 - 1];
		mid[i].se = lst[i][lst[i].size() / 2];
		udt(i);
	}
	val[1] = lst[1].front();

	LL res = 0;
	for (int i = 2; i <= n + m; i++) {
		LL v = val[par[i]] - val[i];
		if (abs(mid[i].fi - v) < abs(mid[i].se - v)) {
			val[i] = mid[i].fi;
			res += abs(mid[i].fi - v);
		}
		else {
			val[i] = mid[i].se;
			res += abs(mid[i].se - v);
		}
	}
	printf("%lld\n", res);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:27:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |   scanf("%d %lld", &p, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7244 KB Output is correct
2 Incorrect 5 ms 7244 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7244 KB Output is correct
2 Incorrect 5 ms 7244 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7244 KB Output is correct
2 Incorrect 5 ms 7244 KB Output isn't correct
3 Halted 0 ms 0 KB -