Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴个线段树,有点丑,7200ms,普通线段树就可以过,不必优化#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> using namespace std; const int inf=0x3f3f3f3f; const int M=1111111; struct node { int minn,maxn; int l,r; }seg[M*8]; int f[M]; void build(int u,int l,int r) { seg[u].minn=inf,seg[u].maxn=0; seg[u].l=l; seg[u].r=r; if(l==r){ f[l]=u; return ; } int mid=(l+r)>>1; build(u*2,l,mid); build(u*2+1,mid+1,r); } int maxn[M],minn[M]; void up(int u,int val) { if(u==1)return ; int dad=u>>1; seg[dad].maxn=max(seg[dad*2].maxn,seg[dad*2+1].maxn); seg[dad].minn=min(seg[dad*2].minn,seg[dad*2+1].minn); up(dad,val); } void Q(int u,int l,int r,int i) { if(l<=seg[u].l&&r>=seg[u].r){ maxn[i]=max(maxn[i],seg[u].maxn); minn[i]=min(minn[i],seg[u].minn); } else { int mid=(seg[u].l+seg[u].r)/2; if(r<=mid)Q(u*2,l,r,i); else if(l>mid)Q(u*2+1,l,r,i); else { Q(u*2,l,mid,i); Q(u*2+1,mid+1,r,i); } } } int main() { int x; int n,k; cin>>n>>k; build(1,1,n); for(int i=0;i<n;i++) { minn[i]=inf; maxn[i]=-inf; } for(int i=1;i<=n;i++) { scanf("%d",&x); seg[f[i]].minn=seg[f[i]].maxn=x; up(f[i],x); } for(int i=k;i<=n;i++) Q(1,i-k+1,i,i-k); for(int i=0;i<=n-k;i++) printf("%d ",minn[i]); printf("\n"); for(int i=0;i<=n-k;i++) printf("%d ",maxn[i]); system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator