Pagini recente » Cod sursa (job #1512434) | Cod sursa (job #629311) | Cod sursa (job #1283689) | Cod sursa (job #2980314) | Cod sursa (job #1250391)
#include<fstream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
const int nmax = 100001;
class Rmq
{
int dimension, logDimension;
vector<vector<int>> prmq;
public:
Rmq(int v[], int dimension);
int getMinimum(int x,int y,int* v);
};
Rmq::Rmq(int v[], int dimension) : prmq(dimension)
{
int i,j;
this->dimension = dimension;
int ldm = log2(dimension)+1;
this->logDimension = ldm;
for(i=0;i<dimension;i++)
{
prmq[i].resize(ldm); //here is the second dimension initialization
prmq[i][0] = i;
}
for(j=1; 1<<j <=dimension; j++)
for(i=0; i+(1<<j)-1 < dimension; i++)
{
if(v[prmq[i][j-1]]<v[prmq[i+(1<<(j-1))][j-1]])
prmq[i][j]=prmq[i][j-1];
else
prmq[i][j]=prmq[i+(1<<(j-1))][j-1];
}
}
int Rmq::getMinimum(int x,int y, int attachedVector[])
{
int loga;
loga=log2(y-x+1);
if(attachedVector[prmq[x][loga]] < attachedVector[prmq[y-(1<<loga)+1][loga]])
return attachedVector[prmq[x][loga]];
return attachedVector[prmq[y-(1<<loga)+1][loga]];
}
int main()
{
int n, m, v[nmax], i, x, y;
f>>n>>m;
for(i=0;i<n;i++){
f>>v[i];
}
Rmq rmq(v,n);
for(i=1;i<=m;i++){
f>>x>>y;
g<<rmq.getMinimum(x-1, y-1, v)<<'\n';
}
f.close();g.close();
return 0;
}