【题解】
线段树或者单调队列都可以。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define LL long long 5 #define rg register 6 #define N 1000010 7 #define ls (u<<1) 8 #define rs (u<<1|1) 9 #define mid ((a[u].l+a[u].r)>>1) 10 using namespace std; 11 int n,m; 12 struct tree{ 13 int l,r,mx,mn; 14 }a[N<<2]; 15 inline int read(){ 16 int k=0,f=1; char c=getchar(); 17 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 18 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 19 return k*f; 20 } 21 void build(int u,int l,int r){ 22 a[u].l=l; a[u].r=r; 23 if(l<r){ 24 build(ls,l,mid),build(rs,mid+1,r); 25 a[u].mx=max(a[ls].mx,a[rs].mx); 26 a[u].mn=min(a[ls].mn,a[rs].mn); 27 } 28 else a[u].mx=a[u].mn=read(); 29 } 30 int query(int u,int l,int r,int type){ 31 if(l<=a[u].l&&a[u].r<=r) return type?a[u].mx:a[u].mn; 32 if(type){ 33 int ret=-1e9; 34 if(l<=mid) ret=max(ret,query(ls,l,r,type)); 35 if(r>mid) ret=max(ret,query(rs,l,r,type)); 36 return ret; 37 } 38 else{ 39 int ret=1e9; 40 if(l<=mid) ret=min(ret,query(ls,l,r,type)); 41 if(r>mid) ret=min(ret,query(rs,l,r,type)); 42 return ret; 43 } 44 } 45 int main(){ 46 n=read(); m=read(); build(1,1,n); 47 for(rg int i=1;i<=n-m+1;i++) printf("%d ",query(1,i,i+m-1,0));//min 48 puts(""); 49 for(rg int i=1;i<=n-m+1;i++) printf("%d ",query(1,i,i+m-1,1));//max 50 return 0; 51 }