Cod sursa(job #2572522)

Utilizator huza_lisandraHuza Lisandra huza_lisandra Data 5 martie 2020 13:11:46
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream fin ("arbint.in");
ofstream fout ("arbint.out");
pair<pair<int,int>,int> arb[500010];
int n, m, v[500010], maxx=0, x, y;
void construire (int nod, int st, int dr)
{
    int mij;
    arb[nod].first.first=st;
    arb[nod].first.second=dr;
    if (st==dr)
        arb[nod].second=v[st];
    else
    {
        mij=(st+dr)/2;
        construire(2*nod, st, mij);
        construire(2*nod+1, mij+1, dr);
        arb[nod].second=max(arb[2*nod].second, arb[2*nod+1].second);
    }
}
int maxim (int nod)
{
    int mij;
    if (x<=arb[nod].first.first && arb[nod].first.second<=y)
       maxx=max(maxx, arb[nod].second);
    else
    {
        mij=(arb[nod].first.first+arb[nod].first.second)/2;
        if (x<=mij) maxim(2*nod);
        if (y>mij) maxim(2*nod+1);
    }
}
void inloc(int nod)
{
    int mij;
      if (x==arb[nod].first.first && x==arb[nod].first.second)
        arb[nod].second=y;
      else
      {
          mij=(arb[nod].first.first+arb[nod].first.second)/2;
          if (x<=mij)
            inloc(2*nod);
          else
            inloc(2*nod+1);
          arb[nod].second=max(arb[2*nod].second, arb[2*nod+1].second);
      }
}
int main ()
{
    int i,cer;
    fin>>n>>m;
    for (i=1; i<=n; i++)
        fin>>v[i];
    construire(1, 1, n);
    for (i=1; i<=m; i++)
    {
        fin>>cer>>x>>y;
        if (cer==0)
        {
            maxx=0;
            maxim(1);
            fout<<maxx<<'\n';
        }
        else inloc(1);
    }
}