Cod sursa(job #1749243)

Utilizator delia_ioanaCeapa Delia Ioana delia_ioana Data 28 august 2016 10:03:36
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <climits>
#include <math.h>
#include <stdio.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) >> 1;
    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;
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout); 

    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i ++) {
	scanf("%d", &aux);
//        infile >> aux;
        pos = i;
        val = aux;
        constructTree (0, n - 1, 0);
    }
 
    for (int i = 0; i < m; i ++) {
//        infile >> aux >> p1 >> p2;
	scanf("%d %d %d", &aux, &p1, &p2);
        a = p1 - 1;
        b = p2 - 1;
        if (aux == 0) {
            findMax(0, n - 1, 0);
	    printf("%d\n", maxi);
//            outfile << maxi << endl;
            maxi = -1;
        }   
        else {
            pos = p1 - 1;
            val = p2;
            constructTree (0, n - 1, 0);
        }
    }
 
    return 0;
}