답안 #926416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
926416 2024-02-12T23:15:08 Z myst6 게임 (IOI13_game) C++14
100 / 100
9257 ms 172300 KB
#include <bits/stdc++.h>
#include "game.h"

using namespace std;
using ll = long long;

long long gcd2(long long X, long long Y);

const int N = 20'000'000;
const int K = 2;
int lptr[N], rptr[N]; ll val[N];
int nextfree = 1;

int R, C;

void update1(int idx, int v, int xl, int xr, ll delta) {
  if (v < xl || v > xr) return;
  if (xl == xr) {
    val[idx] = delta;
  } else {
    int xm = (xl + xr) / 2;
    if (v <= xm) {
      if (!lptr[idx]) lptr[idx] = nextfree++;
      update1(lptr[idx], v, xl, xm, delta);
    } else {
      if (!rptr[idx]) rptr[idx] = nextfree++;
      update1(rptr[idx], v, xm+1, xr, delta);
    }
    val[idx] = 0;
    if (lptr[idx]) val[idx] = gcd2(val[idx], val[lptr[idx]]);
    if (rptr[idx]) val[idx] = gcd2(val[idx], val[rptr[idx]]);
  }
}

ll query1(int idx, int l, int r, int xl, int xr) {
  if (l > r) return 0;
  if (l == xl && r == xr) {
    return val[idx];
  } else {
    int xm = (xl + xr) / 2;
    ll ans = 0;
    if (lptr[idx]) ans = gcd2(ans, query1(lptr[idx], l, min(r, xm), xl, xm));
    if (rptr[idx]) ans = gcd2(ans, query1(rptr[idx], max(l, xm+1), r, xm+1, xr));
    return ans;
  }
}

ll query2(int idx, int l, int r, int l2, int r2, int xl, int xr) {
  if (l > r) return 0;
  if (l == xl && r == xr && val[idx] != -1) {
    if (!val[idx]) return 0;
    return query1(val[idx], l2, r2, 1, C);
  } else {
    int xm = (xl + xr) / 2;
    ll ans = 0;
    if (lptr[idx]) ans = gcd2(ans, query2(lptr[idx], l, min(r, xm), l2, r2, xl, xm));
    if (rptr[idx]) ans = gcd2(ans, query2(rptr[idx], max(l, xm+1), r, l2, r2, xm+1, xr));
    return ans;
  }
}

void update2(int idx, int v, int w, int xl, int xr, ll delta) {
  if (v < xl || v > xr) return;
  if (xl != xr) {
    int xm = (xl + xr) / 2;
    if (v <= xm) {
      if (!lptr[idx]) lptr[idx] = nextfree++;
      update2(lptr[idx], v, w, xl, xm, delta);
    } else {
      if (!rptr[idx]) rptr[idx] = nextfree++;
      update2(rptr[idx], v, w, xm+1, xr, delta);
    }
  }
  if (!val[idx]) {
    // sacrifice runtime for memory
    if (xl != xr && rand() % K != 0) val[idx] = -1;
    else val[idx] = nextfree++;
  }
  if (val[idx] != -1) {
    ll delta2 = delta;
    int xm = (xl + xr) / 2;
    if (lptr[idx]) delta2 = gcd2(delta2, query2(lptr[idx], xl, xm, w, w, xl, xm));
    if (rptr[idx]) delta2 = gcd2(delta2, query2(rptr[idx], xm+1, xr, w, w, xm+1, xr));
    if (!val[idx]) val[idx] = nextfree++;
    update1(val[idx], w, 1, C, delta2);
  }
}

long long gcd2(long long X, long long Y) {
  long long tmp;
  while (X != Y && Y != 0) {
    tmp = X;
    X = Y;
    Y = tmp % Y;
  }
  return X;
}

void init(int _R, int _C) {
  R = _R;
  C = _C;
}

void update(int P, int Q, long long K) {
  update2(0, P+1, Q+1, 1, R, K);
}

