답안 #516582

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
516582 2022-01-21T13:56:00 Z 600Mihnea 게임 (IOI13_game) C++17
80 / 100
13000 ms 77200 KB
#include "game.h"
#include <bits/stdc++.h>

using namespace std;


typedef long long ll;

int dimx;
int dimy;

struct NodeX {
  NodeX* lft;
  NodeX* rgh;
  int tree_x1;
  int tree_x2;
  ll gcd;
  NodeX(int tree_x1, int tree_x2) : lft(nullptr), rgh(nullptr), gcd(0), tree_x1(tree_x1), tree_x2(tree_x2) {}
};

struct NodeY {
  NodeY* lft;
  NodeY* rgh;
  NodeX* xRoot;
  NodeY() : lft(nullptr), rgh(nullptr), xRoot(new NodeX(1, dimx)) {}
};

ll queryX(NodeX* node, int x1, int x2) {
  if (node->tree_x2 < x1 || x2 < node->tree_x1) return 0;
  if (x1 <= node->tree_x1 && node->tree_x2 <= x2) return node->gcd;

  ll gcd_lft = 0, gcd_rgh = 0;

  int tree_mid = (node->tree_x1 + node->tree_x2) / 2;
  if (x1 <= tree_mid && node->lft) gcd_lft = queryX(node->lft, x1, x2);
  if (x2 >= tree_mid + 1 && node->rgh) gcd_rgh = queryX(node->rgh, x1, x2);

  return __gcd(gcd_lft, gcd_rgh);
}

ll queryY(NodeY* node, int tree_y1, int tree_y2, int y1, int y2, int x1, int x2) {
  if (y1 <= tree_y1 && tree_y2 <= y2) return queryX(node->xRoot, x1, x2);

  ll gcd_lft = 0;
  ll gcd_rgh = 0;

  int tree_mid = (tree_y1 + tree_y2) / 2;
  if (y1 <= tree_mid && node->lft) gcd_lft = queryY(node->lft, tree_y1, tree_mid, y1, y2, x1, x2);
  if (tree_mid + 1 <= y2 && node->rgh) gcd_rgh = queryY(node->rgh, tree_mid + 1, tree_y2, y1, y2, x1, x2);

  return __gcd(gcd_lft, gcd_rgh);
}

void setValueX(NodeX* node, int x, ll val) {

  if (node->tree_x1 == node->tree_x2) {
    node->gcd = val;
    return;
  }

  int tree_mid = (node->tree_x1 + node->tree_x2) / 2;

  if (x <= tree_mid) {
    if (!node->lft) {
      node->lft = new NodeX(x, x);
      node->lft->gcd = val;
    } else {
      if (node->lft->tree_x1 <= x && x <= node->lft->tree_x2) {
        setValueX(node->lft, x, val);
      } else {
        NodeX* child = node->lft;
        /// find the LCA
        int low = node->tree_x1, high = node->tree_x2;
        while (low < high) {
          int mid = (low + high) / 2, L, R;
          L = low;
          R = mid;
          if (L <= x && x <= R && L <= child->tree_x1 && child->tree_x2 <= R) {
            low = L;
            high = R;
            continue;
          }
          L = mid + 1;
          R = high;
          if (L <= x && x <= R && L <= child->tree_x1 && child->tree_x2 <= R) {
            low = L;
            high = R;
            continue;
          }
          break;
        }
        int L = low, R = high;
        NodeX* nw = new NodeX(L, R);
        if (x < child->tree_x1) {
          ///cout << "to left\n";
          nw->lft = new NodeX(x, x);
          nw->lft->gcd = val;
          nw->rgh = child;
        } else {
          ///cout << "to right\n";
          nw->lft = child;
          nw->rgh = new NodeX(x, x);
          nw->rgh->gcd = val;
        }
        nw->gcd = __gcd(nw->lft->gcd, nw->rgh->gcd);
        node->lft = nw;


        /**cout << L << " " << R << "\n";
        cout << node->lft->tree_x1 << " " << node->lft->tree_x2 << " " << x << "\n";
        cout << "bad 1\n";
        exit(0);**/
      }
    }
  } else {
    if (!node->rgh) {
      node->rgh = new NodeX(x, x);
      node->rgh->gcd = val;
    } else {
      if (node->rgh->tree_x1 <= x && x <= node->rgh->tree_x2) {
        setValueX(node->rgh, x, val);
      } else {
        NodeX* child = node->rgh;
        /// find the LCA
        int low = node->tree_x1, high = node->tree_x2;

        while (low < high) {
          int mid = (low + high) / 2, L, R;
          L = low;
          R = mid;
          if (L <= x && x <= R && L <= child->tree_x1 && child->tree_x2 <= R) {
            low = L;
            high = R;
            continue;
          }
          L = mid + 1;
          R = high;
          if (L <= x && x <= R && L <= child->tree_x1 && child->tree_x2 <= R) {
            low = L;
            high = R;
            continue;
          }
          break;
        }
        int L = low, R = high;
        NodeX* nw = new NodeX(L, R);
        if (x < child->tree_x1) {
          ///cout << "to left\n";
          nw->lft = new NodeX(x, x);
          nw->lft->gcd = val;
          nw->rgh = child;
        } else {
          ///cout << "to right\n";
          nw->lft = child;
          nw->rgh = new NodeX(x, x);
          nw->rgh->gcd = val;
        }
        nw->gcd = __gcd(nw->lft->gcd, nw->rgh->gcd);
        node->rgh = nw;
      }
    }
    setValueX(node->rgh, x, val);
  }

  ll gcd_lft = 0;
  ll gcd_rgh = 0;

  if (node->lft) gcd_lft = node->lft->gcd;
  if (node->rgh) gcd_rgh = node->rgh->gcd;

  node->gcd = __gcd(gcd_lft, gcd_rgh);
}

