답안 #282070

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
282070 2020-08-23T23:18:08 Z rqi 게임 (IOI13_game) C++14
100 / 100
3929 ms 93804 KB
#include "game.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define bk back()
#define pb push_back

const ld PI = acos((ld)-1);
int MAXX = 1000000000;
int MAXY = 1000000000;

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;
}

pi getRang(pi a, int b, int L = 0, int R = MAXY-1){
    if(L > a.f || a.s > R || L > b || b > R) return mp(-1, -1);
    int M = (L+R)/2;
    pi res1 = getRang(a, b, L, M);
    if(res1 != mp(-1, -1)) return res1;
    pi res2 = getRang(a, b, M+1, R);
    if(res2 != mp(-1, -1)) return res2;
    return mp(L, R);
}

struct Node{
    int L, R, M;
    ll val;
    Node* c[2];

    Node(int _L, int _R, ll _val = 0){
        L = _L;
        R = _R;
        M = (L+R)/2;
        val = _val;
    }

    void pull(){
        val = 0;
        if(c[0]) val = gcd2(val, c[0]->val);
        if(c[1]) val = gcd2(val, c[1]->val);
    }

    void upd(int pos, ll inc){ 
        assert(L <= pos && pos <= R);
        if(L == R){
            val = inc;
            return;
        }
        
        if(pos <= M){
            if(!c[0]){
                c[0] = new Node(pos, pos, inc);
            }
            else{
                if(c[0]->L <= pos && pos <= c[0]->R){
                    c[0]->upd(pos, inc);
                }
                else{
                    pi rang = getRang(mp(c[0]->L, c[0]->R), pos);
                    if(c[0]->R < pos){
                        Node* par = new Node(rang.f, rang.s);
                        par->c[0] = c[0];
                        par->c[1] = new Node(pos, pos, inc);
                        c[0] = par;
                        par->pull();
                    }
                    else{
                        Node* par = new Node(rang.f, rang.s);
                        par->c[1] = c[0];
                        par->c[0] = new Node(pos, pos, inc);
                        c[0] = par;
                        par->pull();
                    }
                }
            }
        }
        else{
            if(!c[1]){
                c[1] = new Node(pos, pos, inc);
            }
            else{
                if(c[1]->L <= pos && pos <= c[1]->R){
                    c[1]->upd(pos, inc);
                }
                else{
                    pi rang = getRang(mp(c[1]->L, c[1]->R), pos);
                    if(c[1]->R < pos){
                        Node* par = new Node(rang.f, rang.s);
                        par->c[0] = c[1];
                        par->c[1] = new Node(pos, pos, inc);
                        c[1] = par;
                        par->pull();
                    }
                    else{
                        Node* par = new Node(rang.f, rang.s);
                        par->c[1] = c[1];
                        par->c[0] = new Node(pos, pos, inc);
                        c[1] = par;
                        par->pull();
                    }
                }
            }
        }
        //cout << "L, R, val, pos, inc: " << L << " " << R << " " << val << " " << pos << " " << inc << "\n";
        // if(c[0]){
        //     cout << "c[0]->L, R, val: " << c[0]->L << " " << c[0]->R << " " << c[0]->val << "\n";
        // }
        // if(c[1]){
        //     cout << "c[1]->L, R, val: " << c[1]->L << " " << c[1]->R << " " << c[1]->val << "\n";
        // }
        pull();
        //cout << "L, R, val, pos, inc: " << L << " " << R << " " << val << " " << pos << " " << inc << "\n";
    }

    ll query(int lo, int hi){
        if(R < lo || hi < L) return 0;
        if(lo <= L && R <= hi){
            return val;
        }
        ll res = 0;
        if(c[0]){
            res = gcd2(res, c[0]->query(lo, hi));
        }
        if(c[1]){
            res = gcd2(res, c[1]->query(lo, hi));
        }
        return res;
    }
};

struct Sparse{
    int L, R, M;
    Node* seg;
    Sparse* c[2];

    Sparse(int _L, int _R){
        L = _L;
        R = _R;
        M = (L+R)/2;
        seg = new Node(0, MAXY-1, 0);
    }



    void upd(int x, int y, ll inc){
        if(x < L || R < x) return;
        //cout << "L, R, x, y, inc: " << L << " " << R << " " << x << " " << y << " " << inc << "\n";
        //cout << "seg->query(0, 1): " << seg->query(0, 1) << "\n";
        //seg->upd(y, inc);
        //cout << "seg->query(0, 1): " << seg->query(0, 1) << "\n";
        if(L == R){
            seg->upd(y, inc);
            return;
        }
        if(x <= M){
            if(!c[0]) c[0] = new Sparse(L, M);
            c[0]->upd(x, y, inc);
        }
        else{
            if(!c[1]) c[1] = new Sparse(M+1, R);
            c[1]->upd(x, y, inc);
        }
        ll val = 0;
        if(c[0]) val = gcd2(val, c[0]->seg->query(y, y));
        if(c[1]) val = gcd2(val, c[1]->seg->query(y, y));
        seg->upd(y, val);
    }

