线段树wa*2求助

P1886 滑动窗口 /【模板】单调队列

lhcpzxgm @ 2018-04-13 21:16:30

include <iostream>

include <cstdio>

define maxn 1000010

using namespace std; int n,k; int a[maxn],maxxt[maxn<<2],minnt[maxn<<2]; int mxans[maxn],mians[maxn],maxx,minn; inline int read() { int x=0,f=1; char ch; while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-'); if(ch=='-') f=-1; else x=ch-'0'; while(ch=getchar(),ch>='0'&&ch<='9') x=x10+ch-'0'; return xf; } inline void up(int rt) { maxxt[rt]=max(maxxt[rt<<1],maxxt[rt<<1|1]); minnt[rt]=min(minnt[rt<<1],minnt[rt<<1|1]); } inline void build(int l,int r,int rt) { if(l==r) { maxxt[rt]=a[l]; minnt[rt]=a[l]; return; } int mid=l+r>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); up(rt); } inline void query(int l,int r,int L,int R,int rt) { if(L<=l&&r<=R) { maxx=max(maxx,maxxt[rt]); minn=min(minn,minnt[rt]); return; } int mid=(l+r)>>1; if(L<=mid) query(l,mid,L,R,rt<<1); if(mid<R) query(mid+1,r,L,R,rt<<1|1); } int main() { n=read(),k=read(); for(register int i=1;i<=n;i++) a[i]=read(); if(k==1) { for(register int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; for(register int i=1;i<=n;i++) cout<<a[i]<<" "; return 0; } build(1,n,1); for(register int i=1;i<=n-k+1;i++) { maxx=-0x7fffff,minn=0x7fffff; query(1,n,i,i+k-1,1); mxans[i]=maxx,mians[i]=minn; } for(register int i=1;i<=n-k+1;i++) cout<<mians[i]<<" "; cout<<endl; for(register int i=1;i<=n-k+1;i++) cout<<mxans[i]<<" "; return 0; }


by AThousandSuns @ 2018-04-13 21:27:11

请使用

```cpp

```

把代码括住


by ViXbob @ 2018-04-13 22:42:56

@AThousandSuns 不用加cpp


|