Cod sursa(job #997891)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 15 septembrie 2013 01:18:28
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.58 kb
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
   
const string file = "arbint";
   
const string infile = file + ".in";
const string outfile = file + ".out";
 
 
 
template <class T>
class SegmentTree
{
public:
    SegmentTree(int low, int hi);
    void Insert(int low, int hi, const T& data);
    T Query(int low, int hi);
protected:
private:
 
    void Insert(int nod, int st, int dr, int low, int hi, const T& data);
    T Query(int nod, int st, int dr, int low, int hi);
 
    int _hi;
    int _low;
    vector<T> _data;
};
 
template<class T>
SegmentTree<T>::SegmentTree(int low, int hi)
{
    _hi = hi;
    _low = low;
    int count = hi - low + 1;
    int c = 1;
    while(c < count) c <<= 1;
    c <<= 1;
    _data.resize(c + 1);
}
 
template<class T>
void SegmentTree<T>::Insert(int low, int hi, const T& data)
{
    Insert(1, _low, _hi, low, hi, data);
}
 
template<class T>
T SegmentTree<T>::Query(int low, int hi)
{
    return Query(1, _low, _hi, low, hi);
}
 
template<class T>
void SegmentTree<T>::Insert(int nod, int st, int dr, int low, int hi, const T& data)
{
    if(low <= st && dr <= hi)
    {
        _data[nod] = data;
    }
    else
    {
        int mid = (dr + st) >> 1;
        if(low <= mid)
            Insert(nod << 1 , st, mid, low, hi, data);
        if(mid < hi)
            Insert((nod << 1) + 1, mid + 1, dr, low, hi, data);
        _data[nod] = max(_data[nod << 1], _data[(nod << 1) + 1]);
    }
}
 
template<class T>
T SegmentTree<T>::Query(int nod, int st, int dr, int low, int hi)
{
    if(low <= st && dr <= hi)
    {
        return _data[nod];
    }
    else
    {
        int mid = (dr + st) >> 1;
        int result = 0;
        if(low <= mid)
            result = max(result, Query(nod << 1, st, mid, low, hi));
        if(mid < hi)
            result = max(result, Query((nod << 1) + 1, mid + 1, dr, low, hi));
        return result;
    }
}
 
const int bufSize = 4000000;
 
char *cursor;
int size;
char output[bufSize];
char *input;
 
 
 
int nextInt()
{
    char * p;
    int result = strtol(cursor, &p, 10);
    cursor = p;
    return result;
}
 
int main()
{
     
    fstream fin(infile.c_str(), ios::in | ios::binary);
    fin.seekg(0, ios::end);
    int s = (int)fin.tellg();
    input = new char[s];
    fin.seekg(0, ios::beg);
    fin.read(input, s);
    cursor = input;
    fin.close();
     
 
    int N, M;
 
    N = nextInt();
    M = nextInt();
 
    SegmentTree<int> tree(1, N);
 
    for(int i = 0; i < N; i++)
    {
        int x;
        x = nextInt();
        tree.Insert(i+1, i+1, x);
    }
     
    fstream fout(outfile.c_str(), ios::out);
 
    for(int i = 0; i < M; i++)
    {
        int op, a, b;
        op = nextInt();
        a = nextInt();
        b = nextInt();
 
        if(op == 0)
        {
            int added = sprintf(output + size, "%d\n", tree.Query(a, b));
            size += added;
        }
        else if(op == 1)
        {
            tree.Insert(a, a, b);
        }
    }
 
    fout << output;
 
    fout.close();
    delete [] input;
 
 
}