    ll query(int lox, int hix, int loy, int hiy){
        if(R < lox || hix < L) return 0;
        if(lox <= L && R <= hix){
            return seg->query(loy, hiy);
        }
        ll res = 0;
        if(c[0]){
            res = gcd2(res, c[0]->query(lox, hix, loy, hiy));
        }
        if(c[1]){
            res = gcd2(res, c[1]->query(lox, hix, loy, hiy));
        }
        return res;
    }
    
};

// bool check(pi a){
//     if(a.f < 0 || a.f > MAXX) return 0;
//     if(a.s < 0 || a.s > MAXY) return 0;
//     return 1;
// }

Sparse* seg;

void init(int R, int C) {
    MAXX = R;
    MAXY = C;
    // pi test = getRang(mp(1, 1), 2);
    // cout << test.f << " " << test.s << "\n";
    seg = new Sparse(0, MAXX-1);
}

void update(int P, int Q, long long K) {
    //cout << "UPDATE " << P << " " << Q << " " << K << "\n";
    seg->upd(P, Q, K);
    // vpi x, y;
    // x.pb(mp(P, P)); y.pb(mp(Q, Q));
    // int curb = 0;
    // while(true){
    //     pi newx = x.bk;
    //     if((((newx.f)>>curb)&1) == 1){
    //         newx.f-=(1<<curb);
    //     }
    //     if((((newx.s)>>curb)&1) == 0){
    //         newx.s+=(1<<curb);
    //     }
    //     if(check(newx)){
    //         x.pb(newx);
    //     }
    //     else break;
    //     curb++;
    //     // cout << newx.f << " " << newx.s << "\n";
    //     // cout << "HI\n";
    //     // cout.flush();
    // }
    // curb = 0;
    // while(true){
    //     pi newy = y.bk;
    //     if((((newy.f)>>curb)&1) == 1){
    //         newy.f-=(1<<curb);
    //     }
    //     if((((newy.s)>>curb)&1) == 0){
    //         newy.s+=(1<<curb);
    //     }
    //     if(check(newy)){
    //         y.pb(newy);
    //     }
    //     else break;
    //     curb++;
        
    // }
    // for(auto a: x){
    //     for(auto b: y){
    //         seg[mp(a, b)] = gcd2(seg[mp(a, b)], K);
    //     }
    // }
}

ll calculate(int lox, int loy, int hix, int hiy) {
    //cout << "QUERY " << lox << " " << hix << " " << loy << " " << hiy << "\n";
    return seg->query(lox, hix, loy, hiy);
    // //cout << "calculate " << P << " " << Q << " " << U << " " << V << "\n";
    // vpi x, y;
    // pi curx = mp(P, U);
    // pi cury = mp(Q, V);
    // int curb = 0;
    // while(curx.f <= curx.s){
    //     if(((curx.f>>curb)&1) == 1){
    //         x.pb(mp(curx.f, curx.f+(1<<curb)-1));
    //         curx.f+=(1<<curb);
    //     }
    //     if(curx.f > curx.s) break;
    //     if(((curx.s>>curb)&1) == 0){
    //         x.pb(mp(curx.s-(1<<curb)+1, curx.s));
    //         curx.s-=(1<<curb);
    //     }
    //     curb++;
    // }
    // curb = 0;
    // while(cury.f <= cury.s){
    //     if(((cury.f>>curb)&1) == 1){
    //         y.pb(mp(cury.f, cury.f+(1<<curb)-1));
    //         cury.f+=(1<<curb);
    //     }
    //     if(cury.f > cury.s) break;
    //     if(((cury.s>>curb)&1) == 0){
    //         y.pb(mp(cury.s-(1<<curb)+1, cury.s));
    //         cury.s-=(1<<curb);
    //     }
    //     curb++;
    // }

    // ll res = 0;
    // // for(auto u: x){
    // //     cout << u.f << " " << u.s << "\n";
    // // }
    // // cout << "\n";
    // // for(auto u: y){
    // //     cout << u.f << " " << u.s << "\n";
    // // }
    // for(auto a: x){
    //     for(auto b: y){
    //         if(seg.count(mp(a, b))) res = gcd2(res, seg[mp(a, b)]);
    //     }
    // }
    // /* ... */
    // return res;
}

