답안 #793495

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793495 2023-07-25T22:45:03 Z rainboy Rooted MST (innopolis2021_final_E) C
10 / 100
2911 ms 87412 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--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2911 ms 52132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 584 ms 14088 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 49744 KB Output is correct
2 Correct 249 ms 55716 KB Output is correct
3 Incorrect 249 ms 55724 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 49744 KB Output is correct
2 Correct 249 ms 55716 KB Output is correct
3 Incorrect 249 ms 55724 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2344 ms 72876 KB Output is correct
2 Correct 2330 ms 74308 KB Output is correct
3 Correct 2354 ms 74956 KB Output is correct
4 Correct 2308 ms 75716 KB Output is correct
5 Correct 2347 ms 73228 KB Output is correct
6 Correct 2323 ms 73160 KB Output is correct
7 Correct 2427 ms 86876 KB Output is correct
8 Correct 2293 ms 87116 KB Output is correct
9 Correct 2067 ms 87412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -