Submission #793496

# Submission time Handle Problem Language Result Execution time Memory
793496 2023-07-25T22:46:56 Z rainboy Rooted MST (innopolis2021_final_E) C
100 / 100
3295 ms 73076 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define N	300000
#define M	600000
#define Q	300000
#define K	(N + Q)
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N + 1))) */
#define INF	0x3f3f3f3f
 
int max(int a, int b) { return a > b ? a : b; }
 
unsigned int X = 12345;
 
int rand_() {
	return (X *= 3) >> 1;
}
 
int ii[M], jj[M], ww[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) {
			int c = ww[hh[j]] != ww[h] ? ww[hh[j]] - ww[h] : hh[j] - h;

			if (c == 0)
				j++;
			else if (c < 0) {
				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], eo[N];
 
void append(int i, int j) {
	int o = eo[i]++;
 
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}
 
int ta[N], tb[N], qu[N];
 
void dfs(int p, int i) {
	static int time;
	int o;
 
	qu[ta[i] = time++] = i;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];
 
		if (j != p)
			dfs(i, j);
	}
	tb[i] = time;
}
 
int st[N_ * 2], n_;
 
void pul(int i) {
	st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}
 
void update(int i, int x) {
	st[i += n_] = x;
	while (i > 1)
		pul(i >>= 1);
}
 
int prev(int r, int x) {
	int l = 0;
 
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((r & 1) == 0) {
			if (st[r] >= x) {
				while (r < n_)
					r = st[r << 1 | 1] >= x ? r << 1 | 1 : r << 1 | 0;
				return r - n_;
			}
			r--;
		}
	return -1;
}
 
int ds[N], hh_[N];
 
int find(int i) {
	return ds[i] < 0 ? i : find(ds[i]);
}
 
long long ans;
 
void join(int h) {
	int i = find(ii[h]), j = find(jj[h]);
 
	if (i == j)
		return;
	append(ii[h], jj[h]), append(jj[h], ii[h]), ans += ww[h];
	if (ds[i] > ds[j])
		ds[i] = j, hh_[i] = h;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, hh_[j] = h;
	}
}
 
int query(int i, int j) {
	int h = -1;
 
	while (i != j)
		if (hh_[j] == -1 || hh_[i] != -1 && ww[hh_[i]] < ww[hh_[j]] || ww[hh_[i]] == ww[hh_[j]] && hh_[i] < hh_[j])
			h = hh_[i], i = ds[i];
		else
			h = hh_[j], j = ds[j];
	return h;
}
 
int xx[N], uu[N], tt[N], zz[N], dd[N], cnt;
 
void add(int i, int x) {
	int r, u, h, j;
 
	xx[i] = x;
	r = qu[prev(ta[i], tb[i])];
	if (uu[r] == -1) {
		ans += x, tt[cnt] = -1, dd[cnt] = x, cnt++;
		uu[r] = i;
	} else {
		u = uu[r], h = query(u, i);
		if (max(ww[h], xx[u]) <= x)
			tt[cnt] = 0, dd[cnt] = 0, cnt++;
		else if (ww[h] < xx[u]) {
			ans += x - xx[u], tt[cnt] = 1, zz[cnt] = u, dd[cnt] = x - xx[u], cnt++;
			uu[r] = i;
		} else {
			ans += x - ww[h], tt[cnt] = 2, zz[cnt] = h, dd[cnt] = x - ww[h], cnt++;
			j = ta[ii[h]] > ta[jj[h]] ? ii[h] : jj[h];
			update(ta[j], tb[j]);
			if (ta[j] <= ta[i] && ta[i] < tb[j])
				uu[j] = i;
			else
				uu[r] = i, uu[j] = u;
		}
	}
}
 
void undo(int i) {
	int r, h, j;
 
	cnt--;
	ans -= dd[cnt];
	r = qu[prev(ta[i], tb[i])];
	if (tt[cnt] == -1)
		uu[r] = -1;
	else if (tt[cnt] == 1)
		uu[r] = zz[cnt];
	else if (tt[cnt] == 2) {
		h = zz[cnt], j = ta[ii[h]] > ta[jj[h]] ? ii[h] : jj[h];
		if (ta[i] < ta[j] || ta[i] >= tb[j])
			uu[r] = uu[j];
		update(ta[j], 0);
	}
}
 
int ll[K], rr[K], ii_[K], xx_[K], k;
 
void solve(int *gg, int k, int l, int r) {
	int g, g_, g1, g2, g3, m, tmp;
 
	g1 = 0, g2 = 0, g3 = k;
	while (g2 < g3)
		if (ll[gg[g2]] <= l && r <= rr[gg[g2]])
			g2++;
		else if (ll[gg[g2]] < r && l < rr[gg[g2]]) {
			tmp = gg[g1], gg[g1] = gg[g2], gg[g2] = tmp;
			g1++, g2++;
		} else {
			g3--;
			tmp = gg[g2], gg[g2] = gg[g3], gg[g3] = tmp;
		}
	for (g = g1; g < g2; g++) {
		g_ = gg[g];
		add(ii_[g_], xx_[g_]);
	}
	if (r - l == 1)
		printf("%lld\n", ans);
	else {
		m = (l + r) / 2;
		solve(gg, g1, l, m), solve(gg, g1, m, r);
	}
	for (g = g2 - 1; g >= g1; g--) {
		g_ = gg[g];
		undo(ii_[g_]);
	}
}
 
