Submission #898200

#TimeUsernameProblemLanguageResultExecution timeMemory
898200Faisal_SaqibGame (IOI13_game)C++17
63 / 100
1413 ms256000 KiB
#include "game.h"
#include <iostream>
#include <numeric>
using namespace std;
// const int N=1e9;
int R,C;
struct SegmentTree
{
  long long gdc=0;
  SegmentTree* next[2];
  SegmentTree(int l,int r)
  {
    next[0]=next[1]=NULL;
  }
  long long get(int& l,int& r,int s=0,int e=C-1)
  {
    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,s,(s+e)/2));
    if(next[1]!=NULL)
      ans=gcd(ans,next[1]->get(l,r,((s+e)/2)+1,e));
    return ans;
  }
  void Update(int& l,long long& val,int s=0,int e=C-1)
  {
    if(s==e)
    {
      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,s,(s+e)/2);
    }
    else
    {
      if(next[1]==NULL)
        next[1]=new SegmentTree(1+((s+e)/2),e);
      next[1]->Update(l,val,1+((s+e)/2),e);
    }
    gdc=0;
    if(next[0]!=NULL)
      gdc=gcd(next[0]->gdc,gdc);
    if(next[1]!=NULL)
      gdc=gcd(next[1]->gdc,gdc);
  }
};
struct SegmentTree2D
{
  SegmentTree* heavy;
  SegmentTree2D* next[2];
  SegmentTree2D(int l,int r)
  {
    heavy=new SegmentTree(0,C-1);
    next[0]=next[1]=NULL;
  }
  long long get(int& sx,int& ex,int& sy,int& ey,int s=0,int e=R-1)
  {
    if(ex<s or e<sx)
      return 0;
    if(sx<=s and e<=ex)
      return heavy->get(sy,ey);
    long long ans=0;
    if(next[0]!=NULL)
      ans=gcd(ans,next[0]->get(sx,ex,sy,ey,s,(s+e)/2));
    if(next[1]!=NULL)
      ans=gcd(ans,next[1]->get(sx,ex,sy,ey,1+((s+e)/2),e));
    return ans;
  }
  void Update(int& l,int& r,long long& val,int s=0,int e=R-1)
  {
    if(s==e)
    {
      heavy->Update(r,val);
      return;
    }
    long long gdc=0;
    if(l<=((s+e)/2))
    {
      if(next[0]==NULL)
        next[0]=new SegmentTree2D(s,(s+e)/2);
      next[0]->Update(l,r,val,s,(s+e)/2);
    }
    else
    {
      if(next[1]==NULL)
        next[1]=new SegmentTree2D(1+((s+e)/2),e);
      next[1]->Update(l,r,val,1+((s+e)/2),e);
    }
    if(next[0]!=NULL)
      gdc=gcd(gdc,next[0]->heavy->get(r,r));
    if(next[1]!=NULL)
      gdc=gcd(gdc,next[1]->heavy->get(r,r));
    heavy->Update(r,gdc);
  }
};
SegmentTree2D* st;
void init(int r, int c)
{
  C=c;
  R=r;
  st=new SegmentTree2D(0,R-1);
}
void update(int p, int q, long long k)
{
  st->Update(p,q,k);
}
long long calculate(int P, int Q, int U, int V)
{
  return st->get(P,U,Q,V);
}
#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...