Submission #393801

# Submission time Handle Problem Language Result Execution time Memory
393801 2021-04-24T14:14:56 Z MKopchev Game (IOI13_game) C++14
0 / 100
610 ms 14284 KB
#include "game.h"
#include<bits/stdc++.h>
using namespace std;

struct info
{
    long long g;
    int l,r;
};

vector< vector<info> > tree;

int pointer;

int make_info()
{
    info me;
    me.g=0;
    me.l=-1;
    me.r=-1;

    vector<info> now={};
    now.push_back(me);
    now.push_back(me);

    tree.push_back(now);
    pointer++;

    //cout<<"make_info "<<pointer-1<<endl;

    return pointer-1;
}
int build(int id)
{
    //int ret=tree[id].size();

    info me;
    me.g=0;
    me.l=-1;
    me.r=-1;

    tree[id].push_back(me);

    //cout<<"build "<<id<<" "<<ret<<endl;

    return tree[id].size()-1;
}

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

int n,m;
int x_first,y_first,x_second,y_second;

void init(int R, int C){
    n=R-1;
    m=C-1;
    make_info();
}

long long ask_for(int id,int lq,int rq)
{
    if(id==-1)return 0;

    int l=0,r=m;

    int node=1;

    while(l!=lq&&r!=rq)
    {
        int av=(l+r)/2;

        if(rq<=av)
        {
            if(tree[id][node].l==-1)return 0;

            node=tree[id][node].l;
            r=av;
        }
        else
        {
            if(tree[id][node].r==-1)return 0;

            node=tree[id][node].r;
            l=av+1;
        }
    }

    return tree[id][node].g;
}
long long minor_update(int id,int node,int l,int r,int y,long long val,bool type)
{
    //cout<<"minor_update "<<id<<" "<<node<<" "<<l<<" "<<r<<" "<<y<<" "<<val<<" "<<type<<" "<<tree[id].size()<<endl;

    if(l==r)
    {
        if(type==1)tree[id][node].g=val;
        else tree[id][node].g=gcd2(ask_for(tree[id][0].l,l,r),ask_for(tree[id][0].r,l,r));

        //cout<<"stop "<<id<<" "<<node<<" -> "<<tree[id][node].g<<endl;

        return tree[id][node].g;
    }

    int av=(l+r)/2;

    long long ret=0;

    if(y<=av)
    {
        if(tree[id][node].l==-1)
        {
            int help=build(id);
            tree[id][node].l=help;
            //tree[id][node].l=build(id);
        }
        minor_update(id,tree[id][node].l,l,av,y,val,type);
    }

    else
    {
        if(tree[id][node].r==-1)
        {
            int help=build(id);
            tree[id][node].r=help;
            //tree[id][node].r=build(id);
        }
        minor_update(id,tree[id][node].r,av+1,r,y,val,type);
    }

    if(tree[id][node].l!=-1)ret=gcd2(ret,tree[id][tree[id][node].l].g);
    if(tree[id][node].r!=-1)ret=gcd2(ret,tree[id][tree[id][node].r].g);

    tree[id][node].g=ret;

    //cout<<id<<" "<<node<<" -> "<<tree[id][node].g<<endl;

    return ret;
}
void main_update(int node,int l,int r,int x,int y,long long val)
{
    if(l==r)
    {
        minor_update(node,1,0,m,y,val,1);
        return;
    }

    int av=(l+r)/2;

    if(x<=av)
    {
        if(tree[node][0].l==-1)
        {
            int help=make_info();
            tree[node][0].l=help;
        }

        main_update(tree[node][0].l,l,av,x,y,val);
    }
    else
    {
        if(tree[node][0].r==-1)
        {
            int help=make_info();
            tree[node][0].r=help;
        }
        main_update(tree[node][0].r,av+1,r,x,y,val);
    }

    minor_update(node,1,0,m,y,val,0);
}
void update(int P, int Q, long long K) {
    main_update(0,0,n,P,Q,K);
    //cout<<"---"<<endl;
}

long long minor_query(int id,int node,int l,int r,int lq,int rq)
{
    if(l==lq&&r==rq)
    {
        //cout<<"minor "<<id<<" "<<node<<" "<<l<<" "<<r<<" -> "<<tree[id][node].g<<endl;
        return tree[id][node].g;
    }
    long long ret=0;

    int av=(l+r)/2;

    if(lq<=av)
    {
        if(tree[id][node].l!=-1)ret=gcd2(  ret,minor_query(id,tree[id][node].l,l,av,lq,min(av,rq)));
    }

    if(av<rq)
    {
        if(tree[id][node].r!=-1)ret=gcd2(ret,minor_query(id,tree[id][node].r,av+1,r,max(av+1,lq),rq));
    }

    return ret;
}

long long main_query(int node,int l,int r,int lq,int rq)
{
    if(l==lq&&r==rq)return minor_query(node,1,0,m,y_first,y_second);

    long long ret=0;

    int av=(l+r)/2;

    if(lq<=av)
    {
        if(tree[node][0].l!=-1)ret=gcd2(ret,main_query(tree[node][0].l,l,av,lq,min(av,rq)));
    }

    if(av<rq)
    {
        if(tree[node][0].r!=-1)ret=gcd2(ret,main_query(tree[node][0].r,av+1,r,max(av+1,lq),rq));
    }

    return ret;
}
long long calculate(int P, int Q, int U, int V) {
    x_first=P;
    y_first=Q;
    x_second=U;
    y_second=V;

    return main_query(0,0,n,x_first,x_second);
}
/*
int main()
{
init(2,3);

update(0,0,20);
update(0,2,15);
update(1,1,12);
cout<<calculate(0,0,0,2)<<endl;//5
cout<<calculate(0,0,1,1)<<endl;//4
update(0,1,6);
update(1,1,14);
cout<<calculate(0,0,0,2)<<endl;//1
cout<<calculate(0,0,1,1)<<endl;//2

    return 0;
}
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Incorrect 610 ms 14284 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -