Submission #948211

# Submission time Handle Problem Language Result Execution time Memory
948211 2024-03-17T22:28:59 Z rainboy Walk (POI13_spa) C
100 / 100
259 ms 17960 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 4 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 1 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 4596 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 2 ms 6644 KB Output is correct
11 Correct 79 ms 13728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 17960 KB Output is correct
2 Correct 93 ms 13664 KB Output is correct
3 Correct 99 ms 13728 KB Output is correct
4 Correct 131 ms 13656 KB Output is correct
5 Correct 129 ms 13732 KB Output is correct
6 Correct 30 ms 6308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6492 KB Output is correct
2 Correct 5 ms 6492 KB Output is correct
3 Correct 5 ms 6612 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 17 ms 6624 KB Output is correct
2 Correct 4 ms 6492 KB Output is correct
3 Correct 4 ms 6492 KB Output is correct
4 Correct 11 ms 6488 KB Output is correct
5 Correct 12 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 11608 KB Output is correct
2 Correct 138 ms 11604 KB Output is correct
3 Correct 129 ms 9300 KB Output is correct
4 Correct 74 ms 8796 KB Output is correct
5 Correct 73 ms 8952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 11428 KB Output is correct
2 Correct 41 ms 8804 KB Output is correct
3 Correct 46 ms 8784 KB Output is correct
4 Correct 139 ms 9052 KB Output is correct
5 Correct 139 ms 9044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 206 ms 17748 KB Output is correct
2 Correct 111 ms 13740 KB Output is correct
3 Correct 119 ms 17748 KB Output is correct
4 Correct 107 ms 11420 KB Output is correct
5 Correct 105 ms 11180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 17836 KB Output is correct
2 Correct 49 ms 9052 KB Output is correct
3 Correct 47 ms 9040 KB Output is correct
4 Correct 134 ms 13636 KB Output is correct
5 Correct 141 ms 13632 KB Output is correct