Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这题目做什么优化可以缩短时间这是我的代码,时间3300ms,想问一下有什么办法可以缩短时间? #include <stdio.h> #define M 200003 const int inf = 0x13131313; int c[M],themin,themax; int MIN(int a,int b){return a<b?a:b;} int MAX(int a,int b){return a>b?a:b;} struct node { int min,max; int low,high; }q[2*M]; void build(int low,int high,int index) { q[index].low=low,q[index].high=high; if(low==high) { q[index].min=q[index].max=c[low]; return ; } int next=index*2,mid; mid=(low+high)/2; build(low,mid,next); build(mid+1,high,next+1); q[index].min=MIN(q[next].min,q[next+1].min); q[index].max=MAX(q[next].max,q[next+1].max); } void query(int low,int high,int index) { if(q[index].low==low&&q[index].high==high) { themin=MIN(themin,q[index].min); themax=MAX(themax,q[index].max); return ; } int mid=(q[index].low+q[index].high)/2,next=index*2; if(mid>=high) { query(low,high,next); } else { if(low>mid) { query(low,high,next+1); } else { query(low,mid,next); query(mid+1,high,next+1); } } } int main() { int i,n,Q,a,b; scanf("%d %d",&n,&Q); for(i=1;i<=n;i++) scanf("%d",&c[i]); build(1,n,1); for(i=0;i<Q;i++) { scanf("%d %d",&a,&b); themin=inf,themax=0; query(a,b,1); printf("%d\n",themax-themin); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator