답안 #1112664

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1112664 2024-11-14T14:17:15 Z steveonalex 게임 (IOI13_game) C++17
0 / 100
2 ms 336 KB
#include <bits/stdc++.h>
#include "game.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll gcd(ll a, ll b){return __gcd(a, b);}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T the_array_itself, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: the_array_itself) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }
 
const int INF = 1e9 + 69;
struct Treap{
    struct Node{
        int val, priority;
        ll g, sum_g; // value of the current node, and value of the entire subtree
        Node* child[2];
        Node(int val): val(val), priority(rngesus(0, MASK(30) - 1)){
            g = sum_g = 0;
            child[0] = child[1] = nullptr;
        }
    };

    Node* root;

    Treap(){
        root = nullptr;
    }

    void merge_children(Node* cur){
        if (cur == nullptr) return;
        ll ans= cur -> g;
        for(int i = 0; i <= 1; ++i) if (cur -> child[i] != nullptr) {
            ans = gcd(ans, cur -> child[i] -> sum_g);
        }
        cur -> sum_g = ans;
    }

    void split(Node* cur, int x, Node*& L, Node*& R){ // split up to a point x
        if (cur == nullptr) {L = R = nullptr;return;}
        if (x < cur -> val){
            split(cur -> child[0], x, L, cur -> child[0]);
            R = cur;
        }
        else{
            split(cur -> child[1], x, cur -> child[1], R);
            L = cur;
        }
        merge_children(L); merge_children(R);
    }       

    void merge(Node*& cur, Node* L, Node* R){
        if (L == nullptr) cur = R;
        else if (R == nullptr) cur = L;
        else{
            if (R -> priority > L -> priority) {
                cur = R;
                merge(cur -> child[0], L, R -> child[0]);
            }
            else{
                cur = L;
                merge(cur -> child[1], L -> child[1], R);
            }
        }
        merge_children(cur);
    }

    void update(int x, ll k){
        Node *L, *mid, *R;
        split(root, x-1, L, mid);
        split(mid, x, mid, R);
        if (mid == nullptr) mid = new Node(x);
        mid -> g = mid -> sum_g = k;
        merge(root, L, mid);
        merge(root, root, R);
    }

    ll get(int u, int v, int l, int r, Node* id){
        if (id == nullptr) return 0;
        if (u <= l && r <= v) return id -> sum_g;
        int mid = id -> val;
        ll ans = 0;
        if (u <= mid && mid <= v) ans = id -> g;
        if (u < mid) ans = gcd(ans, get(u, v, l, mid - 1, id -> child[0]));
        if (v > mid) ans = gcd(ans, get(u, v, mid + 1, r, id -> child[1]));
        return ans;
    }

    ll get(int u, int v){
        return get(u, v, 0, INF, root);
    }

    void clear(Node* cur){
        for(int i = 0; i<= 1; ++i)
        if (cur -> child[i] != nullptr) {
            clear(cur->child[i]);
            delete cur -> child[i];
        }
    }

    void clear(){
        if (root != nullptr){
            clear(root);
            delete root;
        }
    }
};
 
struct Trie2D{
    struct Node{
        int child[2];
        Treap g;
        Node(){g = Treap(); memset(child, -1, sizeof child);}
    };
 
    int l, r;
    vector<Node> a;
 
    Trie2D(){}
    Trie2D(int l, int r): l(l), r(r){
        a.push_back(Node());
    }
 
    void add_child(int &x){
        x = a.size();
        a.push_back(Node());
    }
 
    ll update(int i, int j, ll k, int l, int r, int id){
        if (l == r) {a[id].g.update(j, k); return k;}
        int mid = (l + r) >> 1;
        ll ans = 0;
        if (i <= mid){
            if (a[id].child[0] == -1) add_child(a[id].child[0]);
            ans = update(i, j, k, l, mid, a[id].child[0]);
            ans = gcd(ans, a[a[id].child[1]].g.get(j, j));
        }
        else{
            if (a[id].child[1] == -1) add_child(a[id].child[1]);
            ans = update(i, j, k, mid + 1, r, a[id].child[1]);
            ans = gcd(ans, a[a[id].child[0]].g.get(j, j));
        }
        a[id].g.update(j, ans);
        return ans;
        
    }
 
    void update(int i, int j, ll k){
        update(i, j, k, l, r, 0);
    }
 
    ll get(int u, int v, int x, int y, int l, int r, int id){
        if (u <= l && r <= v) return a[id].g.get(x, y);
        int mid = (l + r) >> 1;
        ll ans = 0;
        if (u <= mid && a[id].child[0] != -1) ans = gcd(ans, get(u, v, x, y, l, mid, a[id].child[0]));
        if (v > mid && a[id].child[1] != -1) ans = gcd(ans, get(u, v, x, y, mid + 1, r, a[id].child[1]));
        return ans;
    }
 
    ll get(int u, int v, int x, int y){return get(u, v, x, y, l, r, 0);}
};
 
Trie2D tri;
void init(int R, int C) {
    tri = Trie2D(0, INF);
}
 
void update(int P, int Q, long long K) {
    tri.update(P, Q, K);
    /* ... */
}
 
long long calculate(int P, int Q, int U, int V) {
    if (P > U) swap(P, U);
    if (Q > V) swap(Q, V);
    return tri.get(P, U, Q, V);
}
 
 
// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
//     clock_t start = clock();
 
//     init(2, 3);
//     update(0, 0, 20);
//     update(0, 2, 15);
//     update(1, 1, 12);
//     cout << calculate(0, 0, 0, 2) << "\n";
 
 
 
 
//     cerr << "Time elapsed: " << clock() - start << " ms!\n";
 
//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -