Cod sursa(job #1046535)

Utilizator PetreFlorinaFMI Petre Florina PetreFlorina Data 3 decembrie 2013 01:34:00
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include<algorithm>
#include<fstream>
#include<math.h>
using namespace std;

int rmq[100000][20];

int main()
{  int n,i,e,m,a,b,max;
bool gasit;
    ifstream f("rmq.in");
    ofstream g("rmq.out");
    f >> n >> m;
    for ( i=0;i<n;++i)
    {
        f>>rmq[i][0];
    }
    for ( e=1;(1<<e)<n;++e)
        for (i=0;i<n;++i)
        {
            rmq[i][e]=rmq[i][e-1];
            if (i+(1<<(e-1))<n && rmq[i][e]>rmq[i+(1<<(e-1))][e-1])
                rmq[i][e]=rmq[i+(1<<(e-1))][e-1];
         }
      for (i=1;i<=m;i++)
      {
            f >> a >> b;
            --a;--b;
            gasit=false;
        if (a>b)
            swap(a,b);
        else
            if (a==b)
             {
                cout << rmq[a][0] << "\n";
                gasit = true;
             }

        int max=log2(a-b+1);

        if (!gasit)

            {
                if (rmq[a][max] < rmq[b-(1<<max)+1][max])
                  cout << rmq[a][max] << "\n";
                else cout << rmq[b-(1<<max)+1][max] << "\n";
            }
    }


    return 0;
}