Created
October 22, 2016 08:56
-
-
Save tina1998612/31339dac4a81ba20934f81813c65fb45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include <bits/stdc++.h> | |
#include <stdio.h> | |
#include <cstdlib> | |
#include <cmath> | |
#include <algorithm> | |
#include <iostream> | |
#define PI 3.14159265 | |
#define N 300+5 | |
#define ll long long | |
#define INF 2147483647 | |
using namespace std; | |
int n,a[N],dp[2*N],num,k,mid,l,r; | |
void init(int n){ | |
num=1; | |
while(num<n) num*=2; | |
for(int i=0;i<num-1;i++) dp[i]=INF; | |
for(int i=num-1;i<(num-1)+num;i++) dp[i]=a[i-(num-1)]; | |
for(int k=num-2;k>=0;k--) | |
dp[k]=min(dp[(1<<k)+1],dp[(1<<k)+2]); | |
} | |
int RMQ(int k,int ql,int qr,int l,int r){ | |
if(r<ql || qr<l) return INF; | |
if(ql<=l && r<=qr) return dp[k]; | |
mid=(l+r)/2;//cout<<l<<" "<<mid<<", "<<k*2+2<<" "<<mid+1<<" "<<r<<endl; | |
return min(RMQ(k*2+1,ql,qr,l,mid),RMQ(k*2+2,ql,qr,mid+1,r)); | |
} | |
int main(){ | |
scanf("%d",&n); | |
for(int i=0;i<n;i++) scanf("%d",&a[i]); | |
init(n); | |
while(scanf("%d%d",&l,&r)) printf("%d\n",RMQ(0,l,r,0,num-1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment