Submission #2908

# Submission time Handle Problem Language Result Execution time Memory
2908 2013-08-03T09:38:39 Z astein Game (IOI13_game) C++
Compilation error
0 ms 0 KB
#include <cstring>
#include <queue>
#include <cstdio>
#include <cassert>

using namespace std;

struct Node {
  Node *lu, *ld, *ru, *rd;
  Node* parent;

  int sx, sy, ex, ey;
  long long value;

  Node(int x1, int y1, int x2, int y2, long long v) : sx(x1), sy(y1), ex(x2), ey(y2), value(v) { lu = 0, ld = 0, ru = 0, rd = 0; }
};

Node* root;

long long gcd(long long a, long long b) {
  if (a == 0 && b == 0) return 0;
  if (a == 0) return b;
  if (b == 0) return a;
  long long c = a % b;
  while (c) {
    a = b, b = c, c = a % b;
  }
  return b;
}

void update(int x, int y, long long v) {
  Node* next = root;
  while (true) {
    int sx = next->sx, sy = next->sy, ex = next->ex, ey = next->ey;
    if (sx == ex && sy == ey && x == sx && y == sy) {
      //printf("%d, %d - %d, %d : %lld\n", sx, sy, ex, ey, v);
      next->value = v;
      break;
    }

    int mx = (sx + ex) / 2;
    int my = (sy + ey) / 2;

    if (sx <= x && x <= mx) {
      if (sy <= y && y <= my) {
        // lu
        if (!next->lu) { 
          Node* child = new Node(sx, sy, mx, my, 0);
          child->parent = next;
          next->lu = child;
        }
        next = next->lu;
      } else {
        // ru
        if (!next->ru) { 
          Node* child = new Node(sx, my + 1, mx, ey, 0);
          child->parent = next;
          next->ru = child;
        }
        next = next->ru;
      }
    } else {
      if (sy <= y && y <= my) {
        // ld
        if (!next->ld) { 
          Node* child = new Node(mx + 1, sy, ex, my, 0);
          child->parent = next;
          next->ld = child;
        }
        next = next->ld;
      } else {
        // rd
        if (!next->rd) {
          Node* child = new Node(mx + 1, my + 1, ex, ey, 0);
          child->parent = next;
          next->rd = child;
        }
        next = next->rd;
      }
    }
  }

  Node* up;
  while (next != root) {
    next = next->parent;

    long long g = 0;
    if (next->lu) g = gcd(next->lu->value, g);
    if (next->ru) g = gcd(next->ru->value, g);
    if (next->ld) g = gcd(next->ld->value, g);
    if (next->rd) g = gcd(next->rd->value, g);
    next->value = g;
    //printf("%d, %d - %d, %d : %lld\n", next->sx, next->sy, next->ex, next->ey, next->value);
  }
}

long long calculate(int x1, int y1, int x2, int y2) {
  queue<Node*> Q;
  Q.push(root);

  long long g = 0;
  while (!Q.empty()) {
    Node* now = Q.front(); Q.pop();
    if (x1 <= now->sx && y1 <= now->sy && now->ex <= x2 && now->ey <= y2) {
      g = gcd(now->value, g);
      continue;
    }
    if (now->sx > x2) continue;
    if (now->sy > y2) continue;
    if (now->ex < x1) continue;
    if (now->ey < y1) continue;

    if (now->lu) Q.push(now->lu);
    if (now->ld) Q.push(now->ld);
    if (now->ru) Q.push(now->ru);
    if (now->rd) Q.push(now->rd);
  }

  return g;
}

void clear(Node* now) {
  queue<Node*> Q;
  Q.push(now);
  int numNode = 0;
  while (!Q.empty()) {
    now = Q.front(); Q.pop();

		if (now->lu) Q.push(now->lu);
	  if (now->ld) Q.push(now->ld);
	  if (now->ru) Q.push(now->ru);
	  if (now->rd) Q.push(now->rd);
	  now->parent = 0;
	  numNode++;
	  delete now;
  }
}

Compilation message

grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
game.cpp: In function 'void update(int, int, long long int)':
game.cpp:83:9: warning: unused variable 'up' [-Wunused-variable]
/tmp/ccLwkOwI.o: In function `main':
grader.c:(.text.startup+0x52): undefined reference to `init'
grader.c:(.text.startup+0xb0): undefined reference to `calculate'
grader.c:(.text.startup+0x11a): undefined reference to `update'
collect2: ld returned 1 exit status