Submission #948187

# Submission time Handle Problem Language Result Execution time Memory
948187 2024-03-17T19:33:31 Z rainboy Walk (POI13_spa) C
100 / 100
268 ms 17956 KB
#include <stdio.h>
#include <string.h>

#define N	1000000
#define N_	((N + 1) * 2)
#define L	60

long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }

unsigned int X = 12345;

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

int k;

long long parse() {
	static char cc[L + 1];
	int h;
	long long x;

	scanf("%s", cc);
	x = 0;
	for (h = 0; h < k; h++)
		if (cc[h] == '1')
			x |= 1LL << h;
	return x;
}

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

		while (j < k)
			if (aa[j] == a)
				j++;
			else if (aa[j] < a) {
				tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
			}
		sort(aa, l, i);
		l = k;
	}
}

int ds[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;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
}

int main() {
	static long long aa[N], ll[N_], rr[N_];
	int n, n_, h, i, il, ir, j;
	long long s, t, l, r, u, v, x, x_;

	scanf("%d%d", &k, &n), s = parse(), t = parse();
	for (i = 0; i < n; i++)
		aa[i] = parse();
	sort(aa, 0, n);
	n_ = 0;
	for (i = -1; i < n; i++) {
		l = i < 0 ? 0 : aa[i] + 1, r = i + 1 == n ? (1LL << k) - 1 : aa[i + 1] - 1;
		if (l <= r) {
			if (l == r)
				ll[n_] = l, rr[n_] = r, n_++;
			else
				for (h = k - 1; h >= 0; h--)
					if ((l & 1LL << h) != (r & 1LL << h)) {
						ll[n_] = l, rr[n_] = l | (1LL << h) - 1, n_++, ll[n_] = r & ~((1LL << h) - 1), rr[n_] = r, n_++;
						break;
					}
		}
	}
	memset(ds, -1, n_ * sizeof *ds);
	for (h = 0; h < k; h++) {
		u = 1LL << h, v = 1LL << k - 1 - h, x = 0, il = 0, ir = 0;
		while (x < v) {
			while (il < n_ && rr[il] < u * x * 2)
				il++;
			while (ir < n_ && ll[ir] < u * (x * 2 + 2))
				ir++;
			i = il, j = il;
			while (i < ir && j < ir) {
				if (ll[i] < u * (x * 2 + 1) && rr[j] >= u * (x * 2 + 1) && max(ll[i], ll[j] - u) <= min(rr[i], rr[j] - u))
					join(i, j);
				if (rr[i] < rr[j] - u)
					i++;
				else
					j++;
			}
			x_ = v;
			if (il < n_)
				x_ = min(x_, rr[il] / u / 2 + 1);
			if (ir < n_)
				x_ = min(x_, ll[ir] / u / 2);
			x = x_;
		}
	}
	for (i = 0; i < n_; i++)
		if (ll[i] <= s && s <= rr[i])
			break;
	for (j = 0; j < n_; j++)
		if (ll[j] <= t && t <= rr[j])
			break;
	printf(find(i) == find(j) ? "TAK\n" : "NIE\n");
	return 0;
}

Compilation message

spa.c: In function 'main':
spa.c:90:43: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
   90 |       ll[n_] = l, rr[n_] = l | (1LL << h) - 1, n_++, ll[n_] = r & ~((1LL << h) - 1), rr[n_] = r, n_++;
      |                                ~~~~~~~~~~~^~~
spa.c:97:34: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   97 |   u = 1LL << h, v = 1LL << k - 1 - h, x = 0, il = 0, ir = 0;
      |                            ~~~~~~^~~
spa.c: In function 'parse':
spa.c:24:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |  scanf("%s", cc);
      |  ^~~~~~~~~~~~~~~
spa.c: In function 'main':
spa.c:77:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |  scanf("%d%d", &k, &n), s = parse(), t = parse();
      |  ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6548 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 80 ms 13652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 17836 KB Output is correct
2 Correct 102 ms 13648 KB Output is correct
3 Correct 102 ms 13724 KB Output is correct
4 Correct 126 ms 13756 KB Output is correct
5 Correct 130 ms 13752 KB Output is correct
6 Correct 31 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6488 KB Output is correct
2 Correct 4 ms 6488 KB Output is correct
3 Correct 5 ms 6492 KB Output is correct
4 Correct 3 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 6492 KB Output is correct
2 Correct 5 ms 6492 KB Output is correct
3 Correct 4 ms 6588 KB Output is correct
4 Correct 12 ms 6844 KB Output is correct
5 Correct 11 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 11424 KB Output is correct
2 Correct 137 ms 11496 KB Output is correct
3 Correct 125 ms 13908 KB Output is correct
4 Correct 75 ms 10816 KB Output is correct
5 Correct 73 ms 10580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 268 ms 11428 KB Output is correct
2 Correct 41 ms 8792 KB Output is correct
3 Correct 39 ms 9820 KB Output is correct
4 Correct 142 ms 12444 KB Output is correct
5 Correct 147 ms 12372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 17956 KB Output is correct
2 Correct 111 ms 13832 KB Output is correct
3 Correct 127 ms 17956 KB Output is correct
4 Correct 105 ms 11356 KB Output is correct
5 Correct 110 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 17744 KB Output is correct
2 Correct 46 ms 9052 KB Output is correct
3 Correct 46 ms 9052 KB Output is correct
4 Correct 140 ms 13640 KB Output is correct
5 Correct 134 ms 13396 KB Output is correct