Submission #1013412

#TimeUsernameProblemLanguageResultExecution timeMemory
1013412parsadox2Game (IOI13_game)C++17
10 / 100
1400 ms14868 KiB
#include "game.h"
#include <bits/stdc++.h>
 
using namespace std;
 
int N , M;
 
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 SEG{
    struct NODE{
        int lc , rc , l , r;
        long long g;
        NODE(){
            lc = 0;  rc = 0;
            g = 0;
        }
    };
    vector <NODE> t;
    NODE emp;
    void Build()
    {
        t.push_back(emp);
        t.push_back(emp);
        t[1].l = 0;
        t[1].r = M;
    }
    int Set(int id , long long vl , int node = 1 , int nl = 0 , int nr = M)
    {
        //cout << node << " " << id << " " << vl << " SET " << nl << " " << nr <<  endl;
        if(id < nl || nr <= id)
        {
            //cout << "SALAM " << endl;
            int p = t.size();
            t.push_back(emp);
            int nd = t.size();
            t.push_back(emp);
            t[nd].l = id;
            t[nd].r = id + 1;
            t[p].l = min(id , nl);
            t[p].r = max(id + 1 , nr);
            if(id >= nr)
            {
                t[p].lc = node;
                t[p].rc = nd;
            }
            else
            {
                t[p].lc = nd;
                t[p].rc = node;
            }
            Set(id , vl , p , t[p].l , t[p].r);
            return p;
        }
        if(nr - nl == 1)
        {
            t[node].g = vl;
            return node;
        }
        int mid = (nl + nr) >> 1;
        if(id < mid)
        {
            if(t[node].lc == 0)
            {
                int nd = t.size();
                t.push_back(emp);
                t[node].lc = nd;
                t[nd].l = id;
                t[nd].r = id + 1;
            }   
            int x = Set(id , vl , t[node].lc , t[t[node].lc].l , t[t[node].lc].r);
            t[node].lc = x;
        }
        else
        {
            if(t[node].rc == 0)
            {
                int nd = t.size();
                t.push_back(emp);
                t[node].rc = nd;
                t[nd].l = id;
                t[nd].r = id + 1;
            }
            int x = Set(id , vl , t[node].rc , t[t[node].rc].l , t[t[node].rc].r);
            t[node].rc = x;
        }
        t[node].g = gcd2(t[t[node].lc].g , t[t[node].rc].g);
        t[node].l = t[t[node].lc].l;
        t[node].r = t[t[node].rc].r;
        return node;
    }
    long long Get(int l , int r , int node = 1 , int nl = 0 , int nr = M)
    {
        //cout << node << " !! " << nl << " " << nr << " " << t[node].lc << " " << t[node].rc << endl;
        if(r <= nl || nr <= l)
            return 0;
        if(l <= nl && nr <= r)
        {
            //cout << node << " " << nl << " " << nr << " : " << t[node].g << endl;
            return t[node].g;
        }
        long long res = 0;
        if(t[node].lc != 0)
            res = gcd2(res , Get(l , r , t[node].lc , t[t[node].lc].l , t[t[node].lc].r));
        if(t[node].rc != 0)
            res = gcd2(res , Get(l , r , t[node].rc , t[t[node].rc].l , t[t[node].rc].r));
        return res;
    }
};
 
struct SEG2{
    struct NODE{
        int lc , rc;
        SEG s;
    };
    NODE emp;
    vector <NODE> t;
    void Build()
    {
        emp.s.Build();
        emp.lc = 0;
        emp.rc = 0;
        t.push_back(emp);
        t.push_back(emp);
    }
    void Set(int id , int a , long long b)
    {
        int nd = 1 , nl = 0 , nr = N , mid;
        vector <int> vec;
        while(nr - nl != 1)
        {
            vec.push_back(nd);
            mid = (nl + nr) >> 1;
            if(id < mid)
            {
                if(t[nd].lc == 0)
                {
                    t[nd].lc = t.size();
                    t.push_back(emp);
                }
                nr = mid;
                nd = t[nd].lc;
            }
            else
            {
                if(t[nd].rc == 0)
                {
                    t[nd].rc = t.size();
                    t.push_back(emp);
                }
                nl = mid;
                nd = t[nd].rc;
            }
        }
        t[nd].s.Set(a , b);
        while(!vec.empty())
        {
            int nd = vec.back();
            //cout << " ROW " << nd << endl;
            vec.pop_back();
            t[nd].s.Set(a , gcd2(t[t[nd].lc].s.Get(a , a + 1) , t[t[nd].rc].s.Get(a , a + 1)));
        }
    }
    long long Get(int l , int r , int s , int e , int node = 1 , int nl = 0 , int nr = N)
    {
        if(r <= nl || nr <= l)
            return 0;
        if(l <= nl && nr <= r)
        {
            long long x = t[node].s.Get(s , e);
            return x;
        }
        int mid = (nl + nr) >> 1;
        return gcd2(Get(l , r , s , e , t[node].lc , nl , mid) , Get(l , r , s , e , t[node].rc , mid , nr));
    }
} seg;
 
void init(int R, int C) {
    seg.Build();
    N = R;
    M = C;
}
 
void update(int P, int Q, long long K) {
    seg.Set(P , Q , K);
}
 
long long calculate(int P, int Q, int U, int V) {
    int l = min(P , U) , r = max(P , U);
    int s = min(Q , V) , e = max(Q , V);
    return seg.Get(l , r + 1 , s , e + 1);
}
/*
2 3 4
1 0 0 20
1 0 2 15
1 1 1 12
2 0 0 1 1
*/
#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...