Submission #689619

# Submission time Handle Problem Language Result Execution time Memory
689619 2023-01-29T00:01:44 Z Mamedov Game (IOI13_game) C++17
100 / 100
4058 ms 64204 KB
#pragma GCC optimize ("O3")
#include "game.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXR = 1e9;
const int MAXC = 1e9;
const int MAX = 5e6;

ll gcd(ll a, ll b) {
  if (!b) return a;
  else return gcd(b, a % b);
}

ll tree[MAX];
int L[MAX], R[MAX], root[MAX], lazyPos[MAX];
int nxtFree = 1;

/// Here lazyPos is used to optimize memory.
/// When there is only one updated position in the range, we stop at that node and don't create new nodes down

void relax(int v, int l, int r) {
  int mid = (l + r) >> 1;
  if (lazyPos[v] <= mid) {
    L[v] = ++nxtFree;
    tree[L[v]] = tree[v];
    lazyPos[L[v]] = lazyPos[v];
  } else {
    R[v] = ++nxtFree;
    tree[R[v]] = tree[v];
    lazyPos[R[v]] = lazyPos[v];
  }
  lazyPos[v] = 0;
}

void update1D(int v, int l, int r, int pos, ll val) {
  if (l == r) {
    tree[v] = val;
  } else {
    if (lazyPos[v]) {
      if (lazyPos[v] == pos) {
        tree[v] = val;
      } else {
        relax(v, l, r);
      }
    }
    int mid = (l + r) >> 1;
    if (pos <= mid) {
      if (!L[v])  {
        L[v] = ++nxtFree;
        tree[L[v]] = val;
        lazyPos[L[v]] = pos;
      } else {
        update1D(L[v], l, mid, pos, val);
      }
    } else {
      if (!R[v]) {
        R[v] = ++nxtFree;
        tree[R[v]] = val;
        lazyPos[R[v]] = pos;
      } else {
        update1D(R[v], mid + 1, r, pos, val);
      }
    }
    ll gcdL = 0, gcdR = 0;
    if (L[v]) gcdL = tree[L[v]];
    if (R[v]) gcdR = tree[R[v]];
    tree[v] = gcd(gcdL, gcdR);
  }
}

ll query1D(int v, int l, int r, int ql, int qr) {
  if (ql > r || l > qr) return 0;
  else if (ql <= l && r <= qr) return tree[v];
  else if (lazyPos[v]) return (ql <= lazyPos[v] && lazyPos[v] <= qr) ? tree[v] : 0;
  else {
    int mid = (l + r) >> 1;
    ll gcdL = 0, gcdR = 0;
    if (L[v]) gcdL = query1D(L[v], l, mid, ql, qr);
    if (R[v]) gcdR = query1D(R[v], mid + 1, r, ql, qr);
    return gcd(gcdL, gcdR);
  }
}

void update2D(int v, int l, int r, int posR, int posC, ll val) {
  if (l == r) {
    if (!root[v]) root[v] = ++nxtFree;
    update1D(root[v], 1, MAXC, posC, val);
  } else {
    int mid = (l + r) >> 1;
    if (posR <= mid) {
      if (!L[v]) L[v] = ++nxtFree;
      update2D(L[v], l, mid, posR, posC, val);
    } else {
      if (!R[v]) R[v] = ++nxtFree;
      update2D(R[v], mid + 1, r, posR, posC, val);
    }
    ll gcdL = 0, gcdR = 0;
    if (L[v]) gcdL = query1D(root[L[v]], 1, MAXC, posC, posC);
    if (R[v]) gcdR = query1D(root[R[v]], 1, MAXC, posC, posC);
    ll combinedVal = gcd(gcdL, gcdR);
    if (!root[v]) root[v] = ++nxtFree;
    update1D(root[v], 1, MAXC, posC, combinedVal);
  }
}

ll query2D(int v, int l, int r, int rowL, int rowR, int colL, int colR) {
  if (rowL > r || l > rowR) return 0;
  else if (rowL <= l && r <= rowR) {
    if (root[v]) return query1D(root[v], 1, MAXC, colL, colR);
    else return 0;
  } else {
    int mid = (l + r) >> 1;
    ll gcdL = 0, gcdR = 0;
    if (L[v]) gcdL = query2D(L[v], l, mid, rowL, rowR, colL, colR);
    if (R[v]) gcdR = query2D(R[v], mid + 1, r, rowL, rowR, colL, colR);
    return gcd(gcdL, gcdR);
  }
}

void init(int R, int C) {

}

void update(int P, int Q, ll K) {
  ++P;
  ++Q;
  update2D(1, 1, MAXR, P, Q, K);
}

