Cod sursa(job #1863360)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 30 ianuarie 2017 20:59:44
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.02 kb
#include <bits/stdc++.h>
using namespace std;
 
class ipfstream{
    ifstream f;
    char buf[(int)1e5], *p = buf, *ep = buf + sizeof(buf);
    void adv(){
        if(++p == ep) f.read(p=buf, sizeof(buf)); }
public:
    ipfstream(const char * const str): f(str){
        f.read(buf, sizeof(buf)); }
    ipfstream& operator>>(int& rhs){
        while(*p < '0') adv();
        rhs = 0;
        while(*p >= '0') rhs = 10 * rhs + *p - '0', adv();
        return *this; } };

class opfstream{
    ofstream g;
    char buf[(int)1e5], *p = buf, *ep = buf + sizeof(buf) - 20;
public:
    opfstream(const char * const str): g(str){}
    ~opfstream(){
        g.write(buf, p-buf); }
    opfstream& operator<<(const char ch){
        *p++ = ch;
        if(p == ep) g.write(p=buf, ep-buf);
        return *this; }
    opfstream& operator<<(int x){
        if(x == 0) return (*this) << '0';
        auto *tmp = p;
        while(x) *tmp++ = x%10 + '0', x /= 10;
        reverse(p, tmp);
        p = tmp;
        if(p >= ep){
            g.write(buf, p-buf);
            p = buf; }
        return (*this); } };
 
class arbint{
    int n;
    vector<int> buf;
public:
    arbint(const vector<int>& vv): n(vv.size()), buf(2*n){
        copy(begin(vv), end(vv), begin(buf)+n);
        for(int i = n-1; i; --i) buf[i] = max(buf[2*i], buf[2*i+1]); }
    void update(int poz, const int val){
        buf[poz += n] = val;
        for(poz /= 2; poz; poz /= 2)
            buf[poz] = max(buf[2*poz], buf[2*poz+1]); }
    int query(int st, int dr){
        int r = 0;
        for(st += n, dr += n+1; st < dr; st /= 2, dr /= 2){
            if(st%2) r = max(r, buf[st++]);
            if(dr%2) r = max(r, buf[--dr]); }
        return r; } };
 
int main(){
    ipfstream f("arbint.in");
    opfstream g("arbint.out");
 
    int n, m;
    f >> n >> m;
    vector<int> v(n);
    for(auto& x : v) f >> x;
    arbint arb(v);
 
    for(int i = 0, t, a, b; i < m; ++i){
        f >> t >> a >> b;
        if(t == 0) g << arb.query(a-1, b-1) << '\n';
        else arb.update(a-1, b); }
    return 0; }