Cod sursa(job #1770726)

Utilizator danielNiculaeDaniel Niculae danielNiculae Data 4 octombrie 2016 19:25:47
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.2 kb
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.spark
 */

/* 
 * File:   main.cpp
 * Author: daniel
 *
 * Created on 04 October 2016, 12:19
 */

#include <cstdlib>
#include <algorithm>

using namespace std;

int N, M;

    
FILE *fin = fopen("arbint.in", "r");
FILE *fout = fopen("arbint.out", "w");

class Node {
public:
    Node() {
        
    }
    
    Node(int value, int left, int right) {
        this->value = value;
        this->left = left;
        this->right = right;
        this->leftNode = NULL;
        this->rightNode = NULL;
    }
    int value;
    int left, right;
    Node* leftNode;
    Node* rightNode;
};

void insert(int val, int pos, Node* nd) {
//    fprintf(fout, "in insert, left %d, right %d\n", nd->left, nd->right);
//    fflush(fout);
    if(nd->left == nd->right) {
        nd->value = val;
        return;
    }
    else {
        nd->value = max(nd->value, val);
    }
    
    if(pos <= (nd->left+nd->right)/2) {
        if(nd->leftNode == NULL) {
            nd->leftNode = new Node(val, nd->left, (nd->left+nd->right)/2);
        }
        insert(val, pos, nd->leftNode);
    }
     else {
        if(nd->rightNode == NULL) {
            nd->rightNode = new Node(val, (nd->left+nd->right)/2 + 1, nd->right);
        }
        insert(val, pos, nd->rightNode);
    }
}

int getMaximum(int a, int b, Node* nd) {
//    fprintf(fout, "in maximum, left %d, right %d\n", nd->left, nd->right);
//    fflush(fout);
    if(a <= nd->left &&  nd->right <= b) {
        return nd->value;
    }
    
    int mid = (nd->left + nd->right)/2;
    if(a <= mid && mid < b) {
        return max(getMaximum(a, b, nd->leftNode), getMaximum(a, b, nd->rightNode));
    }
    else if(a <= mid) {
        return getMaximum(a, b, nd->leftNode);
    }
    else {
        return getMaximum(a, b, nd->rightNode);
    }
}

void updateValue(int pos, int val, Node* nd) {
//    fprintf(fout, "in update, left %d, right %d\n", nd->left, nd->right);
//    fflush(fout);
    if(nd->left == nd->right) {
        nd->value = val;
    }
    else {
        int mid = (nd->left + nd->right)/2;
        if(pos <= mid) {
            updateValue(pos, val, nd->leftNode);
            nd->value = max(nd->leftNode->value, nd->rightNode->value);
        }
        else {
            updateValue(pos, val, nd->rightNode);
            nd->value = max(nd->leftNode->value, nd->rightNode->value);
        } 
    }
}



/*
 * 
 */
int main(int argc, char** argv) {

    fscanf(fin, "%d %d", &N, &M);
    
//    fprintf(fin, "Printing root");
//    fflush(out);
    Node* root = new Node(-1, 1, N);//create root with default value
//    fprintf(fin, "Printed root");
//    fflush(out);
    
    for(int i = 1 ; i <= N ; i++) {
        int x;
        fscanf(fin, "%d", &x);
        insert(x, i, root);
    }
    
    for(int i = 1 ; i <= M ; i++) {
        int type, a, b;
        fscanf(fin, "%d %d %d", &type, &a, &b);
        if(type == 0) {
            fprintf(fout, "%d\n", getMaximum(a, b, root));
        }
        else if(type == 1) {
            updateValue(a, b, root);
        }
    }
    
    fclose(fin);
    fclose(fout);
    
    return 0;
}