Cod sursa(job #1213152)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 27 iulie 2014 13:20:40
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.39 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 n, m, a[MAXN], arb[MAXN << 2];

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 int query(int node, int st, int dr, int x, int y) {
    if(x <= st && dr <= y)
        return arb[node];
    int mid = ((st + dr) >> 1);
    int ans = -oo;
    if(x <= mid)
        ans = query(node << 1, st, mid, x, y);
    if(mid < y)
        ans = max(ans, query((node << 1) | 1, mid + 1, dr, x, y));
    return ans;
}

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

int main() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    fin >> n >> m;
    for(int i = 1 ; i <= n ; ++ i)
        fin >> a[i];
    build(1, 1, n);
    while(m --) {
        int op, x, y;
        fin >> op >> x >> y;
        if(op == 1)
            update(1, 1, n, x, y);
        else
            fout << query(1, 1, n, x, y) << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}