답안 #948184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948184 2024-03-17T19:31:46 Z rainboy Walk (POI13_spa) C
62 / 100
264 ms 22356 KB
#include <stdio.h>
#include <string.h>

#define N	1000000
#define N_	(N * 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();
      |  ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 1 ms 6588 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 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 6492 KB Output is correct
11 Correct 81 ms 17960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 17964 KB Output is correct
2 Correct 91 ms 13492 KB Output is correct
3 Correct 103 ms 13848 KB Output is correct
4 Correct 126 ms 13756 KB Output is correct
5 Correct 139 ms 17180 KB Output is correct
6 Correct 35 ms 11348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 4 ms 6612 KB Output is correct
3 Correct 5 ms 6488 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 3 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 6492 KB Output is correct
2 Incorrect 5 ms 6492 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 11600 KB Output is correct
2 Incorrect 135 ms 11500 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 11428 KB Output is correct
2 Incorrect 40 ms 8796 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 219 ms 17960 KB Output is correct
2 Correct 113 ms 18348 KB Output is correct
3 Correct 148 ms 22356 KB Output is correct
4 Correct 106 ms 14116 KB Output is correct
5 Correct 107 ms 14196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 203 ms 17840 KB Output is correct
2 Correct 47 ms 10844 KB Output is correct
3 Correct 48 ms 10812 KB Output is correct
4 Correct 135 ms 17236 KB Output is correct
5 Correct 135 ms 17236 KB Output is correct