結果

問題 No.1524 Upward Mobility
ユーザー 👑 ygussanyygussany
提出日時 2021-05-28 22:39:01
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 1,467 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 31,232 KB
実行使用メモリ 22,912 KB
最終ジャッジ日時 2024-04-25 00:36:46
合計ジャッジ時間 8,576 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
22,912 KB
testcase_01 AC 55 ms
12,416 KB
testcase_02 AC 65 ms
12,416 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next, *prev;
	int v;
	long long sum;
} list;

int main()
{
	int i, N, a[100001], b[100001], par[100001];
	scanf("%d", &N);
	for (i = 2, par[1] = 1; i <= N; i++) scanf("%d", &(par[i]));
	for (i = 1; i <= N; i++) scanf("%d", &(a[i]));
	for (i = 1; i <= N; i++) scanf("%d", &(b[i]));
	
	int u, w;
	list head[100001], tail[100001], d[100001], *p, *pp;
	for (u = 1; u <= N; u++) {
		head[u].prev = NULL;
		head[u].next = &(tail[u]);
		head[u].v = 0;
		head[u].sum = 1LL << 60;
		tail[u].prev = &(head[u]);
		tail[u].next = NULL;
		tail[u].v = N + 1;
		tail[u].sum = 0;
	}
	for (w = N; w >= 1; w--) {
		u = par[w];
		for (pp = &(head[w]); pp->next->v < a[w]; pp = pp->next);
		d[w].v = a[w];
		d[w].sum = pp->next->sum + b[w];
		d[w].prev = pp;
		d[w].next = pp->next;
		pp->next->prev = &(d[w]);
		pp->next = &(d[w]);
		while (pp->sum <= pp->next->sum) {
			pp = pp->prev;
			pp->next->next->prev = pp;
			pp->next = pp->next->next;
		}
		if (w == 1) break;
		
		for (p = head[u].next, pp = head[w].next; pp->next != NULL; ) {
			if (p->v > pp->v) {
				pp = pp->next;
				pp->prev->sum += p->sum;
				if (pp->prev->sum > p->sum) {
					pp->prev->prev = p->prev;
					pp->prev->next = p;
					p->prev->next = pp->prev;
					p->prev = pp->prev;
				}
			} else {
				while (p->v < pp->v) {
					p->sum += pp->sum;
					p = p->next;
				}
			}
		}
	}
	printf("%lld\n", head[1].next->sum);
	fflush(stdout);
	return 0;
}
0