Cod sursa(job #1795101)

Utilizator c0mradec0mrade c0mrade Data 1 noiembrie 2016 23:29:12
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.71 kb
#include <fstream>
#include <cstdio>
#include <algorithm>

using namespace std;

class InputReader {
public:
    InputReader() {}
    InputReader(const char *file_name) {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }

    template <typename T>
    inline InputReader &operator >>(T &n) {
        while(buffer[cursor] < '0' || buffer[cursor] > '9') {
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        return *this;
    }
    void close() {
        fclose(input_file);
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance() {
        ++ cursor;
        if(cursor == SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }

public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
};

const int N = 1e5;
int n;
int t[2 * N];

int main()
{
    InputReader cin("arbint.in");
    OutParser cout("arbint.out");

    int m = 0;
    cin >> n >> m;

    for (int i = 0; i < n; ++ i)
        cin >> t[n + i];

    for (int i = n - 1; i > 0; -- i)
        t[i] = max(t[i << 1], t[i << 1 | 1]);

    bool type;
    int a, b;
    while (m --) {
        cin >> type >> a >> b;
        --a;
        if (!type)
        {
            int res = 0;
            for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
                if (a & 1)
                    res = max(res, t[a ++]);
                if (b & 1)
                    res = max(res, t[-- b]);
            }
            cout << res << '\n';
        }
        else
        {
            for (t[a += n] = b; a > 1; a >>= 1)
                t[a>>1] = max(t[a], t[a ^ 1]);
        }
    }

    return 0;
}