Submission #402777

#TimeUsernameProblemLanguageResultExecution timeMemory
402777ACmachineGame (IOI13_game)C++17
100 / 100
7649 ms84792 KiB
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;

#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)

const double EPS = 1e-9;
const int MOD = 1e9+7;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

void DBG(){cout << "]" << endl;}
template<typename T, typename ...U> void DBG(const T& head, const U... args){ cout << head << "; "; DBG(args...); }
#define dbg(...) cout << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__);
#define chk() cout << "Check at line(" << __LINE__ << ") hit." << endl;

template<class T, unsigned int U>
ostream& operator<<(ostream& out, const array<T, U> &v){out << "[";  REP(i, U) out << v[i] << ", ";  out << "]"; return out;}
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "[";  REP(i, v.size()) out << v[i] << ", ";  out << "]"; return out;}

template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }



long long gcd2(long long X, long long Y) {
    if(X == 0) return Y;
    if(Y == 0) return X;
    long long tmp;
    while (X != Y && Y != 0) {
        tmp = X;
        X = Y;
        Y = tmp % Y;
    }
    return X;
}

struct treap{
    ll val; int key;
    ll res;
    int y;
    array<treap*, 2> ch;
    treap(int _key, ll _val) : key(_key), val(_val), y(rand()), res(val){
        ch = {NULL, NULL};
    }
};


void recalc(treap *t){
    if(t == NULL) return;
    t->res = t->val;
    REP(i, 2){
        if(t->ch[i] != NULL)
            t->res = gcd2(t->res, t->ch[i]->res);
    }
    return;
}
ll getres(treap* t){
    return (t == NULL ? 0 : t->res);
}
pair<treap*, treap*> split(treap* t, int key){ // all < key
    if(t == NULL)
        return {NULL, NULL};
    if(t->key < key){
        auto pa = split(t->ch[1], key);
        t->ch[1] = pa.ff;
        recalc(t);
        return {t, pa.ss};
    }
    else{
        auto pa = split(t->ch[0], key);
        t->ch[0] = pa.ss;
        recalc(t);
        return {pa.ff, t};
    }
}
treap* merge(treap* f, treap *s){
    if(f == NULL) return s;
    if(s == NULL) return f;
    if(f->y >= s->y){
        f->ch[1] = merge(f->ch[1], s);
        recalc(f);
        return f;
    }
    else{
        s->ch[0] = merge(f, s->ch[0]);
        recalc(s);
        return s;
    }
}
treap* insert(treap* t, treap *s){ // second add to treapu t
    if(t == NULL) {
        return s;
    }
    auto paf = split(t, s->key);
    auto pas = split(paf.ss, s->key + 1);
    t = merge(paf.ff, merge(s, pas.ss));
    return t;
}
ll query_treap(treap* &t, int l, int r){
    if(t == NULL) return 0ll;
    auto pa = split(t, l);
    auto pa2 = split(pa.ss, r);
    ll res = getres(pa2.ff);
    t = merge(pa.ff, merge(pa2.ff, pa2.ss));
    return res;
}
// wohoo correct treap
// dynamic segtree

struct segtree2d{
    vector<treap*> tree;
    vector<int> lch; // left and right child
    vector<int> rch;
    int n;
    void create_node(){
        treap* s = new treap(-1, 0);
        tree.pb(s);
        lch.pb(-1);
        rch.pb(-1);
    }
    void pull(int id, int x){
        ll fv = 0, sv = 0;
        if(lch[id] != -1){
            fv = query_treap(tree[lch[id]], x, x + 1);
        }
        if(rch[id] != -1){
            sv = query_treap(tree[rch[id]], x, x + 1);
        }
        tree[id] = insert(tree[id], new treap(x, gcd2(fv, sv)));
    }
    segtree2d(){}
    segtree2d(int sz){
        n = 1;
        while(n <= sz)
            n *= 2;
        create_node();
    }
    void upd(int row, int col, int beg, int end, int v, ll value){
        if(beg == row && beg + 1 == end){
            treap* s = new treap(col, value);
            tree[v] = insert(tree[v], s);
            return;
        }
        int mid = (beg + end) >> 1;
        if(row < mid){
            if(lch[v] == -1){
                create_node();
                int id = (int)tree.size() - 1;
                lch[v] = id;
            }
            upd(row, col, beg, mid, lch[v], value);
        }
        else{
            if(rch[v] == -1){
                create_node();
                int id = (int)tree.size() - 1;
                rch[v] = id;
            }
            upd(row, col, mid, end, rch[v], value);
        }
        pull(v, col);
    }
    ll query(int x1, int x2, int y1, int y2, int beg, int end, int v){
        if(beg >= y1 && end <= y2){
            return query_treap(tree[v], x1, x2);
        }
        if(beg >= y2 || end <= y1)
            return 0ll;
        int mid = (beg + end) >> 1;
        ll tmf = 0;
        if(lch[v] != -1)
            tmf = query(x1, x2, y1, y2, beg, mid, lch[v]);
        ll tms = 0;
        if(rch[v] != -1)
            tms = query(x1, x2, y1, y2, mid, end, rch[v]);
        return gcd2(tmf, tms);
    }
};
segtree2d st;
void init(int R, int C) {
    srand(time(NULL));
    st = segtree2d(R);
}

void update(int P, int Q, long long K) {
    st.upd(P, Q, 0, st.n, 0, K);
}

long long calculate(int P, int Q, int U, int V) { // xrange[Q, V + 1); yrange[P, U + 1)
    ll res = st.query(Q, V + 1, P, U + 1, 0, st.n, 0);
    return res;
}

Compilation message (stderr)

game.cpp: In constructor 'treap::treap(int, ll)':
game.cpp:73:17: warning: 'treap::key' will be initialized after [-Wreorder]
   73 |     ll val; int key;
      |                 ^~~
game.cpp:73:8: warning:   'll treap::val' [-Wreorder]
   73 |     ll val; int key;
      |        ^~~
game.cpp:77:5: warning:   when initialized here [-Wreorder]
   77 |     treap(int _key, ll _val) : key(_key), val(_val), y(rand()), res(val){
      |     ^~~~~
game.cpp:75:9: warning: 'treap::y' will be initialized after [-Wreorder]
   75 |     int y;
      |         ^
game.cpp:74:8: warning:   'll treap::res' [-Wreorder]
   74 |     ll res;
      |        ^~~
game.cpp:77:5: warning:   when initialized here [-Wreorder]
   77 |     treap(int _key, ll _val) : key(_key), val(_val), y(rand()), res(val){
      |     ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...