답안 #441684

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441684 2021-07-05T19:24:23 Z rainboy Zamjene (COCI16_zamjene) C
0 / 140
1537 ms 97796 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 (tt[h] == 1 || 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;
					yy[i_] = ((long long) yy[i_] - ppy[aa[i]] + ppy[aa[j]]) % MD;
					xx[j_] = ((long long) xx[j_] - ppx[aa[j]] + ppx[aa[i]]) % MD;
					yy[j_] = ((long long) yy[j_] - ppy[aa[j]] + ppy[aa[i]]) % 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
				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] <= 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);
      |    ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 8140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 8180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 8220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 8212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 8220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 9036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 75 ms 16676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 959 ms 73412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1537 ms 97796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1020 ms 93556 KB Output isn't correct
2 Halted 0 ms 0 KB -