Submission #382617

#TimeUsernameProblemLanguageResultExecution timeMemory
382617rainboyBridges (APIO19_bridges)C11
13 / 100
109 ms2284 KiB
#include <stdio.h>

#define N	50000
#define M	100000
#define N_	(1 << 16)	/* N_ = pow2(ceil(log2(N))) */
#define INF	0x3f3f3f3f

int max(int a, int b) { return a > b ? a : b; }

int ds[N], sz[N];

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

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

int st[N_ * 2], n_;

void pul(int i) {
	st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}

void build(int *aa, int n) {
	int i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	for (i = 0; i < n_; i++)
		st[n_ + i] = i < n ? aa[i] : INF;
	for (i = n_ - 1; i > 0; i--)
		pul(i);
}

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

int query_l(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 query_r(int l, int x) {
	int r = n_ - 1;

	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
		if ((l & 1) == 1) {
			if (st[l] > x) {
				while (l < n_)
					l = st[l << 1 | 0] > x ? l << 1 | 0 : l << 1 | 1;
				return l - n_;
			}
			l++;
		}
	return n_;
}

int main() {
	static int ii[M], jj[M], ww[M];
	int n, m, q, h;

	scanf("%d%d", &n, &m);
	for (h = 0; h < m; h++)
		scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
	scanf("%d", &q);
	if (n <= 1000 && m <= 1000 && q <= 100000) {
		while (q--) {
			int t, w;

			scanf("%d", &t);
			if (t == 1) {
				scanf("%d%d", &h, &w), h--;
				ww[h] = w;
			} else {
				int s, i;

				scanf("%d%d", &s, &w), s--;
				for (i = 0; i < n; i++)
					ds[i] = -1, sz[i] = 1;
				for (h = 0; h < m; h++)
					if (w <= ww[h])
						join(ii[h], jj[h]);
				printf("%d\n", sz[find(s)]);
			}
		}
	} else {
		build(ww, n - 1);
		while (q--) {
			int t, w;

			scanf("%d", &t);
			if (t == 1) {
				scanf("%d%d", &h, &w), h--;
				update(h, w);
			} else {
				int s;

				scanf("%d%d", &s, &w), s--;
				printf("%d\n", query_r(s, w) - query_l(s - 1, w));
			}
		}
	}
	return 0;
}

Compilation message (stderr)

bridges.c: In function 'main':
bridges.c:88:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   88 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
bridges.c:90:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   90 |   scanf("%d%d%d", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.c:91:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   91 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
bridges.c:96:4: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   96 |    scanf("%d", &t);
      |    ^~~~~~~~~~~~~~~
bridges.c:98:5: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   98 |     scanf("%d%d", &h, &w), h--;
      |     ^~~~~~~~~~~~~~~~~~~~~
bridges.c:103:5: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  103 |     scanf("%d%d", &s, &w), s--;
      |     ^~~~~~~~~~~~~~~~~~~~~
bridges.c:117:4: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  117 |    scanf("%d", &t);
      |    ^~~~~~~~~~~~~~~
bridges.c:119:5: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  119 |     scanf("%d%d", &h, &w), h--;
      |     ^~~~~~~~~~~~~~~~~~~~~
bridges.c:124:5: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  124 |     scanf("%d%d", &s, &w), s--;
      |     ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...