Compilation message

grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
   18 |  int res;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 588 ms 12920 KB Output is correct
5 Correct 403 ms 12664 KB Output is correct
6 Correct 516 ms 10232 KB Output is correct
7 Correct 600 ms 9848 KB Output is correct
8 Correct 379 ms 8184 KB Output is correct
9 Correct 561 ms 9976 KB Output is correct
10 Correct 557 ms 9592 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1002 ms 16020 KB Output is correct
13 Correct 1552 ms 9164 KB Output is correct
14 Correct 281 ms 5880 KB Output is correct
15 Correct 1717 ms 10748 KB Output is correct
16 Correct 248 ms 14328 KB Output is correct
17 Correct 772 ms 11512 KB Output is correct
18 Correct 1586 ms 15756 KB Output is correct
19 Correct 1364 ms 15992 KB Output is correct
20 Correct 1273 ms 15352 KB Output is correct
21 Correct 0 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 582 ms 12824 KB Output is correct
13 Correct 442 ms 12632 KB Output is correct
14 Correct 531 ms 10104 KB Output is correct
15 Correct 596 ms 9848 KB Output is correct
16 Correct 383 ms 8184 KB Output is correct
17 Correct 601 ms 9872 KB Output is correct
18 Correct 599 ms 9592 KB Output is correct
19 Correct 958 ms 15964 KB Output is correct
20 Correct 1540 ms 9068 KB Output is correct
21 Correct 274 ms 5624 KB Output is correct
22 Correct 1692 ms 10776 KB Output is correct
23 Correct 250 ms 14200 KB Output is correct
24 Correct 765 ms 11512 KB Output is correct
25 Correct 1577 ms 15736 KB Output is correct
26 Correct 1340 ms 15864 KB Output is correct
27 Correct 1247 ms 15360 KB Output is correct
28 Correct 604 ms 48760 KB Output is correct
29 Correct 1607 ms 51020 KB Output is correct
30 Correct 2663 ms 38264 KB Output is correct
31 Correct 2299 ms 31008 KB Output is correct
32 Correct 421 ms 10360 KB Output is correct
33 Correct 558 ms 10968 KB Output is correct
34 Correct 338 ms 44664 KB Output is correct
35 Correct 1204 ms 30052 KB Output is correct
36 Correct 2627 ms 48864 KB Output is correct
37 Correct 2087 ms 49032 KB Output is correct
38 Correct 2232 ms 48516 KB Output is correct
39 Correct 1673 ms 40056 KB Output is correct
40 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 612 ms 12812 KB Output is correct
13 Correct 409 ms 12536 KB Output is correct
14 Correct 552 ms 10100 KB Output is correct
15 Correct 573 ms 9908 KB Output is correct
16 Correct 383 ms 8248 KB Output is correct
17 Correct 549 ms 9976 KB Output is correct
18 Correct 578 ms 9656 KB Output is correct
19 Correct 972 ms 15736 KB Output is correct
20 Correct 1516 ms 8956 KB Output is correct
21 Correct 273 ms 5624 KB Output is correct
22 Correct 1719 ms 10924 KB Output is correct
23 Correct 257 ms 14376 KB Output is correct
24 Correct 760 ms 11288 KB Output is correct
25 Correct 1591 ms 15748 KB Output is correct
26 Correct 1337 ms 15844 KB Output is correct
27 Correct 1165 ms 15384 KB Output is correct
28 Correct 592 ms 49016 KB Output is correct
29 Correct 1583 ms 51008 KB Output is correct
30 Correct 2579 ms 38516 KB Output is correct
31 Correct 2306 ms 30940 KB Output is correct
32 Correct 399 ms 10388 KB Output is correct
33 Correct 549 ms 11000 KB Output is correct
34 Correct 329 ms 44792 KB Output is correct
35 Correct 1112 ms 30076 KB Output is correct
36 Correct 2353 ms 49024 KB Output is correct
37 Correct 1891 ms 49136 KB Output is correct
38 Correct 1890 ms 48760 KB Output is correct
39 Correct 804 ms 92920 KB Output is correct
40 Correct 2812 ms 93804 KB Output is correct
41 Correct 3929 ms 73556 KB Output is correct
42 Correct 3356 ms 57832 KB Output is correct
43 Correct 616 ms 88440 KB Output is correct
44 Correct 477 ms 10744 KB Output is correct
45 Correct 1572 ms 40256 KB Output is correct
46 Correct 3414 ms 92440 KB Output is correct
47 Correct 3178 ms 92576 KB Output is correct
48 Correct 3095 ms 92112 KB Output is correct
49 Correct 0 ms 256 KB Output is correct