Submission #645624

#TimeUsernameProblemLanguageResultExecution timeMemory
645624abekerGame (IOI13_game)C++17
80 / 100
3462 ms256000 KiB
#include <bits/stdc++.h>
#include "game.h"
using namespace std;

typedef long long ll;

const int offset = 1 << 30;
const int MAX_ROW = 1e6;
const int MAX_COL = 1.5e7;

int row_root;
int root[MAX_ROW], l[MAX_ROW], r[MAX_ROW];
int left_child[MAX_COL], right_child[MAX_COL];
ll num[MAX_COL];

ll gcd(ll a, ll b) {
  return b ? gcd(b, a % b) : a;
}

void create_col(int &node) {
  static int sz;
  if (!node)
    node = ++sz;
}

void create_row(int &node) {
  static int sz;
  if (!node) {
    node = ++sz;
    create_col(root[node]);
  }
}

void init(int R, int C) {
  create_row(row_root);
}

void update_col(int x, int lo, int hi, int lft, int rig, int col, ll val) {
  if (hi - lo == 1) {
    num[x] = lft || rig ? gcd(num[lft], num[rig]) : val;
    return;
  }
  int mid = (lo + hi) / 2;
  if (col < mid) {
    create_col(left_child[x]);
    update_col(left_child[x], lo, mid, left_child[lft], left_child[rig], col, val);
  }
  else {
    create_col(right_child[x]);
    update_col(right_child[x], mid, hi, right_child[lft], right_child[rig], col, val);
  }
  num[x] = gcd(num[left_child[x]], num[right_child[x]]);
}

void update_row(int x, int lo, int hi, int row, int col, ll val) {
  if (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (row < mid) {
      create_row(l[x]);
      update_row(l[x], lo, mid, row, col, val);
    }
    else {
      create_row(r[x]);
      update_row(r[x], mid, hi, row, col, val);
    }
  }
  update_col(root[x], 0, offset, root[l[x]], root[r[x]], col, val);
}

void update(int p, int q, ll k) {
  update_row(row_root, 0, offset, p, q, k);
}

ll query_col(int x, int lo, int hi, int from, int to) {
  if (!x || lo >= to || hi <= from)
    return 0;
  if (lo >= from && hi <= to)
    return num[x];
  int mid = (lo + hi) / 2;
  return gcd(query_col(left_child[x], lo, mid, from, to), query_col(right_child[x], mid, hi, from, to));
}

ll query_row(int x, int lo, int hi, int from, int to, int col1, int col2) {
  if (!x || lo >= to || hi <= from)
    return 0;
  if (lo >= from && hi <= to)
    return query_col(root[x], 0, offset, col1, col2);
  int mid = (lo + hi) / 2;
  return gcd(query_row(l[x], lo, mid, from, to, col1, col2), query_row(r[x], mid, hi, from, to, col1, col2));
}

ll calculate(int p, int q, int u, int v) {
  return query_row(row_root, 0, offset, p, u + 1, q, v + 1);
}
#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...