Submission #948185

# Submission time Handle Problem Language Result Execution time Memory
948185 2024-03-17T19:32:23 Z rainboy Walk (POI13_spa) C
62 / 100
283 ms 18000 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; }
int max(int a, int 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 6492 KB Output is correct
6 Correct 1 ms 4440 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 79 ms 13648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 17824 KB Output is correct
2 Correct 96 ms 13652 KB Output is correct
3 Correct 98 ms 13648 KB Output is correct
4 Correct 133 ms 13732 KB Output is correct
5 Correct 127 ms 13904 KB Output is correct
6 Correct 30 ms 6316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 4 ms 6492 KB Output is correct
3 Correct 5 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 6492 KB Output is correct
2 Incorrect 4 ms 6492 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 283 ms 11628 KB Output is correct
2 Incorrect 138 ms 11436 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 258 ms 11432 KB Output is correct
2 Incorrect 40 ms 8796 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 209 ms 18000 KB Output is correct
2 Correct 114 ms 13648 KB Output is correct
3 Correct 120 ms 17748 KB Output is correct
4 Correct 105 ms 11356 KB Output is correct
5 Correct 104 ms 11352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 17960 KB Output is correct
2 Correct 46 ms 9112 KB Output is correct
3 Correct 46 ms 9044 KB Output is correct
4 Correct 139 ms 13480 KB Output is correct
5 Correct 156 ms 13656 KB Output is correct