ll calculate(int P, int Q, int U, int V) {
  ++P;
  ++Q;
  ++U;
  ++V;
  return query2D(1, 1, MAXR, P, U, Q, V);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 436 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 691 ms 18516 KB Output is correct
5 Correct 548 ms 18872 KB Output is correct
6 Correct 674 ms 15836 KB Output is correct
7 Correct 775 ms 15332 KB Output is correct
8 Correct 451 ms 9304 KB Output is correct
9 Correct 747 ms 15228 KB Output is correct
10 Correct 688 ms 15016 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 444 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1096 ms 10804 KB Output is correct
13 Correct 1510 ms 6324 KB Output is correct
14 Correct 500 ms 1620 KB Output is correct
15 Correct 1751 ms 8612 KB Output is correct
16 Correct 509 ms 10520 KB Output is correct
17 Correct 933 ms 8268 KB Output is correct
18 Correct 1495 ms 11032 KB Output is correct
19 Correct 1299 ms 10992 KB Output is correct
20 Correct 1253 ms 10440 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 2 ms 536 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 444 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 660 ms 18512 KB Output is correct
13 Correct 550 ms 18892 KB Output is correct
14 Correct 670 ms 15740 KB Output is correct
15 Correct 745 ms 15476 KB Output is correct
16 Correct 487 ms 9352 KB Output is correct
17 Correct 682 ms 15272 KB Output is correct
18 Correct 698 ms 15184 KB Output is correct
19 Correct 1093 ms 10852 KB Output is correct
20 Correct 1501 ms 6348 KB Output is correct
21 Correct 505 ms 1540 KB Output is correct
22 Correct 1717 ms 8596 KB Output is correct
23 Correct 486 ms 10632 KB Output is correct
24 Correct 870 ms 8244 KB Output is correct
25 Correct 1417 ms 10908 KB Output is correct
26 Correct 1356 ms 11168 KB Output is correct
27 Correct 1241 ms 10316 KB Output is correct
28 Correct 438 ms 25144 KB Output is correct
29 Correct 1087 ms 24268 KB Output is correct
30 Correct 3225 ms 22048 KB Output is correct
31 Correct 2985 ms 18652 KB Output is correct
32 Correct 419 ms 1508 KB Output is correct
33 Correct 572 ms 3448 KB Output is correct
34 Correct 209 ms 21244 KB Output is correct
35 Correct 700 ms 11780 KB Output is correct
36 Correct 1339 ms 21548 KB Output is correct
37 Correct 1150 ms 21788 KB Output is correct
38 Correct 1002 ms 21100 KB Output is correct
39 Correct 864 ms 16896 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 715 ms 18260 KB Output is correct
13 Correct 549 ms 18592 KB Output is correct
14 Correct 696 ms 15296 KB Output is correct
15 Correct 737 ms 15124 KB Output is correct
16 Correct 453 ms 9008 KB Output is correct
17 Correct 691 ms 14992 KB Output is correct
18 Correct 670 ms 14920 KB Output is correct
19 Correct 1093 ms 10620 KB Output is correct
20 Correct 1551 ms 6084 KB Output is correct
21 Correct 533 ms 1448 KB Output is correct
22 Correct 1763 ms 8364 KB Output is correct
23 Correct 494 ms 10356 KB Output is correct
24 Correct 900 ms 8012 KB Output is correct
25 Correct 1501 ms 10724 KB Output is correct
26 Correct 1373 ms 10660 KB Output is correct
27 Correct 1222 ms 10236 KB Output is correct
28 Correct 432 ms 25024 KB Output is correct
29 Correct 1199 ms 24240 KB Output is correct
30 Correct 3246 ms 21912 KB Output is correct
31 Correct 2943 ms 18592 KB Output is correct
32 Correct 429 ms 1484 KB Output is correct
33 Correct 589 ms 3360 KB Output is correct
34 Correct 204 ms 21056 KB Output is correct
35 Correct 704 ms 11656 KB Output is correct
36 Correct 1424 ms 21476 KB Output is correct
37 Correct 1076 ms 21512 KB Output is correct
38 Correct 1121 ms 20924 KB Output is correct
39 Correct 588 ms 64204 KB Output is correct
40 Correct 1767 ms 56964 KB Output is correct
41 Correct 4058 ms 53304 KB Output is correct
42 Correct 3792 ms 45332 KB Output is correct
43 Correct 370 ms 51660 KB Output is correct
44 Correct 613 ms 10752 KB Output is correct
45 Correct 915 ms 26908 KB Output is correct
46 Correct 1846 ms 55820 KB Output is correct
47 Correct 1921 ms 55792 KB Output is correct
48 Correct 1787 ms 55392 KB Output is correct
49 Correct 1 ms 340 KB Output is correct