richardchen @ 2017-11-08 22:30:37
#include<cstdio>
#include<algorithm>
#define M 1000000
using namespace std;
int n,k,a[M],size;
struct SegmentTree{
int Max,Min;
}tree[M<<2];
void build(int tmp)
{
size=1;
while(size<tmp+2) size<<=1;
for(int i=size+1;i<=size+n;i++) tree[i].Max=tree[i].Min=a[i-size];
for(int i=size-1;i>0;i--)
{
tree[i].Max=max(tree[i<<1].Max,tree[i<<1|1].Max);
tree[i].Min=min(tree[i<<1].Min,tree[i<<1|1].Min);
}
}
int query(int l,int r)
{
int ret=-0x7fffffff-1;
for(l=l+size-1,r=r+size+1;l^r^1;l>>=1,r>>=1)
{
if(~l&1) ret=max(ret,tree[l^1].Max);
if(r&1) ret=max(ret,tree[r^1].Max);
}
return ret;
}
int Query(int L,int R)
{
int Ret=0x7fffffff;
for(L=L+size-1,R=R+size+1;L^R^1;L>>=1,R>>=1)
{
if(~L&1) Ret=min(Ret,tree[L^1].Min);
if(R&1) Ret=min(Ret,tree[R^1].Min);
}
return Ret;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(n);
for(int i=1;i<=n-k+1;i++) printf("%d ",Query(i,i+k-1));
putchar('\n');
for(int i=1;i<=n-k+1;i++) printf("%d ",query(i,i+k-1));
return 0;
}