답안 #794558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
794558 2023-07-26T15:46:00 Z rainboy Rooted MST (innopolis2021_final_E) C
100 / 100
545 ms 89104 KB
/* https://oj.uz/submission/418483 (gs14004) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	300000
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N))) */
#define M	300000
#define W	1000000001
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }

unsigned int X = 12345;

int rand_() { 
	return (X *= 3) >> 1;
}

int xx[N], n;
int ii[M], jj[M], ww[M], m;

void sort(int *hh, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (ww[hh[j]] == ww[h])
				j++;
			else if (ww[hh[j]] < ww[h]) {
				tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
			}
		sort(hh, l, i);
		l = k;
	}
}

int *ej[N], *ew[N], eo[N];

void append(int i, int j, int w) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0) {
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
		ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
	}
	ej[i][o] = j, ew[i][o] = w;
}

int ds[N];

int find(int i) {
	return ds[i] < 0 ? i : find(ds[i]);
}

void join(int i, int j, int w) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j, append(j, i, w);
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, append(i, j, w);
	}
}

int qu[N], ta[N], ww_[N], cnt;

void dfs(int i) {
	int o;

	qu[ta[i] = cnt++] = i;
	for (o = 0; o < eo[i]; o++) {
		int j = ej[i][o], w = ew[i][o];

		ww_[cnt] = w, dfs(j);
	}
}

long long st[N_ * 2][2][2]; int n_;

void pul(int i) {
	int l = i << 1, r = l | 1, a, b, c;

	memset(st[i], 0x3f, sizeof st[i]);
	for (a = 0; a < 2; a++)
		for (b = 0; b < 2; b++)
			for (c = 0; c < 2; c++)
				st[i][a][c] = min(st[i][a][c], st[l][a][b] + st[r][b][c]);
}

void build() {
	int i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	for (i = 0; i < n_; i++)
		if (i < n) {
			st[n_ + i][0][0] = ww_[i], st[n_ + i][0][1] = ww_[i] + xx[qu[i]];
			st[n_ + i][1][0] = 0, st[n_ + i][1][1] = min(ww_[i], xx[qu[i]]);
		} else {
			st[n_ + i][0][0] = 0, st[n_ + i][0][1] = INF;
			st[n_ + i][1][0] = INF, st[n_ + i][1][1] = 0;
		}
	for (i = n_ - 1; i > 0; i--)
		pul(i);
}

void update(int i, int x) {
	st[n_ + i][0][0] = ww_[i], st[n_ + i][0][1] = ww_[i] + x;
	st[n_ + i][1][0] = 0, st[n_ + i][1][1] = min(ww_[i], x);
	i += n_;
	while (i > 1)
		pul(i >>= 1);
}

