Submission #659175

#TimeUsernameProblemLanguageResultExecution timeMemory
659175Danilo21Game (IOI13_game)C++14
10 / 100
807 ms256000 KiB
#include <bits/stdc++.h>
#include "game.h"

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))

using namespace std;

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 gcd_t{

    ll x;
    gcd_t(ll v = 0){ update(v); }
    gcd_t(const gcd_t& a){ update(a.x); }
    static gcd_t op(const gcd_t& a, const gcd_t& b){ return gcd_t(gcd2(a.x ,b.x)); }
    void update(ll v){ x = v; }
    ll query(){ return x; }
};

template<class T>
struct node{

    T val;
    node<T>* ch[2];
    node<T>(const T& v = T()){ Assign(v); }
    node<T>(node<T>* p){ Assign(p->val); }
    ~node<T>(){ delete ch[0]; delete ch[1]; }
    void Assign(const T& v){
        val = *new T(v);
        ch[0] = ch[1] = nullptr;
    }
    node<T>* Ch(int i, const T& a = T()){
        if (!ch[i]) ch[i] = new node<T>(a);
        return ch[i];
    }
    static node<T>* op(node<T>* a, node<T>* b){
        if (!a) return new node<T>(b);
        if (!b) return new node<T>(a);
        return new node<T>(T::op(a->val, b->val));
    }
    static T Val(node<T>* s, const T& a = T()){ return T(s ? s->val : a); }
};

class segtree{

    ll n;
    node<gcd_t>* root;

    static node<gcd_t>* op(node<gcd_t>* u, node<gcd_t>* v){
        if (!u && !v) return nullptr;
        node<gcd_t>* s = node<gcd_t>::op(u, v);
        node<gcd_t>* a = segtree::op((u ? u->ch[0] : nullptr), (v ? v->ch[0] : nullptr));
        node<gcd_t>* b = segtree::op((u ? u->ch[1] : nullptr), (v ? v->ch[1] : nullptr));
        s->ch[0] = a; s->ch[1] = b;
        return s;
    }

    void update(node<gcd_t>* s, ll l, ll r, ll pos, ll x){
        //cout << tb << s << sp << s->ch[0] << sp << s->ch[1] << sp << l << sp << r << endl;
        if (l == r){
            s->val.update(x);
            return;
        }
        int m = (l + r)>>1;
        if (pos <= m) update(s->Ch(0), l, m, pos, x);
        else update(s->Ch(1), m+1, r, pos, x);
        s->val = gcd_t::op(node<gcd_t>::Val(s->ch[0]), node<gcd_t>::Val(s->ch[1]));
    }

    gcd_t query(node<gcd_t>* s, ll l, ll r, ll ql, ll qr) const {
        if (l > qr || r < ql || !s) return gcd_t();
        if (l >= ql && r <= qr) return s->val;
        ll m = (l + r)>>1;
        gcd_t a = query(s->ch[0], l, m, ql, qr);
        gcd_t b = query(s->ch[1], m+1, r, ql, qr);
        return gcd_t::op(a, b);
    }

    void Copy(node<gcd_t>* u, node<gcd_t>* v){
        u->val = v->val;
        if (v->ch[0]) Copy(u->Ch(0), v->ch[0]);
        if (v->ch[1]) Copy(u->Ch(1), v->ch[1]);
    }

    void Print(node<gcd_t>* s, ll l, ll r) const {
        cout << tb << tb << s << sp << l << sp << r << sp << s->val.x << endl;
        ll m = (l + r)>>1;
        if (s->ch[0]) Print(s->ch[0], l, m);
        if (s->ch[1]) Print(s->ch[1], m+1, r);
    }

public:
    segtree(ll n = 0){ Assign(n); }
    segtree(const segtree& st){
        Assign(st.n);
        Copy(root, st.root);
    }
    ~segtree(){ delete root; }
    void Assign(ll s){
        n = highpow(s);
        if (bitcnt(s) > 1) n <<= 1;
        root = new node<gcd_t>();
    }
    ll Size() const { return n; }
    static segtree* op(const segtree& a, const segtree& b){
        segtree* r = new segtree(a.n);
        delete r->root;
        r->root = segtree::op(a.root, b.root);
        return r;
    }
    void update(ll pos, ll x){ update(root, 0, n-1, pos, x); }
    gcd_t query(ll l, ll r) const { return query(root, 0, n-1, l, r); }
    void Print() const { cout << tb << "SEGTREE: " << n << endl; Print(root, 0, n-1); }
};

class segtree2D{

    ll n;
    node<segtree>* root;

    void update(node<segtree>* s, ll l, ll r, ll pos, array<ll, 2> args){
        if (l == r){
            s->val.update(args[0], args[1]);
            return;
        }
        ll m = (l + r)>>1;
        ll n = s->val.Size();
        if (pos <= m) update(s->Ch(0, segtree(n)), l, m, pos, args);
        else update(s->Ch(1, segtree(n)), m+1, r, pos, args);
        s->val = *segtree::op(node<segtree>::Val(s->ch[0], segtree(n)), node<segtree>::Val(s->ch[1], segtree(n)));
    }

    gcd_t query(node<segtree>* s, ll l, ll r, ll ql, ll qr, array<ll, 2> args) const {
        if (l > qr || r < ql || !s) return gcd_t();
        if (l >= ql && r <= qr) return s->val.query(args[0], args[1]);
        ll m = (l + r)>>1;
        gcd_t a = query(s->ch[0], l, m, ql, qr, args);
        gcd_t b = query(s->ch[1], m+1, r, ql, qr, args);
        return gcd_t::op(a, b);
    }

    void Print(node<segtree>* s, int i, vector<pair<node<segtree>*, pair<ll, ll> > >& v, ll l, ll r) const {
        v[i] = make_pair(s, make_pair(l, r));
        ll m = (l + r)>>1;
        if (s->ch[0]) Print(s->ch[0], 2*i, v, l, m);
        if (s->ch[1]) Print(s->ch[1], 2*i+1, v, m+1, r);
    }

public:
    segtree2D(ll n = 0, const segtree& st = segtree()){ Assign(n, st); }
    void Assign(ll r, const segtree& st){
        n = highpow(r);
        if (bitcnt(r) > 1) n <<= 1;
        root = new node<segtree>(st);
    }
    void update(ll pos, array<ll, 2> args){ update(root, 0, n-1, pos, args); }
    ll query(ll l, ll r, array<ll, 2> args){ return query(root, 0, n-1, l, r, args).x; }
    void Print() const {
        vector<pair<node<segtree>*, pair<ll, ll> > > v(2*n, make_pair(nullptr, make_pair(0, 0)));
        Print(root, 1, v, 0, n-1);
        for (auto p : v){
            if (p.fi){
                cout << tb << p.se.fi << sp << p.se.se << endl;
                p.fi->val.Print();
                cout << endl;
            }
        }
    }
    segtree F(){ return root->ch[0]->ch[0]->val; }
};

const ll LINF = 4e18;
const int mxN = 1e3+10, INF = 2e9;
ll n, m, a[mxN];
segtree2D st;

void init(int R, int C){
    st.Assign(C, segtree(R));
}

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

long long calculate(int P, int Q, int U, int V) {
    return st.query(Q, V, {P, U});
}
#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...