Submission #782774

# Submission time Handle Problem Language Result Execution time Memory
782774 2023-07-14T09:13:49 Z FatihSolak Game (IOI13_game) C++17
100 / 100
4009 ms 16496 KB
    #include "game.h"
    #include <algorithm>
    #include <iostream>
    #include <cassert>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <queue>
    #include <map>
    #include <set>
     
    using namespace std;
     
    #define type(x) __typeof((x).begin())
    #define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)
    #define y1 ___y1
     
    typedef long long ll;
    typedef pair < int, int > ii;
     
    const int inf = 1e9 + 333;
    const ll linf = 1e18 + 333;
     
    const int N = 22000;
    const int K = 92;
     
    class tree{ public: 
        int x, y, x1, y1, x2, y2;
        ll val, ans;
        tree *l, *r;
        tree() {
            x = y = x1 = y1 = x2 = y2 = -1;
            val = ans = 0;
            l = r = 0;
        }
    };
     
    typedef tree* pTree;
     
    int r, c, n, sz, type, x1, y1, x2, y2;
    ll k;
    map < ii, int > h;
    map < ii, ll > M;
    pair < ii, ll > a[N];
     
    inline ll gcd(ll a, ll b) {
        while(b) {
            a %= b;
            swap(a, b);
        }
        return a;
    }
     
    bool cmp(pair < ii, ll > x, pair < ii, ll > y) {
        if(x.first.second == y.first.second)
            return x.first.first < y.first.first;
        return x.first.second < y.first.second;
    }
     
    void init(pTree t, int l, int r, bool d = 0) {
        
        int m = l + r >> 1;
        
        if(!d)
            nth_element(a + l, a + m, a + r + 1);
        else
            nth_element(a + l, a + m, a + r + 1, cmp);
        
        t -> x = t -> x1 = t -> x2 = a[m].first.first;
        t -> y = t -> y1 = t -> y2 = a[m].first.second;
        t -> val = t -> ans = a[m].second;
        
        if(l < m) {
            if(!t -> l)
                t -> l = new tree;
            init(t -> l, l, m - 1, !d);
            t -> ans = gcd(t -> ans, t -> l -> ans);
            t -> x1 = min(t -> x1, t -> l -> x1);
            t -> y1 = min(t -> y1, t -> l -> y1);
            t -> x2 = max(t -> x2, t -> l -> x2);
            t -> y2 = max(t -> y2, t -> l -> y2);
        }
        
        if(m < r) {
            if(!t -> r)
                t -> r = new tree;
            init(t -> r, m + 1, r, !d);
            t -> ans = gcd(t -> ans, t -> r -> ans);
            t -> x1 = min(t -> x1, t -> r -> x1);
            t -> y1 = min(t -> y1, t -> r -> y1);
            t -> x2 = max(t -> x2, t -> r -> x2);
            t -> y2 = max(t -> y2, t -> r -> y2);
        }
        
    }
     
    ll query(pTree t) {
        
        if(x2 < t -> x1 or t -> x2 < x1 or y2 < t -> y1 or t -> y2 < y1)
            return 0;
        
        if(x1 <= t -> x1 and t -> x2 <= x2 and y1 <= t -> y1 and t -> y2 <= y2)
            return t -> ans;
        
        ll ans = 0;
        
        if(x1 <= t -> x and t -> x <= x2 and y1 <= t -> y and t -> y <= y2)
            ans = t -> val;
        
        if(t -> l)
            ans = gcd(ans, query(t -> l));
        
        if(t -> r)
            ans = gcd(ans, query(t -> r));
        
        return ans;
        
    }
     
    void change(pTree t, bool d = 0) {
        
        if(x1 == t -> x and y1 == t -> y) {
            t -> val = t -> ans = k;
            if(t -> l)
                t -> ans = gcd(t -> ans, t -> l -> ans);
            if(t -> r)
                t -> ans = gcd(t -> ans, t -> r -> ans);
            return;
        }
        
        if((!d and ii(x1, y1) < ii(t -> x, t -> y)) or (d and ii(y1, x1) < ii(t -> y, t -> x)))
            change(t -> l, !d);
        else
            change(t -> r, !d);
        
        t -> ans = t -> val;
        
        if(t -> l)
            t -> ans = gcd(t -> ans, t -> l -> ans);
        
        if(t -> r)
            t -> ans = gcd(t -> ans, t -> r -> ans);
        
    }
     
    pTree t;
     
    void init(int R, int C) {
    	r = R;
    	c = C;
    	t = new tree;
    }
     
    void update(int x1, int y1, ll k) {
    	:: x1 = x1;
    	:: y1 = y1;
    	:: k = k;
                if(h.find(ii(x1, y1)) != h.end()) {
                    change(t);
                    a[h[ii(x1, y1)]].second = k;
    				return;
                }
                M[ii(x1, y1)] = k;
                if(M.size() == K) {
                    foreach(it, M)
                        a[sz++] = *it;
                    M.clear();
                    init(t, 0, sz - 1);
                    for(int i = 0; i < sz; i++)
                        h[a[i].first] = i;
                }
    }
     
    ll calculate(int x1, int y1, int x2, int y2) {
    	:: x1 = x1;
    	:: y1 = y1;
    	:: x2 = x2;
    	:: y2 = y2;
                ll ans = query(t);
                foreach(it, M) {
                    int x = it -> first.first;
                    int y = it -> first.second;
                    ll k = it -> second;
                    if(x1 <= x and x <= x2 and y1 <= y and y <= y2)
                        ans = gcd(ans, k);
                }
    			return ans;
    }

