Submission #441686

# Submission time Handle Problem Language Result Execution time Memory
441686 2021-07-05T19:41:39 Z rainboy Zamjene (COCI16_zamjene) C
140 / 140
1931 ms 114012 KB
#include <stdio.h>

#define N	1000000
#define Q	1000000
#define A	1000000
#define M	(N + (Q * 4) + 1)
#define MD	0x7fffffff

unsigned int Z = 12345;

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

int ppx[A + 1], ppy[A + 1], X, Y;

void init() {
	int a;

	X = rand_(), Y = rand_();
	ppx[0] = ppy[0] = 1;
	for (a = 1; a <= A; a++) {
		ppx[a] = (long long) ppx[a - 1] * X % MD;
		ppy[a] = (long long) ppy[a - 1] * Y % MD;
	}
}

long long zz[M], zz_[M]; int sz_[M], idx[M], neg[M];

int compare_a(int a, int b) {
	return a - b;
}

int compare_i(int i, int j) {
	return zz[i] == zz[j] ? 0 : (zz[i] < zz[j] ? -1 : 1);
}

int (*compare)(int, int);

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

		while (j < k) {
			int c = compare(ii[j], i_);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int ds[N], sz[N], xx[N], yy[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];
		xx[j] = ((long long) xx[j] + xx[i]) % MD;
		yy[j] = ((long long) yy[j] + yy[i]) % MD;
	} else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
		sz[i] += sz[j];
		xx[i] = ((long long) xx[i] + xx[j]) % MD;
		yy[i] = ((long long) yy[i] + yy[j]) % MD;
	}
}

long long get(int i) {
	return (long long) xx[i] * MD + yy[i];
}

int cnt[M]; long long sum;

void update(int i, int x) {
	if (i == neg[i])
		sum += (long long) (cnt[neg[i]] + x) * (cnt[neg[i]] + x - 1) / 2 - (long long) cnt[neg[i]] * (cnt[neg[i]] - 1) / 2;
	else if (neg[i] != -1)
		sum += (long long) x * cnt[neg[i]];
	cnt[i] += x;
}

int main() {
	static int aa[N], bb[N], tt[Q], ii[M];
	int n, m, q, k, h, i, j;

	init();
	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]), bb[i] = aa[i];
	compare = compare_a, sort(bb, 0, n);
	m = 0;
	zz[m++] = 0;
	for (i = 0; i < n; i++) {
		ds[i] = -1, sz[i] = 1;
		xx[i] = ((long long) ppx[aa[i]] - ppx[bb[i]] + MD) % MD;
		yy[i] = ((long long) ppy[aa[i]] - ppy[bb[i]] + MD) % MD;
		zz[m] = get(i), sz_[m] = 1, m++;
	}
	for (h = 0; h < q; h++) {
		scanf("%d", &tt[h]);
		if (tt[h] <= 2) {
			int i_, j_, tmp;

			scanf("%d%d", &i, &j), i--, j--, i_ = find(i), j_ = find(j);
			if (i_ != j_) {
				zz[m] = get(i_), sz_[m] = -sz[i_], m++, zz[m] = get(j_), sz_[m] = -sz[j_], m++;
				if (tt[h] == 1) {
					xx[i_] = ((long long) xx[i_] - ppx[aa[i]] + ppx[aa[j]] + MD) % MD;
					yy[i_] = ((long long) yy[i_] - ppy[aa[i]] + ppy[aa[j]] + MD) % MD;
					xx[j_] = ((long long) xx[j_] - ppx[aa[j]] + ppx[aa[i]] + MD) % MD;
					yy[j_] = ((long long) yy[j_] - ppy[aa[j]] + ppy[aa[i]] + MD) % MD;
					tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
					zz[m] = get(i_), sz_[m] = sz[i_], m++, zz[m] = get(j_), sz_[m] = sz[j_], m++;
				} else {
					join(i, j), i_ = find(i);
					zz[m] = get(i_), sz_[m] = sz[i_], m++;
				}
			} else {
				if (tt[h] == 1)
					tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				tt[h] = 0;
			}
		}
	}
	for (i = 0; i < m; i++)
		ii[i] = i;
	compare = compare_i, sort(ii, 0, m);
	for (i = 0, k = 0; i < m; i++) {
		idx[ii[i]] = k;
		if (i + 1 == m || zz[ii[i + 1]] != zz[ii[i]])
			zz_[k++] = zz[ii[i]];
	}
	for (i = 0; i < k; i++) {
		zz[i] = (MD - zz_[i] / MD) % MD * MD + (MD - zz_[i] % MD) % MD;
		ii[i] = i;
	}
	compare = compare_i, sort(ii, 0, k);
	for (i = 0, j = 0; i < k; i++) {
		while (j < k && zz[ii[j]] < zz_[i])
			j++;
		if (j < k && zz[ii[j]] == zz_[i])
			neg[i] = ii[j];
		else
			neg[i] = -1;
	}
	for (i = 1; i <= n; i++)
		update(idx[i], sz_[i]);
	for (h = 0; h < q; h++)
		if (tt[h] == 1 || tt[h] == 2) {
			update(idx[i], sz_[i]), i++;
			update(idx[i], sz_[i]), i++;
			if (tt[h] == 1)
				update(idx[i], sz_[i]), i++, update(idx[i], sz_[i]), i++;
			else
				update(idx[i], sz_[i]), i++;
		} else if (tt[h] == 3)
			printf(sum == (long long) n * (n - 1) / 2 ? "DA\n" : "NE\n");
		else if (tt[h] == 4)
			printf("%lld\n", sum - (long long) cnt[0] * (cnt[0] - 1) / 2);
	return 0;
}

Compilation message

zamjene.c: In function 'main':
zamjene.c:107:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
zamjene.c:109:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |   scanf("%d", &aa[i]), bb[i] = aa[i];
      |   ^~~~~~~~~~~~~~~~~~~
zamjene.c:120:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |   scanf("%d", &tt[h]);
      |   ^~~~~~~~~~~~~~~~~~~
zamjene.c:124:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |    scanf("%d%d", &i, &j), i--, j--, i_ = find(i), j_ = find(j);
      |    ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8200 KB Output is correct
2 Correct 10 ms 8132 KB Output is correct
3 Correct 10 ms 8236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8236 KB Output is correct
2 Correct 10 ms 8212 KB Output is correct
3 Correct 10 ms 8164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8140 KB Output is correct
2 Correct 10 ms 8240 KB Output is correct
3 Correct 10 ms 8140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 8140 KB Output is correct
2 Correct 10 ms 8176 KB Output is correct
3 Correct 11 ms 8260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8268 KB Output is correct
2 Correct 11 ms 8232 KB Output is correct
3 Correct 11 ms 8268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8944 KB Output is correct
2 Correct 16 ms 9116 KB Output is correct
3 Correct 16 ms 9164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 16576 KB Output is correct
2 Correct 95 ms 17996 KB Output is correct
3 Correct 117 ms 18400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 934 ms 73040 KB Output is correct
2 Correct 1751 ms 96020 KB Output is correct
3 Correct 1843 ms 100272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1545 ms 97632 KB Output is correct
2 Correct 1931 ms 114012 KB Output is correct
3 Correct 1137 ms 113388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 986 ms 93704 KB Output is correct
2 Correct 1709 ms 109580 KB Output is correct
3 Correct 1204 ms 113372 KB Output is correct