Submission #948187

#TimeUsernameProblemLanguageResultExecution timeMemory
948187rainboyWalk (POI13_spa)C11
100 / 100
268 ms17956 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...