Submission #159820

#TimeUsernameProblemLanguageResultExecution timeMemory
159820rama_pangGame (IOI13_game)C++14
63 / 100
4022 ms256004 KiB
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

int R, C;

inline lint gcd(lint X, lint Y) {
    lint tmp;
    while (X != Y && Y != 0) {
        tmp = X;
        X = Y;
        Y = tmp % Y;
    }
    return X;
}

struct SegmentTreeInside {
    struct NodeInside {
        lint val;
        NodeInside *lc, *rc;

        NodeInside(): val(0), lc(NULL), rc(NULL) {}
    };

    NodeInside *root;

    SegmentTreeInside() { root = new NodeInside(); }

    void update(NodeInside *n, int tl, int tr, int p, lint x) {
        if (tl == tr) {
            n->val = x;
            return;
        }

        if (n->lc == NULL) n->lc = new NodeInside();
        if (n->rc == NULL) n->rc = new NodeInside();
        
        lint mid = (tl + tr) / 2ll;
        if (p <= mid) 
            update(n->lc, tl, mid, p, x);
        else
            update(n->rc, mid + 1, tr, p, x);

        n->val = gcd(n->lc->val, n->rc->val);   
    }

    lint query(NodeInside *n, int tl, int tr, int le, int ri) {
        if (tr < le || tl > ri || tl > tr) return 0;
        if (le <= tl && tr <= ri) return n->val;
        
        int mid = (tl + tr) / 2;
        if (n->lc == NULL) n->lc = new NodeInside();
        if (n->rc == NULL) n->rc = new NodeInside();

        lint gcd1 = query(n->lc, tl, mid, le, ri); 
        lint gcd2 = query(n->rc, mid + 1, tr, le, ri);
        return gcd(gcd1, gcd2);
    }

};

struct SegmentTree {
    struct Node {
        SegmentTreeInside seg;
        Node *lc, *rc;

        Node() {
            lc = rc = NULL;
        }
    };

    Node *root;

    SegmentTree() { root = new Node(); }

    void update(Node *n, int tl, int tr, int p, int q, lint x) {
        if (tl == tr) {
            n->seg.update(n->seg.root, 0, C - 1, q, x);
            return;
        }

        if (n->lc == NULL) n->lc = new Node();
        if (n->rc == NULL) n->rc = new Node();
        
        lint mid = (tl + tr) / 2;        
        if (p <= mid) 
            update(n->lc, tl, mid, p, q, x);
        else
            update(n->rc, mid + 1, tr, p, q, x);

        lint gcd1 = n->lc->seg.query(n->lc->seg.root, 0, C - 1, q, q);
        lint gcd2 = n->rc->seg.query(n->rc->seg.root, 0, C - 1, q, q);
        n->seg.update(n->seg.root, 0, C - 1, q, gcd(gcd1, gcd2));
    }

    lint query(Node *n, lint tl, lint tr, lint le, lint ri, lint lo, lint hi) {
        if (tr < le || tl > ri || tl > tr) return 0;
        if (le <= tl && tr <= ri) return n->seg.query(n->seg.root, 0, C - 1, lo, hi);

        lint mid = (tl + tr) / 2;
        if (n->lc == NULL) n->lc = new Node();
        if (n->rc == NULL) n->rc = new Node();

        lint gcd1 = query(n->lc, tl, mid, le, ri, lo, hi);
        lint gcd2 = query(n->rc, mid + 1, tr, le, ri, lo, hi);
        return gcd(gcd1, gcd2);
    }

};

SegmentTree solver;

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

void update(int P, int Q, lint K) {
    solver.update(solver.root, 0, R - 1, P, Q, K);
}

lint calculate(int P, int Q, int U, int V) {
    return solver.query(solver.root, 0, R - 1, P, U, Q, V);
}

Compilation message (stderr)

grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
  int res;
      ^~~
#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...