Cod sursa(job #1955792)

Utilizator CodrinsahCotarlan Codrin Codrinsah Data 6 aprilie 2017 11:11:42
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
using namespace std;
ifstream fi ("arbint.in");
ofstream fo ("arbint.out");
struct pct
{
  int maxi,st,dr;
};
pct nod[200000];
int a[100006],poz[100006];
int nrel,nrq,i,tip,x,y;

void umplere (int p)
{
  if (nod[p].st!=nod[p].dr)
  {
    nod[2*p].st=nod[p].st;nod[2*p].dr=(nod[p].st+nod[p].dr)/2;
    nod[2*p+1].st=(nod[p].st+nod[p].dr)/2+1;nod[2*p+1].dr=nod[p].dr;
    umplere(2*p);
    umplere(2*p+1);
    nod[p].maxi=max(nod[2*p].maxi,nod[2*p+1].maxi);
  }
  else {nod[p].maxi=a[nod[p].st];poz[nod[p].st]=p;}
 // fo<<nod[p].st<<' '<<nod[p].dr<<' '<<nod[p].maxi<<'\n';
}

void precalc()
{
  nod[1].st=1;
  nod[1].dr=nrel;
  umplere(1);
}

int maxim(int p)
{
  int s=nod[p].st;
  int d=nod[p].dr;
  int mij=(s+d)/2;
  int el1=0,el2=0;
  if (s>=x and d<=y) el1=nod[p].maxi;
  else if (s!=d)
  {
    if (mij>=x) el1=maxim(2*p);
    if (mij+1<=y) el2=maxim(2*p+1);
  }
  else el1=nod[p].maxi;
  return max(el1,el2);
}

void schimb(int p)
{
  if (nod[p].st==nod[p].dr) nod[p].maxi=y;
  p=p/2;
  nod[p].maxi=max(nod[2*p].maxi,nod[2*p+1].maxi);
  if (p>=1) schimb(p);
}
int main()
{
    fi>>nrel>>nrq;
    for (i=1;i<=nrel;i++) fi>>a[i];
    precalc();
    for (i=1;i<=nrq;i++)
    {
      fi>>tip>>x>>y;
      if (tip==0)
        fo<<maxim(1)<<'\n';
      if (tip==1)
        schimb(poz[x]);
    }
    return 0;
}