Cod sursa(job #1557466)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 27 decembrie 2015 16:21:38
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.24 kb
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 100000;
const int kBuffSize = 655536;

#define fastcall __attribute__((optimize("-O3")))
#define inline __inline__ __attribute__((always_inline))

int T[2 * kMaxN];

inline fastcall char getChar() {
    static char buff[kBuffSize];
    static int pos = kBuffSize;

    if (pos == kBuffSize) {
        fread(buff, 1, kBuffSize, stdin);
        pos = 0;
    }
    return buff[pos++];
}

inline fastcall int readInt() {
    int q = 0;
    char c;
    do {
        c = getChar();
    } while (!isdigit(c));
    do {
        q = (q << 1) + (q << 3) + (c - '0');
        c = getChar();
    } while (isdigit(c));
    return q;
}

char outBuff[kBuffSize];
int outPtr;

inline fastcall void putChar(const char &C) {
    outBuff[outPtr++] = C;
    if (outPtr == kBuffSize) {
        fwrite(outBuff, 1, kBuffSize, stdout);
        outPtr = 0;
    }
}

inline fastcall void writeInt(int X) {
    char digs[10];
    int n = 0, q;
    do {
        q = X / 10;
        digs[n++] = X - (q << 1) - (q << 3) + '0';
        X = q;
    } while (X);
    while (n--) {
        putChar(digs[n]);
    }
    putChar('\n');
}

int main(void) {
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    int N, Q;
    int opType, st, fn;
    int rez;

    N = readInt(); Q = readInt();
    for (int i = 0; i < N; i++) {
        T[i + N] = readInt();
    }
    for (int i = N - 1; i; i--) {
        T[i] = max(T[i << 1], T[(i << 1) | 1]);
    }

    while (Q--) {
        opType = readInt();
        if (!opType) {
            st = N + readInt() - 1; fn = N + readInt() - 1;
            rez = 0;
            while (st <= fn) {
                rez = max(rez, max(T[st], T[fn]));
                st = (st + 1) >> 1;
                fn = (fn - 1) >> 1;
            }
            writeInt(rez);
        } else {
            fn = N + readInt() - 1;
            T[fn] = readInt();
            while (fn > 1) {
                st = fn >> 1;
                T[st] = max(T[fn], T[fn ^ 1]);
                fn = st;
            }
        }
    }
    fclose(stdin);
    fwrite(outBuff, 1, outPtr, stdout);
    fclose(stdout);
    return 0;
}