Submission #382593

# Submission time Handle Problem Language Result Execution time Memory
382593 2021-03-27T20:45:36 Z rainboy Strange Device (APIO19_strange_device) C
35 / 100
1675 ms 128168 KB
#include <stdio.h>

#define N	1000000

unsigned int X = 12345;

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

long long gcd(long long a, long long b) {
	return b == 0 ? a : gcd(b, a % b);
}

long long xx[N * 2], yy[N * 4];

int compare_xy(int i, int j) {
	return xx[i] != xx[j] ? (xx[i] < xx[j] ? -1 : 1) : (yy[i] == yy[j] ? 0 : (yy[i] < yy[j] ? -1 : 1));
}

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

int compare_y(int i, int j) {
	return yy[i] == yy[j] ? 0 : (yy[i] < yy[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 main() {
	static long long ll[N], rr[N];
	static int ii[N * 4], kk[N * 2];
	int n, n_, i, x, small, cnt;
	long long a, b, c, ans;

	scanf("%d%lld%lld", &n, &a, &b), c = a / gcd(a, b + 1);
	if (n == 1) {
		scanf("%lld%lld", &ll[0], &rr[0]);
		printf("%lld\n", c <= (rr[0] - ll[0] + 1) / b ? c * b : rr[0] - ll[0] + 1);
		return 0;
	}
	small = 1;
	for (i = 0; i < n; i++) {
		scanf("%lld%lld", &ll[i], &rr[i]);
		if (rr[i] - ll[i] + 1 > b)
			small = 0;
	}
	if (!small) {
		n_ = 0;
		for (i = 0; i < n; i++)
			while (ll[i] <= rr[i]) {
				xx[n_] = (ll[i] + ll[i] / b) % a, yy[n_] = ll[i] % b, n_++;
				ll[i]++;
			}
		n = n_;
		for (i = 0; i < n; i++)
			ii[i] = i;
		compare = compare_xy, sort(ii, 0, n);
		ans = 1;
		for (i = 1; i < n; i++)
			if (xx[ii[i]] != xx[ii[i - 1]] || yy[ii[i]] != yy[ii[i - 1]])
				ans++;
	} else {
		n_ = 0;
		for (i = 0; i < n; i++)
			if (ll[i] / b == rr[i] / b)
				xx[n_] = ll[i] / b * (b + 1) % a, yy[n_ << 1 | 0] = ll[i] % b, yy[n_ << 1 | 1] = rr[i] % b + 1, n_++;
			else {
				xx[n_] = ll[i] / b * (b + 1) % a, yy[n_ << 1 | 0] = ll[i] % b, yy[n_ << 1 | 1] = b, n_++;
				xx[n_] = rr[i] / b * (b + 1) % a, yy[n_ << 1 | 0] = 0, yy[n_ << 1 | 1] = rr[i] % b + 1, n_++;
			}
		for (i = 0; i < n_; i++)
			ii[i] = i;
		compare = compare_x, sort(ii, 0, n_);
		for (i = 0, x = 0; i < n_; i++)
			xx[ii[i]] = i + 1 == n_ || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
		for (i = 0; i < n_ * 2; i++)
			ii[i] = i;
		compare = compare_y, sort(ii, 0, n_ * 2);
		ans = 0, cnt = 0;
		for (i = 0; i + 1 < n_ * 2; i++) {
			int i_ = ii[i];

			x = xx[i_ >> 1];
			if ((i_ & 1) == 0) {
				if (kk[x]++ == 0)
					cnt++;
			} else {
				if (--kk[x] == 0)
					cnt--;
			}
			ans += (yy[ii[i + 1]] - yy[ii[i]]) * cnt;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

strange_device.c: In function 'main':
strange_device.c:59:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   59 |  scanf("%d%lld%lld", &n, &a, &b), c = a / gcd(a, b + 1);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.c:61:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   61 |   scanf("%lld%lld", &ll[0], &rr[0]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.c:67:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   67 |   scanf("%lld%lld", &ll[i], &rr[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 7 ms 876 KB Output is correct
3 Correct 8 ms 1004 KB Output is correct
4 Correct 3 ms 620 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 14 ms 1516 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 79 ms 6252 KB Output is correct
16 Correct 7 ms 1004 KB Output is correct
17 Correct 64 ms 5504 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 155 ms 12908 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 204 ms 12524 KB Output is correct
5 Runtime error 459 ms 128168 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 581 ms 35692 KB Output is correct
3 Runtime error 632 ms 127724 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 581 ms 35692 KB Output is correct
3 Runtime error 632 ms 127724 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 581 ms 35692 KB Output is correct
3 Runtime error 632 ms 127724 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 78 ms 5100 KB Output is correct
3 Correct 104 ms 7660 KB Output is correct
4 Correct 1675 ms 63152 KB Output is correct
5 Correct 98 ms 7660 KB Output is correct
6 Correct 97 ms 7660 KB Output is correct
7 Correct 97 ms 7660 KB Output is correct
8 Correct 110 ms 8172 KB Output is correct
9 Correct 113 ms 8044 KB Output is correct
10 Correct 85 ms 6764 KB Output is correct
11 Correct 76 ms 7532 KB Output is correct
12 Correct 78 ms 7404 KB Output is correct
13 Correct 101 ms 7660 KB Output is correct
14 Correct 893 ms 49516 KB Output is correct
15 Correct 75 ms 7404 KB Output is correct
16 Correct 867 ms 49256 KB Output is correct
17 Correct 1205 ms 62316 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 7 ms 876 KB Output is correct
3 Correct 8 ms 1004 KB Output is correct
4 Correct 3 ms 620 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 14 ms 1516 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 79 ms 6252 KB Output is correct
16 Correct 7 ms 1004 KB Output is correct
17 Correct 64 ms 5504 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 155 ms 12908 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 204 ms 12524 KB Output is correct
28 Runtime error 459 ms 128168 KB Execution killed with signal 11