Submission #1090839

# Submission time Handle Problem Language Result Execution time Memory
1090839 2024-09-18T22:11:38 Z vjudge1 Game (IOI13_game) C++17
0 / 100
1 ms 360 KB
#include <bits/stdc++.h>
#include "game.h"

using namespace std;
struct node{
    node *ll=nullptr;
    node *lr=nullptr;
    node *rl=nullptr;
    node *rr=nullptr;
    node *tatko=nullptr;
    long long value=0;
    int id;
    pair<long long,long long> x;
    pair<long long,long long> y;
};
int p=0;


// trgni voa posle
long long gcd2(long long x,long long y)
{
    if (x%y==0) return y;
    return gcd2(y,x%y);
}

int stepen(int x)
{
    int a = 1;
    while(a<=x)
    {
        a*=2;
    }
    return a;
}

node koren;
void init(int R,int C)
{
    int r = stepen(max(R,C));
    koren.x.first =0;
    koren.y.first =0;
    koren.x.second=r-1;
    koren.y.second=r-1;
    koren.id = p;
    p++;
}

void update1(long long r=0,long long c=0,long long v=0,node &n=koren)
{
    //cout<< "ulava u "<<n.x.first<<" "<<n.x.second<< "  "<<n.y.first<<" "<<n.y.second<<endl;
    //system("pause");
    if (n.x.first==n.x.second && n.y.first==n.y.second)
    {
        n.value = v;
        return;
    }

    long long midx = (n.x.second+n.x.first)/2;
    long long midy = (n.y.second+n.y.first)/2;

    if (n.x.first<=r && r<=midx)
    {
        if (n.y.first<=c && c<=midy)
        {
            if (n.ll==nullptr)
            {
                node *nov = new node;
                n.ll = nov;
                nov->x={n.x.first,midx};
                nov->y={n.y.first,midy};
                nov->tatko=&n;
                nov->value = v;
                nov->id = p;
                p++;
            }
            update1(r,c,v,*n.ll);
            //n.ll->value = gcd2(n.ll->value,v);
        }
        if (n.y.second>=c && c>midy)
        {
            if (n.lr==nullptr)
            {
                node *nov = new node;
                n.lr=nov;
                nov->x={n.x.first,midx};
                nov->y={midy+1,n.y.second};
                nov->tatko=&n;
                nov->value = v;
                nov->id = p;
                p++;
            }
            update1(r,c,v,*n.lr);
            n.lr->value = gcd2(n.lr->value,v);
        }
    }

    if (n.x.second>=r && r>midx)
    {
        if (c>=n.y.first && c<=midy)
        {
            if (n.rl==nullptr)
            {
                node *nov = new node;
                n.rl=nov;
                nov->x={midx+1,n.x.second};
                nov->y={n.y.first,midy};
                nov->tatko=&n;
                nov->value = v;
                nov->id = p;
                p++;
            }
            update1(r,c,v,*n.rl);
            //n.rl->value = gcd2(n.rl->value,v);
        }
        if (c<=n.y.second && c>midy)
        {
            if (n.rr==nullptr)
            {
                node *nov = new node;
                n.rr=nov;
                nov->x={midx+1,n.x.second};
                nov->y={midy+1,n.y.second};
                nov->tatko=&n;
                nov->value = v;
                nov->id = p;
                p++;
            }
            update1(r,c,v,*n.rr);
            //n.rr->value = gcd2(n.rr->value,v);
        }
    }
    if (n.value == 0) n.value=v;
    else n.value = gcd2(n.value,v);
}