int main() {
	static int tt[N], hh[M], gg[K];
	int n, m, q, g, h, i, t, 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]--;
	for (i = 0; i + 1 < n; i++)
		ii[m] = i, jj[m] = i + 1, ww[m] = INF, m++;
	for (h = 0; h < m; h++)
		hh[h] = h;
	sort(hh, 0, m);
	memset(ds, -1, n * sizeof *ds), memset(hh_, -1, n * sizeof *hh_);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < m; h++)
		join(hh[h]);
	dfs(-1, 0);
	scanf("%d", &q);
	for (t = 0; t < q; t++) {
		scanf("%d%d", &i, &x), i--;
		if (tt[i] < t)
			ll[k] = tt[i], rr[k] = t, ii_[k] = i, xx_[k] = xx[i], k++;
		xx[i] = x, tt[i] = t;
	}
	for (i = 0; i < n; i++)
		ll[k] = tt[i], rr[k] = q, ii_[k] = i, xx_[k] = xx[i], k++;
	for (g = 0; g < k; g++)
		gg[g] = g;
	n_ = 1;
	while (n_ < n + 1)
		n_ <<= 1;
	update(ta[0], tb[0]);
	uu[0] = -1;
	solve(gg, k, 0, q);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:49:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   49 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'query':
Main.c:124:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  124 |   if (hh_[j] == -1 || hh_[i] != -1 && ww[hh_[i]] < ww[hh_[j]] || ww[hh_[i]] == ww[hh_[j]] && hh_[i] < hh_[j])
      |                       ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:124:91: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  124 |   if (hh_[j] == -1 || hh_[i] != -1 && ww[hh_[i]] < ww[hh_[j]] || ww[hh_[i]] == ww[hh_[j]] && hh_[i] < hh_[j])
      |                                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Main.c: In function 'main':
