All pastes #140774 Raw Edit

Rave Hanker

public cpp v1 · immutable
#140774 ·published 2006-08-20 17:26 UTC
rendered paste body
#include <iostream>#include <vector>using namespace std;#define DBG#ifdef DBG#define PDBG(X) cout<<X<<endl;#else#define PDBG(X)#endifvector<double> *Tournament;long int n,      	TreeDepth;inline double min (double a, double b) {	return a<b?a:b;}long int log2(long int num){    unsigned long int prod = 1;    long i;    for(i = 0 ;  ; i++) {            if(prod >= (unsigned long int)num)                break;            prod <<= 1;    }    return i;}long int nextPow2 (long int num){	long int ans = 1;	while (ans<num)		ans<<=1;	return ans;}double minInRange (long int l, long int r, long int lo, long int hi, long int ti, long int tj){	PDBG ("l :"<<l<<" r :"<<r<<" lo :"<<lo<<" hi :"<<hi<<" ti :"<<ti<<" tj :"<<tj);	if ( l == r)		return Tournament[0][l];	if (l == lo && r == hi)		return Tournament[ti][tj];	long int mid = (lo+hi)/2;		if ( lo<= l && r <= mid)		return minInRange (l, r, lo, mid, ti-1, tj<<1);	else if ( mid< l && r<= hi)		return minInRange (l, r, mid+1, hi, ti-1, (tj<<1)+1 );	else 		return min ( minInRange( l, mid, lo, mid, ti-1, tj<<1), minInRange ( mid+1, r, mid+1, hi, ti-1, (tj<<1)+1));	return 0;}int main(){	long int i, tsz;	cin>>n;	vector<double> tournament[log2(n)+1];	Tournament = &tournament[0];	for ( i=0; i<n; i++) {		double tmp;		cin>>tmp;		tournament[0].push_back(tmp);	}	for (i=0, tsz=n; tsz>2; tsz=(tsz+1)/2, i++) {		for (int j=1; j<tsz; j+=2) {			tournament[i+1].push_back( min ( tournament[i][j-1], tournament[i][j]) );		}		if ( tsz%2 == 1 ) {			tournament[i+1].push_back( tournament[i][tsz-1]);		}	}	tournament[i+1].push_back( min ( tournament[i][1], tournament[i][0]) );	TreeDepth = i+1;	PDBG("TreeDepth :"<<TreeDepth);#ifdef DBG	for (i=0, tsz=n; tsz>1; tsz= (tsz==1?0:(tsz+1)/2) , i++) {		for (int j=0; j<tournament[i].size(); j++) {			cout<<tournament[i][j]<<" ";		}		cout<<endl;	}	cout<<tournament[i][0]<<endl;#endif		long int m;	cin>>m;	while(m--) {		long int l, r;		cin>>l>>r;		cout<<minInRange(l, r-1, 0, nextPow2(n)-1, TreeDepth, 0)<<endl;	}	return 0;}