long long calculate1(long long lx=0,long long ty=0,long long rx=0,long long by=0,node &n=koren)
{
    //cout<< "ulava za "<<n.x.first<< " "<<n.y.first<<"  "<<n.x.second<<" "<<n.y.second<<" rez="<<n.value<< " id="<<n.id<<endl;
    //system("pause");
    if (lx<=n.x.first && rx>=n.x.second && ty<=n.y.first && by>=n.y.second)
    {
        //cout<< "prvo id = "<<n.id<<" vrakja "<<n.value<<endl;
        return n.value;
    }
    long long odg=0;
    long long midx=(n.x.first+n.x.second)/2;
    long long midy=(n.y.first+n.y.second)/2;

    if (lx<=midx && rx>=n.x.first)
    {
        if (ty<=midy && by>=n.y.first && n.ll!=nullptr)
        {
            long long t = calculate1(lx,ty,rx,by,*n.ll);
            if (odg==0) odg = t;
            else if(t>0) odg = gcd2(odg,t);
        }
        if (midy<by && n.y.second>=ty && n.lr!=nullptr)
        {
            long long t = calculate1(lx,ty,rx,by,*n.lr);
            if (odg==0) odg = t;
            else if (t>0) odg = gcd2(odg,t);
        }
    }

    if (midx<rx && n.x.second>=lx)
    {
        if (ty<=midy && by>=n.y.first && n.rl!=nullptr)
        {
            long long t = calculate1(lx,ty,rx,by,*n.rl);
            if (odg==0) odg = t;
            else if(t>0) odg = gcd2(odg,t);
        }
        if (midy<by && n.y.second>=ty && n.rr!=nullptr)
        {
            long long t = calculate1(lx,ty,rx,by,*n.rr);
            if (odg==0) odg = t;
            else if(t>0) odg = gcd2(odg,t);
        }
    }

    //cout<< "id = "<<n.id<<" vrakja "<<odg<<endl;
    return odg;
}

void update(int P, int Q,long long K)
{
    update1(P,Q,K);
}
long long calculate(int P, int Q, int U, int V)
{
    return calculate1(P,Q,U,V);
}
/*
int main()
{
    /*
    init(2,3);
    update(0,0,20);
    update(0,2,15);
    update(1,1,12);
    cout<<calculate(0,0,0,2)<<endl;
    cout<<calculate(0,0,1,1)<<endl;
    update(0,1,6);
    update(1,1,14);
    cout<<calculate(0,0,0,2)<<endl;
    cout<<calculate(0,0,1,1)<<endl;
    
    init(4,4);
    update(0,0,10);
    update(0,1,12);
    update(0,2,14);
    update(0,3,16);
    update(1,0,20);
    update(1,1,18);
    update(1,3,100);
    update(2,0,15);
    update(2,2,98);
    update(3,0,13);
    update(3,1,13);

    /*for (int x1=0;x1<4;x1++)
        for (int x2=x1;x2<4;x2++)
            for (int y1=0;y1<4;y1++)
                for (int y2=y1;y2<4;y2++){
                    cout<< "od ("<<x1<<","<<y1<<") do ("<<x2<<","<<y2<<") = ";
                    cout<<calculate(x1,y1,x2,y2)<<endl;
                }
    cout<<"0,0 -> 1,1 ="<<calculate(0,0,1,1)<<endl;
    cout<<"0,2 -> 1,3 ="<<calculate(0,2,1,3)<<endl;
    cout<<"0,0 -> 2,0 ="<<calculate(0,0,2,0)<<endl;
    cout<<"2,2 -> 3,3 ="<<calculate(2,2,3,3)<<endl;
    cout<<"3,0 -> 3,1 ="<<calculate(3,0,3,1)<<endl;
    cout<<"2,0 -> 2,0 ="<<calculate(2,0,2,0)<<endl;


    return 0;
}*/

Compilation message

game.cpp:196:5: warning: "/*" within comment [-Wcomment]
  196 |     /*
      |      
game.cpp:221:5: warning: "/*" within comment [-Wcomment]
  221 |     /*for (int x1=0;x1<4;x1++)
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Runtime error 1 ms 348 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 1 ms 360 KB Execution killed with signal 8
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Runtime error 1 ms 360 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 360 KB Output is correct
2 Correct 0 ms 360 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Runtime error 0 ms 356 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 0 ms 360 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Runtime error 0 ms 360 KB Execution killed with signal 8
5 Halted 0 ms 0 KB -