void setValueY(NodeY* node, int tree_y1, int tree_y2, int y, int x, ll val) {
  if (tree_y1 == tree_y2) {
    setValueX(node->xRoot, x, val);
    return;
  }

  int tree_mid = (tree_y1 + tree_y2) / 2;
  if (y <= tree_mid) {
    if (!node->lft) node->lft = new NodeY;
    setValueY(node->lft, tree_y1, tree_mid, y, x, val);
  } else {
    if (!node->rgh) node->rgh = new NodeY;
    setValueY(node->rgh, tree_mid + 1, tree_y2, y, x, val);
  }

  ll gcd_lft = 0;
  ll gcd_rgh = 0;


  if (node->lft) gcd_lft = queryX(node->lft->xRoot, x, x);

  if (node->rgh) gcd_rgh = queryX(node->rgh->xRoot, x, x);




  setValueX(node->xRoot, x, __gcd(gcd_lft, gcd_rgh));
}

NodeY* rootY;

void init(int R, int C) {
  dimx = R;
  dimy = C;

  rootY = new NodeY;
}

void update(int P, int Q, long long K) {
  int x = P;
  int y = Q;
  ll k = K;
  x++;
  y++;
  setValueY(rootY, 1, dimy, y, x, k);
}

long long calculate(int P, int Q, int U, int V) {
  int x1 = P;
  int y1 = Q;
  int x2 = U;
  int y2 = V;
  x1++;
  y1++;
  x2++;
  y2++;

  ll sol = queryY(rootY, 1, dimy, y1, y2, x1, x2);

  return sol;
}

Compilation message

game.cpp: In constructor 'NodeX::NodeX(int, int)':
game.cpp:17:6: warning: 'NodeX::gcd' will be initialized after [-Wreorder]
   17 |   ll gcd;
      |      ^~~
game.cpp:15:7: warning:   'int NodeX::tree_x1' [-Wreorder]
   15 |   int tree_x1;
      |       ^~~~~~~
game.cpp:18:3: warning:   when initialized here [-Wreorder]
   18 |   NodeX(int tree_x1, int tree_x2) : lft(nullptr), rgh(nullptr), gcd(0), tree_x1(tree_x1), tree_x2(tree_x2) {}
      |   ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 470 ms 12268 KB Output is correct
