답안 #256421

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256421 2020-08-02T16:27:15 Z Sorting 게임 (IOI13_game) C++17
0 / 100
4 ms 512 KB
#include "game.h"
#include <bits/stdc++.h>
 
using namespace std;
 
const int k_Inf = 1e9;
 
long long gcd2(long long X, long long Y) {
    long long tmp;
    while (X != Y && Y != 0) {
        tmp = X;
        X = Y;
        Y = tmp % Y;
    }
    return X;
}
 
struct SegmentTree{
    vector<long long> nodes;
    vector<int> left, right;
    vector<pair<int, long long>> updates;
    int sz;
 
    void get_new_node(){
        nodes.push_back(0);
        left.push_back(-1);
        right.push_back(-1);
    }
 
    SegmentTree(){
        get_new_node();
        sz = 0;
    }
 
    int update(int idx, int l, int r, int s, long long new_val){
        if(idx == 0){
            if(sz < 4){
                updates.push_back({s, new_val});
                sz++;
                return idx;
            }
            else if(sz != k_Inf){
                sz = k_Inf;
                for(auto [s, new_val]: updates)
                    update(0, 0, k_Inf, s, new_val);
                updates.clear();
            }
        }
        if(s < l || r < s) return idx;
        if(idx == -1){
            get_new_node();
            idx = nodes.size() - 1;
        }
        if(l == r){
            nodes[idx] = new_val;
            return idx;
        }
 
        int mid = (l + r) >> 1;
        int new_l = update(left[idx], l, mid, s, new_val);
        int new_r = update(right[idx], mid + 1, r, s, new_val);
 
        left[idx] = new_l;
        right[idx] = new_r;
    
        if(left[idx] == -1) nodes[idx] = nodes[right[idx]];
        else if(right[idx] == -1) nodes[idx] = nodes[left[idx]];
        else nodes[idx] = gcd2(nodes[left[idx]], nodes[right[idx]]);
 
        return idx;
    }
 
    long long query(int idx, int l, int r, int sl, int sr){
        if(idx == 0){
            if(!updates.empty()){
                long long ans = 0;
                for(auto [s, new_val]: updates)
                    if(sl <= s && s <= sr)
                        ans = gcd2(new_val, ans); 
                return ans;
            }
        }
        if(idx == -1) return 0;
        if(sl > r || sr < l) return 0;
        if(sl <= l && r <= sr) return nodes[idx];
 
        int mid = (l + r) >> 1;
        long long lvalue = query(left[idx], l, mid, sl, sr);
        long long rvalue = query(right[idx], mid + 1, r, sl, sr);
        return gcd2(lvalue, rvalue);
    }
};
 
struct SegmentTree2D{
    vector<SegmentTree*> nodes;
    vector<int> left, right;
 
    void get_new_node(){
        nodes.push_back(new SegmentTree());
        left.push_back(-1);
        right.push_back(-1);
    }
 
    SegmentTree2D(){
        get_new_node();
    }
 
    int update(int idx, int l, int r, int x, int y, long long new_val){
        if(x < l || r < x) return idx;
        if(idx == -1){
            get_new_node();
            idx = nodes.size() - 1;
        }
        if(l == r){
            nodes[idx]->update(0, 0, k_Inf, y, new_val);
            return idx;
        }
 
        int mid = (l + r) >> 1;
        int new_l = update(left[idx], l, mid, x, y, new_val);
        int new_r = update(right[idx], mid + 1, r, x, y, new_val);
 
        left[idx] = new_l;
        right[idx] = new_r;
    
        long long t = 0;
        if(left[idx] != -1) t = gcd2(t, nodes[left[idx]]->query(0, 0, k_Inf, y, y));
        if(right[idx] != -1)t = gcd2(t, nodes[right[idx]]->query(0, 0, k_Inf, y, y));
        nodes[idx]->update(0, 0, k_Inf, y, t);
        return idx;
    }
 
    long long query(int idx, int l1, int r1, int sl1, int sr1, int sl2, int sr2){
        if(idx == -1) return 0;
        if(sl1 > r1 || sr1 < l1) return 0;
        if(sl1 <= l1 && r1 <= sr1) return nodes[idx]->query(0, 0, k_Inf, sl2, sr2);
 
        int mid = (l1 + r1) >> 1;
        long long lvalue = query(left[idx], l1, mid, sl1, sr1, sl2, sr2);
        long long rvalue = query(right[idx], mid + 1, r1, sl1, sr1, sl2, sr2);
        return gcd2(lvalue, rvalue);
    }
};
 
 
int r, c;
SegmentTree2D st;
 
void init(int _r, int _c) {
    r = _r, c = _c;
}
 
void update(int p, int q, long long k) {
    st.update(0, 0, k_Inf, p, q, k);
}
 
long long calculate(int p, int q, int u, int v) {
    return st.query(0, 0, k_Inf, p, u, q, v);
}

Compilation message

grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
  int res;
      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Incorrect 1 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -