Cod sursa(job #1164633)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 10:57:05
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.74 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "arbint.in";
const char outfile[] = "arbint.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int arb[MAXN << 2], a[MAXN], N, M;

inline void Build(int Node, int st, int dr) {
    if(st == dr) {
        arb[Node] = a[st];
        return;
    }
    int mid = ((st + dr) >> 1);
    Build(Node << 1, st, mid);
    Build((Node << 1) | 1, mid+1, dr);
    arb[Node] = max(arb[Node << 1], arb[(Node << 1) | 1]);
}

inline void Update(int Node, int st, int dr, int pos, int value) {
    if(st == dr) {
        arb[Node] = value;
        return;
    }
    int mid = ((st + dr) >> 1);
    if(pos <= mid)
        Update(Node << 1, st, mid, pos, value);
    else Update((Node << 1) | 1, mid+1, dr, pos, value);
    arb[Node] = max(arb[Node << 1], arb[(Node << 1) | 1]);
}

inline int Query(int Node, int st, int dr, int a, int b) {
    if(a <= st && dr <= b)
        return arb[Node];
    int mid = ((st + dr) >> 1);
    int actMax = -oo;
    if(a <= mid)
        actMax = max(actMax, Query(Node << 1, st, mid, a, b));
    if(mid < b)
        actMax = max(actMax, Query((Node << 1) | 1, mid+1, dr, a, b));
    return actMax;
}

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void get(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] -'0';
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    }
}

int main() {
    freopen(infile, "r", stdin);
    get(N);
    get(M);
    for(int i = 1 ; i <= N ; ++ i)
        get(a[i]);
    Build(1, 1, N);
    for(int i = 1 ; i <= M ; ++ i) {
        int op, a, b;
        get(op);
        get(a);
        get(b);
        switch(op) {
            case 0:
            fout << Query(1, 1, N, a, b) << '\n';
            break;
            case 1:
            Update(1, 1, N, a, b);
            break;
        }
    }
    fout.close();
    return 0;
}