int main() {
	static int hh[M];
	int q, h, h_, i, x;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &xx[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
		hh[h] = h;
	}
	sort(hh, 0, m);
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		ew[i] = (int *) malloc(2 * sizeof *ew[i]);
	}
	memset(ds, -1, n * sizeof *ds);
	for (h = 0; h < m; h++) {
		h_ = hh[h];
		join(ii[h_], jj[h_], ww[h_]);
	}
	for (i = 0; i < n; i++)
		join(i, i + 1, W);
	dfs(find(0));
	build();
	scanf("%d", &q);
	while (q--) {
		scanf("%d%d", &i, &x), i--;
		update(ta[i], x);
		printf("%lld\n", st[1][0][1]);
	}
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:47:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   47 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Main.c: In function 'main':
Main.c:129:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:131:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |   scanf("%d", &xx[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:133:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |   scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:150:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
Main.c:152:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |   scanf("%d%d", &i, &x), i--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Correct 2 ms 816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 363 ms 79812 KB Output is correct
2 Correct 311 ms 75192 KB Output is correct
3 Correct 146 ms 11188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 13644 KB Output is correct
2 Correct 163 ms 10336 KB Output is correct
3 Correct 128 ms 5360 KB Output is correct
4 Correct 463 ms 86760 KB Output is correct
5 Correct 360 ms 83036 KB Output is correct
6 Correct 328 ms 81196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 77116 KB Output is correct
2 Correct 194 ms 77468 KB Output is correct
3 Correct 249 ms 77596 KB Output is correct
4 Correct 214 ms 17188 KB Output is correct
5 Correct 166 ms 13856 KB Output is correct
6 Correct 115 ms 9220 KB Output is correct
7 Correct 344 ms 85420 KB Output is correct
8 Correct 366 ms 85324 KB Output is correct
9 Correct 326 ms 85508 KB Output is correct
10 Correct 325 ms 85452 KB Output is correct
11 Correct 328 ms 85728 KB Output is correct
12 Correct 336 ms 86252 KB Output is correct
13 Correct 328 ms 86804 KB Output is correct
14 Correct 334 ms 87636 KB Output is correct
15 Correct 331 ms 87660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 77116 KB Output is correct
2 Correct 194 ms 77468 KB Output is correct
3 Correct 249 ms 77596 KB Output is correct
4 Correct 214 ms 17188 KB Output is correct
5 Correct 166 ms 13856 KB Output is correct
6 Correct 115 ms 9220 KB Output is correct
7 Correct 344 ms 85420 KB Output is correct
8 Correct 366 ms 85324 KB Output is correct
9 Correct 326 ms 85508 KB Output is correct
10 Correct 325 ms 85452 KB Output is correct
11 Correct 328 ms 85728 KB Output is correct
12 Correct 336 ms 86252 KB Output is correct
13 Correct 328 ms 86804 KB Output is correct
14 Correct 334 ms 87636 KB Output is correct
15 Correct 331 ms 87660 KB Output is correct
16 Correct 191 ms 77136 KB Output is correct
17 Correct 188 ms 77404 KB Output is correct
18 Correct 203 ms 77532 KB Output is correct
19 Correct 219 ms 17076 KB Output is correct
20 Correct 173 ms 14000 KB Output is correct
21 Correct 131 ms 9280 KB Output is correct
22 Correct 337 ms 85484 KB Output is correct
23 Correct 319 ms 85356 KB Output is correct
24 Correct 320 ms 85420 KB Output is correct
25 Correct 382 ms 85396 KB Output is correct
26 Correct 339 ms 85688 KB Output is correct
27 Correct 367 ms 86208 KB Output is correct
28 Correct 324 ms 86696 KB Output is correct
29 Correct 347 ms 87692 KB Output is correct
30 Correct 349 ms 87680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 441 ms 86436 KB Output is correct
2 Correct 409 ms 86448 KB Output is correct
3 Correct 511 ms 86840 KB Output is correct
4 Correct 389 ms 87008 KB Output is correct
5 Correct 438 ms 87556 KB Output is correct
6 Correct 545 ms 87884 KB Output is correct
7 Correct 464 ms 88192 KB Output is correct
8 Correct 445 ms 88512 KB Output is correct
9 Correct 452 ms 88796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Correct 2 ms 816 KB Output is correct
6 Correct 170 ms 39320 KB Output is correct
7 Correct 148 ms 39604 KB Output is correct
8 Correct 128 ms 39700 KB Output is correct
9 Correct 194 ms 44676 KB Output is correct
10 Correct 105 ms 9064 KB Output is correct
11 Correct 89 ms 7456 KB Output is correct
12 Correct 69 ms 5984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Correct 2 ms 816 KB Output is correct
6 Correct 363 ms 79812 KB Output is correct
7 Correct 311 ms 75192 KB Output is correct
8 Correct 146 ms 11188 KB Output is correct
9 Correct 202 ms 13644 KB Output is correct
10 Correct 163 ms 10336 KB Output is correct
11 Correct 128 ms 5360 KB Output is correct
12 Correct 463 ms 86760 KB Output is correct
13 Correct 360 ms 83036 KB Output is correct
14 Correct 328 ms 81196 KB Output is correct
15 Correct 199 ms 77116 KB Output is correct
16 Correct 194 ms 77468 KB Output is correct
17 Correct 249 ms 77596 KB Output is correct
18 Correct 214 ms 17188 KB Output is correct
19 Correct 166 ms 13856 KB Output is correct
20 Correct 115 ms 9220 KB Output is correct
21 Correct 344 ms 85420 KB Output is correct
22 Correct 366 ms 85324 KB Output is correct
23 Correct 326 ms 85508 KB Output is correct
24 Correct 325 ms 85452 KB Output is correct
25 Correct 328 ms 85728 KB Output is correct
26 Correct 336 ms 86252 KB Output is correct
27 Correct 328 ms 86804 KB Output is correct
28 Correct 334 ms 87636 KB Output is correct
29 Correct 331 ms 87660 KB Output is correct
30 Correct 191 ms 77136 KB Output is correct
31 Correct 188 ms 77404 KB Output is correct
32 Correct 203 ms 77532 KB Output is correct
33 Correct 219 ms 17076 KB Output is correct
34 Correct 173 ms 14000 KB Output is correct
35 Correct 131 ms 9280 KB Output is correct
36 Correct 337 ms 85484 KB Output is correct
37 Correct 319 ms 85356 KB Output is correct
38 Correct 320 ms 85420 KB Output is correct
39 Correct 382 ms 85396 KB Output is correct
40 Correct 339 ms 85688 KB Output is correct
41 Correct 367 ms 86208 KB Output is correct
42 Correct 324 ms 86696 KB Output is correct
43 Correct 347 ms 87692 KB Output is correct
44 Correct 349 ms 87680 KB Output is correct
45 Correct 441 ms 86436 KB Output is correct
46 Correct 409 ms 86448 KB Output is correct
47 Correct 511 ms 86840 KB Output is correct
48 Correct 389 ms 87008 KB Output is correct
49 Correct 438 ms 87556 KB Output is correct
50 Correct 545 ms 87884 KB Output is correct
51 Correct 464 ms 88192 KB Output is correct
52 Correct 445 ms 88512 KB Output is correct
53 Correct 452 ms 88796 KB Output is correct
54 Correct 170 ms 39320 KB Output is correct
55 Correct 148 ms 39604 KB Output is correct
56 Correct 128 ms 39700 KB Output is correct
57 Correct 194 ms 44676 KB Output is correct
58 Correct 105 ms 9064 KB Output is correct
59 Correct 89 ms 7456 KB Output is correct
60 Correct 69 ms 5984 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 264 ms 78520 KB Output is correct
63 Correct 312 ms 78812 KB Output is correct
64 Correct 281 ms 78908 KB Output is correct
65 Correct 207 ms 17700 KB Output is correct
66 Correct 172 ms 14416 KB Output is correct
67 Correct 138 ms 9796 KB Output is correct
68 Correct 452 ms 89104 KB Output is correct