Cod sursa(job #2922609)

Utilizator EasyTnsEasyTns EasyTns Data 9 septembrie 2022 09:21:48
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.75 kb
	#include<vector>
#include<fstream>
#include<iostream>
using namespace std;

class InParser {
    vector<char> str;
    int ptr;
    ifstream fin;
 
    char getChar() {
        if (ptr == int(str.size())) {
            fin.read(str.data(), str.size());
            ptr = 0;
        }
        return str[ptr++];
    }
 
    template<class T>
    T getInt() {
        char chr = getChar();
        while (!isdigit(chr) && chr != '-')
            chr = getChar();
        int sgn = +1;
        if (chr == '-') {
            sgn = -1;
            chr = getChar();
        }
        T num = 0;
        while (isdigit(chr)) {
            num = num * 10 + chr - '0';
            chr = getChar();
        }
        return sgn * num;
    }
 
public:
    InParser(const char* name) : str(1e5), ptr(str.size()), fin(name) { }
    ~InParser() { fin.close(); }
 
    template<class T>
    friend InParser& operator>>(InParser& in, T& num) {
        num = in.getInt<T>();
        return in;
    }
};
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 << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }
 
    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
int a[18][100002],b[100002];
int n,m,i,j,p, x,y,e,lung;
int main()
{
    
    InParser fin("rmq.in");
    OutParser fout("rmq.out");
 
    fin>>n>>m;
    for( i=1;i<=n;i++)
        fin>>a[0][i];
 
    for(  p=1;(1<<p)<=n;p++)
        for( i=1;i<=n;i++)
        {
            a[p][i]=a[p-1][i];
            j=i+(1<<(p-1));
            if(j<=n&&a[p][i]>a[p-1][j])
                a[p][i]=a[p-1][j];
        }
    b[1]=0;
    for( i=2;i<=n;i++)
        b[i]=1+b[i/2];
 
    for( i=1;i<=m;i++)
    {
        fin>>x>>y;
        e=b[y-x+1];
        lung=(1<<e);
        fout<<min(a[e][x],a[e][y-lung+1])<<'\n';
    }
 
}