Cod sursa(job #1749239)

Utilizator delia_ioanaCeapa Delia Ioana delia_ioana Data 28 august 2016 09:54:27
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <climits>
#include <math.h>
#define dim 100001
  
using namespace std;
int m, n, aux, start = 0, p1, p2, arb_size, a, b, pos, val, arbore[dim * 4 + 66], maxi = -1;
ifstream infile("arbint.in"); 
ofstream outfile("arbint.out"); 
 
void constructTree (int start, int end, int poz) {
    if (start == end) {
        arbore[poz] = val;
        return;
    }
    int doublePoz = poz << 1;

    int mid = (start + end) >> 1;
    if (pos <= mid) 
        constructTree(start, mid, doublePoz + 1);
    else constructTree(mid + 1, end, doublePoz + 2);
    arbore[poz] = max(arbore[doublePoz + 1], arbore[doublePoz + 2]);
} 
 
void findMax (int start, int end, int poz) {
    if (start >= a && b >= end) {
        if (arbore[poz] > maxi)
            maxi = arbore[poz];
        return;
    }
    int mid = (start + end) / 2;
    int doublePoz = poz << 1;
    if (a <= mid) 
        findMax(start, mid, doublePoz + 1);
    if (b > mid) 
        findMax(mid + 1, end, doublePoz + 2);
}
 
int main() {
    infile >> n >> m;
 
    for (int i = 0; i < n; i ++) {
        infile >> aux;
        pos = i;
        val = aux;
        constructTree (0, n - 1, 0);
    }
 
    infile >> aux;
    while (aux == 1) {
        infile >> p1 >> p2 >> aux;
        pos = p1 - 1;
        val = p2;
        constructTree (0, n - 1, 0);
        start ++;
    }
     
    for (int i = start; i < m; i ++) {
        infile >> p1 >> p2;
        a = p1 - 1;
        b = p2 - 1;
        if (aux == 0) {
            findMax(0, n - 1, 0);
            outfile << maxi << endl;
            maxi = -1;
        }   
        else {
            pos = p1 - 1;
            val = p2;
            constructTree (0, n - 1, 0);
        }
        if (i != m - 1)
            infile >> aux;
    }
 
    return 0;
}