poj3264 st表+输入输出挂

来源:互联网 发布:淘宝以旧换新主板骗局 编辑:程序博客网 时间:2024/05/19 06:46

题目:

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 50313 Accepted: 23562Case 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

Line 1: Two space-separated integers, N and Q
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 31734251 54 62 2

Sample Output

630

Source

USACO 2007 January Silver


思路: 

rmq问题,由于查询次数多,采用O(nlogn)+O(1)的st算法。


代码:

开挂前 3391ms

#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h>    //tower()#include<set>  #include<map>  #include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector>   #include<time.h>  #include<assert.h>  //assert#include<cmath>#include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=50010;const int maxm=1001;const int inf=INT_MAX;int n,q,a[maxn],st[maxn][16],sti[maxn][16];void initrmq(){//记录最小值 for(int i=0;i<n;++i) st[i][0]=sti[i][0]=a[i];for(int j=1;(1<<j)<=n;++j){for(int i=0;i+(1<<j)-1<n;++i){//i+2^j-1<n,st[i][j]才有意义 st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);sti[i][j]=max(sti[i][j-1],sti[i+(1<<(j-1))][j-1]);}}}int rmq(int u,int v){int k=(int)(log(v-u+1.0)/log(2.0));//向下取整并转换成int,功能同floor(),保证u+1<<k落在[u,v]内。ceil()向上取整 return max(sti[u][k],sti[v-(1<<k)+1][k])-min(st[u][k],st[v-(1<<k)+1][k]);}int main(){//7164K3391MSint c,d;while(~scanf("%d%d",&n,&q)){for(int i=0;i<n;++i){scanf("%d",&a[i]);}initrmq();while(q--){scanf("%d%d",&c,&d);printf("%d\n",rmq(c-1,d-1));}}return 0;}



开挂后 750ms

#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<ctype.h>    //tower()#include<set>  #include<map>  #include<iomanip>// cout<<setprecision(1)<<fixed<<a;#include<vector>   #include<time.h>  #include<assert.h>  //assert#include<cmath>#include<algorithm>#include<bitset>#include<limits.h>#include<stack>#include<queue>using namespace std;const int maxn=50010;const int maxm=1001;const int inf=INT_MAX;int n,q,a[maxn],st[maxn][16],sti[maxn][16];void initrmq(){for(int i=0;i<n;++i) st[i][0]=sti[i][0]=a[i];for(int j=1;(1<<j)<=n;++j){for(int i=0;i+(1<<j)-1<n;++i){st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);sti[i][j]=max(sti[i][j-1],sti[i+(1<<(j-1))][j-1]);}}}int rmq(int u,int v){int k=(int)(log(v-u+1.0)/log(2.0));return max(sti[u][k],sti[v-(1<<k)+1][k])-min(st[u][k],st[v-(1<<k)+1][k]);}void In(int &x){char c;x=0;c=getchar();int sign=1;while(!(c>='0'&&c<='9'||c=='-')) c=getchar();if(c=='-') sign=-1,c=getchar();while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}x*=sign;}void Out(int x){if(x<0){x=-x;putchar('-');}if(x>9) Out(x/10);putchar(x%10+'0');}int main(){//7152K750MSint c,d;while(~scanf("%d%d",&n,&q)){for(int i=0;i<n;++i){In(a[i]);}initrmq();while(q--){In(c); In(d);Out(rmq(c-1,d-1));putchar('\n');//最后才能输出换行 }}return 0;}




0 0
原创粉丝点击