Compilation message

game.cpp: In function 'void init(pTree, int, int, bool)':
game.cpp:63:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   63 |         int m = l + r >> 1;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 0 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 738 ms 10048 KB Output is correct
5 Correct 450 ms 9904 KB Output is correct
6 Correct 686 ms 7392 KB Output is correct
7 Correct 684 ms 7200 KB Output is correct
8 Correct 398 ms 6768 KB Output is correct
9 Correct 692 ms 7276 KB Output is correct
10 Correct 691 ms 6732 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1414 ms 9744 KB Output is correct
13 Correct 1917 ms 5900 KB Output is correct
14 Correct 328 ms 5596 KB Output is correct
15 Correct 1929 ms 6476 KB Output is correct
16 Correct 495 ms 6604 KB Output is correct
17 Correct 682 ms 7072 KB Output is correct
18 Correct 1432 ms 8108 KB Output is correct
19 Correct 1145 ms 8148 KB Output is correct
20 Correct 1196 ms 7788 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 739 ms 10236 KB Output is correct
13 Correct 454 ms 9948 KB Output is correct
14 Correct 680 ms 7372 KB Output is correct
15 Correct 688 ms 7064 KB Output is correct
16 Correct 406 ms 6812 KB Output is correct
17 Correct 694 ms 7288 KB Output is correct
18 Correct 692 ms 6860 KB Output is correct
19 Correct 1418 ms 9960 KB Output is correct
20 Correct 1905 ms 5860 KB Output is correct
21 Correct 328 ms 5484 KB Output is correct
22 Correct 1943 ms 6464 KB Output is correct
23 Correct 494 ms 6680 KB Output is correct
24 Correct 675 ms 7132 KB Output is correct
25 Correct 1419 ms 7956 KB Output is correct
26 Correct 1141 ms 8072 KB Output is correct
27 Correct 1199 ms 7500 KB Output is correct
28 Correct 305 ms 12780 KB Output is correct
29 Correct 1567 ms 15452 KB Output is correct
30 Correct 2242 ms 9152 KB Output is correct
31 Correct 1762 ms 8916 KB Output is correct
32 Correct 333 ms 10052 KB Output is correct
33 Correct 380 ms 9976 KB Output is correct
34 Correct 515 ms 9240 KB Output is correct
35 Correct 719 ms 11988 KB Output is correct
36 Correct 1463 ms 13332 KB Output is correct
37 Correct 1139 ms 13552 KB Output is correct
38 Correct 1218 ms 12876 KB Output is correct
39 Correct 953 ms 12904 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 743 ms 10148 KB Output is correct
13 Correct 441 ms 9932 KB Output is correct
14 Correct 690 ms 7396 KB Output is correct
15 Correct 698 ms 7068 KB Output is correct
16 Correct 400 ms 6808 KB Output is correct
17 Correct 693 ms 7220 KB Output is correct
18 Correct 697 ms 6956 KB Output is correct
19 Correct 1436 ms 9836 KB Output is correct
20 Correct 1940 ms 5856 KB Output is correct
21 Correct 329 ms 5616 KB Output is correct
22 Correct 1999 ms 6436 KB Output is correct
23 Correct 504 ms 6632 KB Output is correct
24 Correct 692 ms 7092 KB Output is correct
25 Correct 1473 ms 8032 KB Output is correct
26 Correct 1157 ms 8192 KB Output is correct
27 Correct 1200 ms 7704 KB Output is correct
28 Correct 306 ms 12788 KB Output is correct
29 Correct 1608 ms 15336 KB Output is correct
30 Correct 2248 ms 9128 KB Output is correct
31 Correct 1859 ms 8924 KB Output is correct
32 Correct 358 ms 10068 KB Output is correct
33 Correct 479 ms 9960 KB Output is correct
34 Correct 534 ms 9096 KB Output is correct
35 Correct 742 ms 12036 KB Output is correct
36 Correct 1543 ms 13260 KB Output is correct
37 Correct 1155 ms 13444 KB Output is correct
38 Correct 1317 ms 12880 KB Output is correct
39 Correct 600 ms 14544 KB Output is correct
40 Correct 3185 ms 16496 KB Output is correct
41 Correct 4009 ms 11300 KB Output is correct
42 Correct 3182 ms 10472 KB Output is correct
43 Correct 920 ms 11292 KB Output is correct
44 Correct 460 ms 10428 KB Output is correct
45 Correct 984 ms 12812 KB Output is correct
46 Correct 2274 ms 15316 KB Output is correct
47 Correct 2279 ms 15400 KB Output is correct
48 Correct 2396 ms 15016 KB Output is correct
49 Correct 0 ms 212 KB Output is correct