# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
619887 | Olympia | 게임 (IOI13_game) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "game.h"
using namespace std;
struct Node {
Node* left;
Node* right;
int x, y;
int64_t val1, val2;
Node (int64_t x, int64_t val) {
this->x = x, this->y = rand(), this->left = nullptr, this->right = nullptr, this->val1 = this->val2 = val;
}
};
int64_t gcd (int64_t a, int64_t b) {
return a + b;
}
void print (Node*x) {
if (x != nullptr) {
print(x->left);
print(x->right);
}
}
int64_t g (Node*x) {
return ((x == nullptr) ? 0 : x->val2);
}
void upd (Node*&x) {
x->val2 = gcd(gcd(g(x->left), g(x->right)), x->val1);
}
pair<Node*, Node*> splitX (Node*cur, int x) {
if (cur == nullptr) return make_pair(nullptr, nullptr);
if (cur->right == nullptr && cur->x <= x) {
return make_pair(cur, nullptr);
}
if (cur->left == nullptr && cur->x > x) {
return make_pair(nullptr, cur);
}
if (cur->x <= x) {
auto p = splitX(cur->right, x);
cur->right = p.first;
upd(cur);
return make_pair(cur, p.second);
} else {
auto p = splitX(cur->left, x);
cur->left = p.second;
upd(cur);
return make_pair(p.first, cur);
}
}
Node* merge (Node* node1, Node* node2) {
if (node1 == nullptr) return node2;
if (node2 == nullptr) return node1;
if (node2->y <= node1->y) {
node2->left = merge(node1, node2->left);
upd(node2);
return node2;
} else {
node1->right = merge(node1->right, node2);
upd(node1);
return node1;
}
}
int64_t query (Node* root, int l, int r) {
auto p = splitX(root, l - 1); //[<= l - 1, >= l]
auto q = splitX(p.second, r); //[>= l <= r, >r]
int64_t ans = g(q.first);
root = merge(p.first, merge(q.first, q.second));
return ans;
}
void update (Node*& root, int x, int64_t val) {
//update the thing at index x to be val
auto p = splitX(root, x - 1);
auto q = splitX(p.second, x);
root = merge(p.first, merge(new Node(x, val), q.second));
}
map<int,Node*> myMap;
Node*root;
void update (int x, int y, int64_t z) {
Node*cur = myMap[x];
update(cur, y, z);
myMap[x] = cur;
}
int64_t calculate (int x1, int y1, int x2, int y2) {
int64_t ans = 0;
for (int x = x1; x <= x2; x++) {
if (!myMap.count(x)) continue;
ans = gcd(ans, query(myMap[x], y1, y2));
}
return ans;
}
void init (int n, int m) {
}
/*
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
init(n, m);
update(0, 0, 100);
cout << calculate(0, 0, 0, 1) << '\n';
/*
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
//update
int x, y, z;
cin >> x >> y >> z;
Node*cur = myMap[x];
update(cur, y, z);
myMap[x] = cur;
assert(myMap.count(0));
} else {
//query
int x1, x2, y1, y2;
cin >> x1 >> x2 >> y1 >> y2;
int ans = 0;
for (int x = x1; x <= x2; x++) {
if (!myMap.count(x)) continue;
ans = gcd(ans, query(myMap[x], y1, y2));
}
cout << ans << '\n';
}
}
}
*/