long long calculate(int P, int Q, int U, int V) {
  return query2(0, P+1, U+1, Q+1, V+1, 1, R);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 372 ms 7976 KB Output is correct
5 Correct 249 ms 7884 KB Output is correct
6 Correct 373 ms 5200 KB Output is correct
7 Correct 411 ms 4472 KB Output is correct
8 Correct 263 ms 5308 KB Output is correct
9 Correct 369 ms 4436 KB Output is correct
10 Correct 346 ms 4544 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1144 ms 8832 KB Output is correct
13 Correct 1858 ms 4636 KB Output is correct
14 Correct 202 ms 848 KB Output is correct
15 Correct 2064 ms 4556 KB Output is correct
16 Correct 148 ms 7856 KB Output is correct
17 Correct 949 ms 5580 KB Output is correct
18 Correct 1300 ms 8276 KB Output is correct
19 Correct 1045 ms 8532 KB Output is correct
20 Correct 1016 ms 8044 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 444 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 366 ms 7684 KB Output is correct
13 Correct 249 ms 8016 KB Output is correct
14 Correct 373 ms 5112 KB Output is correct
15 Correct 408 ms 4516 KB Output is correct
16 Correct 262 ms 5052 KB Output is correct
17 Correct 375 ms 4432 KB Output is correct
18 Correct 341 ms 4260 KB Output is correct
19 Correct 1145 ms 8492 KB Output is correct
20 Correct 1872 ms 3976 KB Output is correct
21 Correct 201 ms 840 KB Output is correct
22 Correct 2068 ms 4924 KB Output is correct
23 Correct 147 ms 7972 KB Output is correct
24 Correct 948 ms 5400 KB Output is correct
25 Correct 1324 ms 8276 KB Output is correct
26 Correct 1052 ms 8272 KB Output is correct
27 Correct 1014 ms 8012 KB Output is correct
28 Correct 665 ms 66252 KB Output is correct
29 Correct 1382 ms 80232 KB Output is correct
30 Correct 7211 ms 53884 KB Output is correct
31 Correct 3230 ms 42048 KB Output is correct
32 Correct 347 ms 1360 KB Output is correct
33 Correct 476 ms 3460 KB Output is correct
34 Correct 278 ms 73992 KB Output is correct
35 Correct 1118 ms 37968 KB Output is correct
36 Correct 2195 ms 74004 KB Output is correct
37 Correct 1904 ms 74576 KB Output is correct
38 Correct 1924 ms 74380 KB Output is correct
39 Correct 1429 ms 57488 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 380 ms 7708 KB Output is correct
13 Correct 252 ms 8020 KB Output is correct
14 Correct 373 ms 4672 KB Output is correct
15 Correct 406 ms 4432 KB Output is correct
16 Correct 259 ms 4644 KB Output is correct
17 Correct 369 ms 4616 KB Output is correct
18 Correct 348 ms 4196 KB Output is correct
19 Correct 1169 ms 8528 KB Output is correct
20 Correct 1875 ms 4160 KB Output is correct
21 Correct 207 ms 852 KB Output is correct
22 Correct 2070 ms 4420 KB Output is correct
23 Correct 147 ms 7896 KB Output is correct
24 Correct 957 ms 5556 KB Output is correct
25 Correct 1309 ms 8260 KB Output is correct
26 Correct 1061 ms 8608 KB Output is correct
27 Correct 1012 ms 7832 KB Output is correct
28 Correct 678 ms 66132 KB Output is correct
29 Correct 1353 ms 80464 KB Output is correct
30 Correct 7302 ms 54348 KB Output is correct
31 Correct 3247 ms 41660 KB Output is correct
32 Correct 354 ms 1104 KB Output is correct
33 Correct 472 ms 3408 KB Output is correct
34 Correct 274 ms 74320 KB Output is correct
35 Correct 1128 ms 37720 KB Output is correct
36 Correct 2201 ms 73936 KB Output is correct
37 Correct 1912 ms 74596 KB Output is correct
38 Correct 1912 ms 74320 KB Output is correct
39 Correct 813 ms 150324 KB Output is correct
40 Correct 2348 ms 172300 KB Output is correct
41 Correct 9257 ms 118356 KB Output is correct
42 Correct 5011 ms 92788 KB Output is correct
43 Correct 527 ms 163648 KB Output is correct
44 Correct 475 ms 10660 KB Output is correct
45 Correct 1423 ms 67644 KB Output is correct
46 Correct 3072 ms 168216 KB Output is correct
47 Correct 3141 ms 170816 KB Output is correct
48 Correct 3402 ms 171364 KB Output is correct
49 Correct 0 ms 600 KB Output is correct