1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include<cstdio> #include<algorithm> using namespace std; const int MAXN=1000005,INF=2147483647; int a[MAXN]={0},ma[MAXN]={0}; struct node{ int ma; int mi; }; node sgt[MAXN*4],t,o;
void build(int rt,int st,int ed){ if(st==ed){ sgt[rt].mi=a[st]; sgt[rt].ma=a[st]; return; } int mid=(st+ed)>>1; build(rt<<1,st,mid); build(rt<<1|1,mid+1,ed); sgt[rt].ma=max(sgt[rt<<1].ma,sgt[rt<<1|1].ma); sgt[rt].mi=min(sgt[rt<<1].mi,sgt[rt<<1|1].mi); }
node query(int rt,int nst,int ned,int qst,int qed){ if(qst>ned||qed<nst) return o; if(qst<=nst&&qed>=ned) return sgt[rt]; int mid=(nst+ned)>>1; node t1=query(rt<<1,nst,mid,qst,qed); node t2=query(rt<<1|1,mid+1,ned,qst,qed); t.ma=max(t1.ma,t2.ma); t.mi=min(t1.mi,t2.mi); return t; }
int main(){ int i,n,k; o.ma=INF+1; o.mi=INF; scanf("%d%d",&n,&k); for(i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); for(i=1;i<=n-k+1;i++){ t=query(1,1,n,i,i+k-1); printf("%d ",t.mi); ma[i]=t.ma; } printf("\n"); for(i=1;i<=n-k+1;i++) printf("%d ",ma[i]); return 0; }
|