Cod sursa(job #1863379)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 30 ianuarie 2017 21:07:01
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.13 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){
        rhs = 0;
        while(*p < '0') adv();
        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){
            const int tmp_ = x/10;
            *tmp++ = x - 10 * tmp_ + '0';
            x = tmp_; }
        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;
        int ppoz = poz;
        for(poz >>= 1; poz; poz >>= 1, ppoz >>= 1)
            buf[poz] = max(buf[ppoz], buf[ppoz^1]); }
    int query(int st, int dr){
        int r = 0;
        for(st += n, dr += n, ++dr; st < dr; st >>= 1, dr >>= 1){
            if(st&1) r = max(r, buf[st++]);
            if(dr&1) 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; }