5 Correct 370 ms 12580 KB Output is correct
6 Correct 482 ms 9632 KB Output is correct
7 Correct 665 ms 9420 KB Output is correct
8 Correct 309 ms 6452 KB Output is correct
9 Correct 535 ms 9468 KB Output is correct
10 Correct 551 ms 9012 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 761 ms 11424 KB Output is correct
13 Correct 1413 ms 4904 KB Output is correct
14 Correct 209 ms 892 KB Output is correct
15 Correct 1665 ms 6132 KB Output is correct
16 Correct 348 ms 9860 KB Output is correct
17 Correct 589 ms 6216 KB Output is correct
18 Correct 1186 ms 10232 KB Output is correct
19 Correct 967 ms 10380 KB Output is correct
20 Correct 932 ms 9748 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 436 ms 12212 KB Output is correct
13 Correct 383 ms 12588 KB Output is correct
14 Correct 471 ms 9588 KB Output is correct
15 Correct 538 ms 9380 KB Output is correct
16 Correct 334 ms 6528 KB Output is correct
17 Correct 569 ms 9484 KB Output is correct
18 Correct 459 ms 9072 KB Output is correct
19 Correct 868 ms 11512 KB Output is correct
20 Correct 1385 ms 5024 KB Output is correct
21 Correct 212 ms 888 KB Output is correct
22 Correct 1634 ms 6128 KB Output is correct
23 Correct 350 ms 9944 KB Output is correct
24 Correct 680 ms 6280 KB Output is correct
25 Correct 1233 ms 10176 KB Output is correct
26 Correct 1073 ms 10328 KB Output is correct
27 Correct 972 ms 9784 KB Output is correct
28 Correct 672 ms 35436 KB Output is correct
29 Correct 1270 ms 38152 KB Output is correct
30 Correct 7679 ms 29644 KB Output is correct
31 Correct 6839 ms 22716 KB Output is correct
32 Correct 278 ms 1012 KB Output is correct
33 Correct 422 ms 1544 KB Output is correct
34 Correct 714 ms 35188 KB Output is correct
35 Correct 905 ms 18676 KB Output is correct
36 Correct 1857 ms 35528 KB Output is correct
37 Correct 1448 ms 35664 KB Output is correct
38 Correct 1763 ms 35028 KB Output is correct
39 Correct 1144 ms 27544 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 220 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 387 ms 12268 KB Output is correct
13 Correct 406 ms 12556 KB Output is correct
14 Correct 487 ms 9684 KB Output is correct
15 Correct 528 ms 9436 KB Output is correct
16 Correct 311 ms 6404 KB Output is correct
17 Correct 597 ms 9524 KB Output is correct
18 Correct 489 ms 9064 KB Output is correct
19 Correct 731 ms 11500 KB Output is correct
20 Correct 1443 ms 4860 KB Output is correct
21 Correct 214 ms 836 KB Output is correct
22 Correct 1696 ms 6088 KB Output is correct
23 Correct 358 ms 9976 KB Output is correct
24 Correct 622 ms 6140 KB Output is correct
25 Correct 1181 ms 10268 KB Output is correct
26 Correct 1054 ms 10388 KB Output is correct
27 Correct 1037 ms 9784 KB Output is correct
28 Correct 695 ms 35396 KB Output is correct
29 Correct 1264 ms 38012 KB Output is correct
30 Correct 7667 ms 29728 KB Output is correct
31 Correct 6969 ms 22728 KB Output is correct
32 Correct 314 ms 960 KB Output is correct
33 Correct 407 ms 1640 KB Output is correct
34 Correct 709 ms 35056 KB Output is correct
35 Correct 881 ms 18544 KB Output is correct
36 Correct 1956 ms 35420 KB Output is correct
37 Correct 1537 ms 35504 KB Output is correct
38 Correct 1885 ms 34972 KB Output is correct
39 Correct 1316 ms 76160 KB Output is correct
40 Correct 2048 ms 77200 KB Output is correct
41 Execution timed out 13029 ms 56272 KB Time limit exceeded
42 Halted 0 ms 0 KB -