Main.c:214:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  214 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:216:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  216 |   scanf("%d", &xx[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:218:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  218 |   scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:230:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  230 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
Main.c:232:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  232 |   scanf("%d%d", &i, &x), i--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 3 ms 596 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2910 ms 52044 KB Output is correct
2 Correct 2755 ms 54960 KB Output is correct
3 Correct 640 ms 17140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 579 ms 12576 KB Output is correct
2 Correct 554 ms 11080 KB Output is correct
3 Correct 494 ms 8856 KB Output is correct
4 Correct 3295 ms 55880 KB Output is correct
5 Correct 3145 ms 62812 KB Output is correct
6 Correct 3197 ms 60980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 49848 KB Output is correct
2 Correct 254 ms 49264 KB Output is correct
3 Correct 265 ms 49020 KB Output is correct
4 Correct 211 ms 23064 KB Output is correct
5 Correct 159 ms 19720 KB Output is correct
6 Correct 105 ms 15100 KB Output is correct
7 Correct 502 ms 64204 KB Output is correct
8 Correct 557 ms 64204 KB Output is correct
9 Correct 518 ms 64248 KB Output is correct
10 Correct 522 ms 64140 KB Output is correct
11 Correct 514 ms 64448 KB Output is correct
12 Correct 495 ms 64964 KB Output is correct
13 Correct 488 ms 65608 KB Output is correct
14 Correct 467 ms 66580 KB Output is correct
15 Correct 474 ms 66452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 49848 KB Output is correct
2 Correct 254 ms 49264 KB Output is correct
3 Correct 265 ms 49020 KB Output is correct
4 Correct 211 ms 23064 KB Output is correct
5 Correct 159 ms 19720 KB Output is correct
6 Correct 105 ms 15100 KB Output is correct
7 Correct 502 ms 64204 KB Output is correct
8 Correct 557 ms 64204 KB Output is correct
9 Correct 518 ms 64248 KB Output is correct
10 Correct 522 ms 64140 KB Output is correct
11 Correct 514 ms 64448 KB Output is correct
12 Correct 495 ms 64964 KB Output is correct
13 Correct 488 ms 65608 KB Output is correct
14 Correct 467 ms 66580 KB Output is correct
15 Correct 474 ms 66452 KB Output is correct
16 Correct 325 ms 56036 KB Output is correct
17 Correct 331 ms 55612 KB Output is correct
18 Correct 320 ms 55660 KB Output is correct
19 Correct 250 ms 22992 KB Output is correct
20 Correct 221 ms 19760 KB Output is correct
21 Correct 176 ms 15164 KB Output is correct
22 Correct 550 ms 64188 KB Output is correct
23 Correct 561 ms 64244 KB Output is correct
24 Correct 537 ms 64332 KB Output is correct
25 Correct 569 ms 64364 KB Output is correct
26 Correct 522 ms 64520 KB Output is correct
27 Correct 578 ms 65068 KB Output is correct
28 Correct 551 ms 65616 KB Output is correct
29 Correct 546 ms 66520 KB Output is correct
30 Correct 522 ms 66440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2095 ms 72900 KB Output is correct
2 Correct 2217 ms 72908 KB Output is correct
3 Correct 2184 ms 73076 KB Output is correct
4 Correct 2013 ms 72976 KB Output is correct
5 Correct 2203 ms 73044 KB Output is correct
6 Correct 2024 ms 72888 KB Output is correct
7 Correct 2035 ms 72940 KB Output is correct
8 Correct 2083 ms 72908 KB Output is correct
9 Correct 1810 ms 72908 KB Output is correct
# Verdict Execution time Memory 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 3 ms 596 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
6 Correct 654 ms 29280 KB Output is correct
7 Correct 700 ms 29128 KB Output is correct
8 Correct 687 ms 29484 KB Output is correct
9 Correct 942 ms 34604 KB Output is correct
10 Correct 347 ms 12088 KB Output is correct
11 Correct 319 ms 10420 KB Output is correct
12 Correct 303 ms 8904 KB Output is correct
# Verdict Execution time Memory 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 3 ms 596 KB Output is correct
5 Correct 5 ms 880 KB Output is correct
6 Correct 2910 ms 52044 KB Output is correct
7 Correct 2755 ms 54960 KB Output is correct
8 Correct 640 ms 17140 KB Output is correct
9 Correct 579 ms 12576 KB Output is correct
10 Correct 554 ms 11080 KB Output is correct
11 Correct 494 ms 8856 KB Output is correct
12 Correct 3295 ms 55880 KB Output is correct
13 Correct 3145 ms 62812 KB Output is correct
14 Correct 3197 ms 60980 KB Output is correct
15 Correct 240 ms 49848 KB Output is correct
16 Correct 254 ms 49264 KB Output is correct
17 Correct 265 ms 49020 KB Output is correct
18 Correct 211 ms 23064 KB Output is correct
19 Correct 159 ms 19720 KB Output is correct
20 Correct 105 ms 15100 KB Output is correct
21 Correct 502 ms 64204 KB Output is correct
22 Correct 557 ms 64204 KB Output is correct
23 Correct 518 ms 64248 KB Output is correct
24 Correct 522 ms 64140 KB Output is correct
25 Correct 514 ms 64448 KB Output is correct
26 Correct 495 ms 64964 KB Output is correct
27 Correct 488 ms 65608 KB Output is correct
28 Correct 467 ms 66580 KB Output is correct
29 Correct 474 ms 66452 KB Output is correct
30 Correct 325 ms 56036 KB Output is correct
31 Correct 331 ms 55612 KB Output is correct
32 Correct 320 ms 55660 KB Output is correct
33 Correct 250 ms 22992 KB Output is correct
34 Correct 221 ms 19760 KB Output is correct
35 Correct 176 ms 15164 KB Output is correct
36 Correct 550 ms 64188 KB Output is correct
37 Correct 561 ms 64244 KB Output is correct
38 Correct 537 ms 64332 KB Output is correct
39 Correct 569 ms 64364 KB Output is correct
40 Correct 522 ms 64520 KB Output is correct
41 Correct 578 ms 65068 KB Output is correct
42 Correct 551 ms 65616 KB Output is correct
43 Correct 546 ms 66520 KB Output is correct
44 Correct 522 ms 66440 KB Output is correct
45 Correct 2095 ms 72900 KB Output is correct
46 Correct 2217 ms 72908 KB Output is correct
47 Correct 2184 ms 73076 KB Output is correct
48 Correct 2013 ms 72976 KB Output is correct
49 Correct 2203 ms 73044 KB Output is correct
50 Correct 2024 ms 72888 KB Output is correct
51 Correct 2035 ms 72940 KB Output is correct
52 Correct 2083 ms 72908 KB Output is correct
53 Correct 1810 ms 72908 KB Output is correct
54 Correct 654 ms 29280 KB Output is correct
55 Correct 700 ms 29128 KB Output is correct
56 Correct 687 ms 29484 KB Output is correct
57 Correct 942 ms 34604 KB Output is correct
58 Correct 347 ms 12088 KB Output is correct
59 Correct 319 ms 10420 KB Output is correct
60 Correct 303 ms 8904 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 1886 ms 58656 KB Output is correct
63 Correct 2035 ms 58248 KB Output is correct
64 Correct 2148 ms 58220 KB Output is correct
65 Correct 680 ms 23660 KB Output is correct
66 Correct 652 ms 20248 KB Output is correct
67 Correct 602 ms 15628 KB Output is correct
68 Correct 2524 ms 69084 KB Output is correct