Submission #768819

# Submission time Handle Problem Language Result Execution time Memory
768819 2023-06-28T16:58:56 Z rainboy Birthday gift (IZhO18_treearray) C
0 / 100
1 ms 340 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define M	200000
#define Q	200000
#define N_	(M * 2 + Q * 3 + 1)
#define INF	0x3f3f3f3f

unsigned int X = 12345;

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

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 dd[N], pp[N], qq[N], qu[N], cnt;

int dfs1(int p, int i, int d) {
	int o, s, k_, j_;

	pp[i] = p, dd[i] = d;
	s = 1, k_ = 0, j_ = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			int k = dfs1(i, j, d + 1);

			s += k;
			if (k_ < k)
				k_ = k, j_ = j;
		}
	}
	qq[i] = j_;
	return s;
}

void dfs2(int p, int i, int q) {
	int o, j_;

	j_ = qq[i], qq[i] = q;
	if (j_ != -1)
		dfs2(i, j_, q);
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && j != j_)
			dfs2(i, j, j);
	}
}

int lca(int i, int j) {
	while (qq[i] != qq[j])
		if (dd[qq[i]] > dd[qq[j]])
			i = pp[qq[i]];
		else
			j = pp[qq[j]];
	return dd[i] < dd[j] ? i : j;
}

int zz[N_], ll[N_], rr[N_], kk[N_], u_, l_, r_;

int node(int k) {
	static int _ = 1;

	zz[_] = rand_();
	kk[_] = k;
	return _++;
}

void split(int u, int k) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	if (kk[u] < k) {
		split(rr[u], k);
		rr[u] = l_, l_ = u;
	} else if (kk[u] > k) {
		split(ll[u], k);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

int tr_add(int u, int k) {
	split(u, k);
	return merge(merge(l_, node(k)), r_);
}

int tr_remove(int u, int k) {
	split(u, k);
	return merge(l_, r_);
}

int tr_ceil(int u, int k) {
	int k_ = INF;

	while (u)
		if (kk[u] >= k)
			k_ = kk[u], u = ll[u];
		else
			u = rr[u];
	return k_;
}

int main() {
	static int ii[M], tt[N];
	int n, m, q, h, i, j, l, r;

	scanf("%d%d%d", &n, &m, &q);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	dfs1(-1, 0, 0);
	dfs2(-1, 0, 0);
	for (h = 0; h < m; h++)
		scanf("%d", &ii[h]), ii[h]--;
	for (h = 0; h < m; h++) {
		i = ii[h];
		tt[i] = merge(tt[i], node(h * 2));
		if (h + 1 < m) {
			i = lca(ii[h], ii[h + 1]);
			tt[i] = merge(tt[i], node(h * 2 + 1));
		}
	}
	while (q--) {
		int t;

		scanf("%d", &t);
		if (t == 1) {
			scanf("%d%d", &h, &j), h--, j--;
			i = ii[h];
			tt[i] = tr_remove(tt[i], h * 2);
			if (h > 0) {
				i = lca(ii[h - 1], ii[h]);
				tt[i] = tr_remove(tt[i], h * 2 - 1);
			}
			if (h + 1 < m) {
				i = lca(ii[h], ii[h + 1]);
				tt[i] = tr_remove(tt[i], h * 2 + 1);
			}
			ii[h] = j;
			i = ii[h];
			tt[i] = tr_add(tt[i], h * 2);
			if (h > 0) {
				i = lca(ii[h - 1], ii[h]);
				tt[i] = tr_add(tt[i], h * 2 - 1);
			}
			if (h + 1 < m) {
				i = lca(ii[h], ii[h + 1]);
				tt[i] = tr_add(tt[i], h * 2 + 1);
			}
		} else {
			scanf("%d%d%d", &l, &r, &i), l--, r--, i--;
			if ((h = tr_ceil(tt[i], l * 2)) <= r * 2)
				printf("%d %d\n", h / 2 + 1, (h + 1) / 2 + 1);
			else
				printf("-1\n");
		}
	}
	return 0;
}

Compilation message

treearray.c: In function 'append':
treearray.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
treearray.c: In function 'main':
treearray.c:137:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |  scanf("%d%d%d", &n, &m, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.c:141:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
treearray.c:147:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |   scanf("%d", &ii[h]), ii[h]--;
      |   ^~~~~~~~~~~~~~~~~~~
treearray.c:159:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |   scanf("%d", &t);
      |   ^~~~~~~~~~~~~~~
treearray.c:161:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |    scanf("%d%d", &h, &j), h--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
treearray.c:184:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |    scanf("%d%d%d", &l, &r, &i), l--, r--, i--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -