# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
898072 | Faisal_Saqib | Game (IOI13_game) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #include "game.h"
#include <iostream>
#include <numeric>
using namespace std;
const int N=2000;
int R,C;
struct SegmentTree
{
long long gdc=0;
int s,e;
SegmentTree* next[2];
SegmentTree(int l,int r)
{
s=l;
e=r;
next[0]=next[1]=NULL;
}
long long get(int& l,int& r)
{
if(e<l or r<s)
return 0;
if(l<=s and e<=r)
return gdc;
long long ans=0;
if(next[0]!=NULL)
ans=gcd(ans,next[0]->get(l,r));
if(next[1]!=NULL)
ans=gcd(ans,next[1]->get(l,r));
return ans;
}
void Update(int& l,long long& val)
{
if(s==e)
{
if(s==l)
gdc=val;
return;
}
if(l<=((s+e)/2))
{
if(next[0]==NULL)
next[0]=new SegmentTree(s,(s+e)/2);
next[0]->Update(l,val);
}
else
{
if(next[1]==NULL)
next[1]=new SegmentTree(1+((s+e)/2),e);
next[1]->Update(l,val);
}
gdc=0;
if(next[0]!=NULL)
gdc=gcd(next[0]->gdc,gdc);
if(next[1]!=NULL)
gdc=gcd(next[1]->gdc,gdc);
}
};
struct SegmentTree2D
{
int s,e;
SegmentTree* heavy;
SegmentTree2D* next[2];
SegmentTree2D(int l,int r)
{
s=l;
e=r;
heavy=new SegmentTree(0,C-1);
next[0]=next[1]=NULL;
}
long long get(int& sx,int& sy,int& ex,int& ey)
{
if(sx<=s and e<=ex)
return heavy->get(sy,ey);
if(e<sx or ex<s)
return 0;
long long ans=0;
if(next[0]!=NULL)
{
ans=gcd(ans,next[0]->get(sx,sy,ex,ey));
}
if(next[1]!=NULL)
{
ans=gcd(ans,next[1]->get(sx,sy,ex,ey));
}
return ans;
}
void Update(int& l,int& r,long long& val)
{
heavy->Update(r,val);
if(s==e)
return;
if(l<=((s+e)/2))
{
if(next[0]==NULL)
next[0]=new SegmentTree2D(s,(s+e)/2);
next[0]->Update(l,r,val);
}
else
{
if(next[1]==NULL)
next[1]=new SegmentTree2D(1+((s+e)/2),e);
next[1]->Update(l,r,val);
}
}
};
SegmentTree2D* st;
void init(int r, int c)
{
R=r;
C=c;
}
void update(int p, int q, long long k)
{
if(st==NULL)
st=new SegmentTree2D(0,R-1);
st->Update(p,q,k);
}
long long calculate(int P, int Q, int U, int V)
{
long long ans=0;
if(st!=NULL)
ans=st->get(P,Q,U,V);
return ans;
}