Cod sursa(job #1982722)

Utilizator M.AndreiMuntea Andrei Marius M.Andrei Data 20 mai 2017 01:09:37
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <vector>
#include <cstdio>
#include <string>
using namespace std;

#define ll long long
#define ull unsigned long long

/*------------------------------------------------------------------*/

ifstream f{"arbint.in"};
ofstream q{"arbint.out"};

#define MAXN 400066

int arb[MAXN];
int n, m;


void update(int node, int left, int right, int pos, int value)
{
   if (left == right)
   {
      arb[node] = value;
      return;
   }

   int mid = (left + right) / 2;

   if (pos <= mid) update(node * 2, left, mid, pos, value);
   else update(node * 2 + 1, mid + 1, right, pos, value);

   arb[node] = max(arb[node * 2], arb[node * 2 + 1]);
}

int query(int node, int left, int right, int start, int end)
{
   if (start <= left && right <= end)
   {
      return arb[node];
   }

   int mid = (left + right) / 2;
   int maxL = numeric_limits<int>::min();
   int maxR = numeric_limits<int>::min();

   if (start <= mid) maxL = query(node * 2, left, mid, start, end);
   if (mid < end) maxR = query(node * 2 + 1, mid + 1, right, start, end);

   return max(maxL, maxR);
}


int main()
{
   ios_base::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);

   f >> n >> m;
   int x, a, b;

   for (int i = 1; i <= n; ++i)
   {
      f >> x;
      update(1, 1, n, i, x);
   }

   for(int i = 1; i <=m; ++i)
   {
      f >> x >> a >> b;
      if (x == 0)  q << query(1, 1, n, a, b) << "\n";
      else update(1, 1, n, a, b);
   }


   f.close();
   q.close();
   return 0;
}