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 |
构造两颗线段树,时间就少点,我没有做任何优化就1.5秒,那些250ms大牛是怎么写的?In Reply To:线段树水过,就是时间有点问题,3313+MS,哪位大牛能指点一下,OrzOrzOrz Posted by:vongang at 2011-08-09 20:52:49 我这个程序怎么优化,才能降低到1秒内。 Problem: 3264 User: hujk2008 Memory: 752K Time: 1500MS Language: C Result: Accepted #include <stdio.h> #include <string.h> #define MAXLEN 50002 int d[MAXLEN] ; int fmin[MAXLEN] ; int fmax[MAXLEN] ; int q,n ; int lowbit(int x) { return x&(x^(x-1)) ; } void setfmin( int pos,int x) { while(pos <= n) { if( fmin[pos] == 0 || fmin[pos] > x) fmin[pos] = x ; pos += lowbit(pos) ; } } void setfmax(int pos , int x) { while(pos <= n) { if( fmax[pos] == 0 || fmax[pos] < x) fmax[pos] = x ; pos += lowbit(pos) ; } } int findmin(int s, int e) { int re = d[e]; int low ; while(s <= e) { low = lowbit(e) ; if( e - low >= s) { if( fmin[e] < re) re = fmin[e] ; e -= low ; }else { if( d[e] < re) re = d[e] ; e -- ; } } return re ; } int findmax(int s, int e) { int re = d[e]; int low ; while(s <= e) { low = lowbit(e) ; if( e - low >= s) { if( fmax[e] > re) re = fmax[e] ; e -= low ; }else { if( d[e] > re) re = d[e] ; e -- ; } } return re ; } int main( ) { int i ,x, y; memset(fmin,0,sizeof(fmin)) ; memset(fmax,0,sizeof(fmax)) ; scanf("%d%d",&n,&q) ; for( i = 1; i <= n ;i ++ ) { scanf("%d",&d[i]) ; setfmin(i,d[i]) ; setfmax(i,d[i]) ; } for( i = 0 ;i < q ; i ++ ) { scanf("%d%d",&x,&y) ; printf("%d\n",findmax(x,y) - findmin(x,y)) ; } return 0 ; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator