Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34766 | Accepted: 16368 | |
Case Time Limit: 2000MS |
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Output
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 30
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> using namespace std; typedef long long LL; const int MAX=0xfffffff; int n; const int mx=50050; int num[mx]; class node{ public: int l,r,maxv,minv; }tree[mx*4]; void pushup(int v) { tree[v].maxv=max(tree[v*2].maxv,tree[v*2+1].maxv); tree[v].minv=min(tree[v<<1].minv,tree[v<<1|1].minv); } void build(int v,int l,int r) { tree[v].l=l,tree[v].r=r; if(l==r) { tree[v].maxv=num[l]; tree[v].minv=num[l]; return ; } int mid=(l+r)/2; build(v*2,l,mid),build(v*2+1,mid+1,r); pushup(v); } int query1(int v,int l,int r) { if(l==tree[v].l&&tree[v].r==r) return tree[v].maxv; else { int mid=(tree[v].l+tree[v].r)/2; if(r<=mid) return query1(v*2,l,r); //如果不添加等号,程序无法运行 else if(l>mid) return query1(v*2+1,l,r); //如果加上=,就发生结果变化了 else return max(query1(v*2,l,mid),query1(v*2+1,mid+1,r)); } } int query2(int v,int l,int r) { if(l==tree[v].l&&tree[v].r==r) return tree[v].minv; else { int mid=(tree[v].l+tree[v].r)/2; if(r<=mid) return query2(v*2,l,r); //如果不添加等号,程序无法运行 else if(l>mid) return query2(v*2+1,l,r); //如果加上=,就发生结果变化了 else return min(query2(v*2,l,mid),query2(v*2+1,mid+1,r)); } } int main( ) { //freopen("1.txt","r",stdin); int q; while(scanf("%d %d",&n,&q)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&num[i]); build(1,1,n); while(q--) { int a,b; scanf("%d %d",&a,&b); int ma=query1(1,a,b); int mi=query2(1,a,b); printf("%d\n",ma-mi); } } return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<cmath> using namespace std; typedef long long LL; const int MAX=0xfffffff; const int mx=50050; int ma[mx][20], mi[mx][20]; int n; void RMQ_ST() { int lg=floor(log10(double(n))/log10(double(2))); for(int j=1;j<=lg;j++) for(int i=1;i<=n+1-(1<<j);i++) { ma[i][j] = max(ma[i][j-1], ma[i+(1<<(j-1))][j-1]); mi[i][j] = min(mi[i][j-1], mi[i+(1<<(j-1))][j-1]); } } int main( ) { int q; while(scanf("%d %d",&n,&q)!=EOF) { for(int i=1;i<=n;i++) { int a; scanf("%d",&a); ma[i][0]=mi[i][0]=a; } RMQ_ST(); while(q--) { int a,b; scanf("%d %d",&a,&b); if(a>b) a=a^b^(b=a); int lg=floor(log10(double(b-a+1))/log10(double(2))); printf("%d\n",max(ma[a][lg],ma[b-(1<<lg)+1][lg])-min(mi[a][lg],mi[b-(1<<lg)+1][lg])); } } }