题目大意:
解题思路:
代码:
1 # include<iostream> 2 # include<cstdio> 3 4 using namespace std; 5 6 # define inf 99999999 7 # define MAX 1000010 8 9 struct Segtree 10 { 11 int left, right; 12 int mx, mn; 13 }tree[MAX*4]; 14 15 int a[MAX]; 16 int maxx,minx; 17 int n,k; 18 19 void build ( int id,int l,int r ) 20 { 21 tree[id].left = l; tree[id].right = r; 22 if ( l==r ) 23 { 24 int t; 25 scanf("%d",&t); 26 tree[id].mx = tree[id].mn = t; 27 return; 28 } 29 else 30 { 31 int mid = ( l+r )>>1; 32 build(id<<1,l,mid); 33 build(id<<1|1,mid+1,r); 34 tree[id].mx = max( tree[id<<1].mx,tree[id<<1|1].mx ); 35 tree[id].mn = min( tree[id<<1].mn,tree[id<<1|1].mn ); 36 } 37 38 } 39 40 41 void query_min ( int id,int l,int r ) 42 { 43 44 if ( tree[id].left==l&&tree[id].right==r ) 45 { 46 minx = min( minx,tree[id].mn ); 47 return; 48 } 49 else 50 { 51 int mid = (tree[id].left+tree[id].right)>>1; 52 if ( r<=mid ) 53 { 54 query_min(id<<1,l,r); 55 } 56 else if ( l > mid ) 57 { 58 query_min(id<<1|1,l,r); 59 } 60 else 61 { 62 query_min(id<<1,l,mid); 63 query_min(id<<1|1,mid+1,r); 64 } 65 } 66 67 } 68 69 void query_max( int id,int l,int r ) 70 { 71 72 if ( tree[id].left==l&&tree[id].right==r ) 73 { 74 maxx = max( maxx,tree[id].mx ); 75 return; 76 } 77 else 78 { 79 int mid = ( tree[id].left+tree[id].right )>>1; 80 if ( r<=mid ) 81 { 82 query_max( id<<1,l,r ); 83 } 84 else if ( l > mid ) 85 { 86 query_max( id<<1|1,l,r ); 87 } 88 else 89 { 90 query_max( id<<1,l,mid ); 91 query_max( id<<1|1,mid+1,r ); 92 } 93 } 94 95 } 96 97 98 99 int main(void) 100 { 101 while ( scanf("%d %d",&n,&k)!=EOF ) 102 { 103 int r; 104 build(1,1,n); 105 int m = n-k; 106 for ( int l = 1;l <= m;l++ ) 107 { 108 r = l+k-1; 109 minx = inf; 110 query_min(1,l,r); 111 printf("%d ",minx); 112 } 113 minx = inf; 114 query_min(1,m+1,n); 115 printf("%d ",minx); 116 117 for ( int l = 1;l <= m;l++ ) 118 { 119 r = l+k-1; 120 maxx = -inf; 121 query_max(1,l,r); 122 printf("%d ",maxx); 123 } 124 maxx = -inf; 125 query_max(1,m+1,n); 126 printf("%d ",maxx); 127 128 } 129 130 131 return 0; 132 }