TheSambra @ 2018-06-14 20:41:25
还得氧气优化,不然TLE...
#include<bits/stdc++.h>
#define ls(x) x<<1
#define rs(x) x<<1|1
#define m(a,b) (a+b)>>1
using namespace std;
int base[1000010];
int maxx[2000010],minn[2000010];
int ans1[2000010],ans2[2000010];
void push_up(int n)
{
maxx[n]=max(maxx[ls(n)],maxx[rs(n)]);
minn[n]=min(minn[ls(n)],minn[rs(n)]);
return ;
}
void build(int n,int l,int r)
{
if(l==r)
{
maxx[n]=minn[n]=base[l];
return ;
}
int mid=m(l,r);
build(ls(n),l,mid);
build(rs(n),mid+1,r);
push_up(n);
return ;
}
int check1(int n,int nl,int nr,int al,int ar)
{
if(nl>ar||nr<al)
return -2147483647;
if(nl>=al&&nr<=ar)
return maxx[n];
int mid=m(nl,nr);
return max(check1(ls(n),nl,mid,al,ar),check1(rs(n),mid+1,nr,al,ar));
}
int check2(int n,int nl,int nr,int al,int ar)
{
if(nl>ar||nr<al)
return 2147483647;
if(nl>=al&&nr<=ar)
return minn[n];
int mid=m(nl,nr);
return min(check2(ls(n),nl,mid,al,ar),check2(rs(n),mid+1,nr,al,ar));
}
int main()
{
ios::sync_with_stdio(false);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>base[i];
if(k==1)
{
for(int i=1;i<=n;i++)
cout<<base[i]<<" ";
cout<<endl;
for(int i=1;i<=n;i++)
cout<<base[i]<<" ";
return 0;
}
build(1,1,n);
int l=1,r=l+k-1;
for(;r<=n;l++,r++)
{
ans2[l]=check1(1,1,n,l,r);
ans1[l]=check2(1,1,n,l,r);
}
for(int i=1;i<=n-k+1;i++)
cout<<ans1[i]<<" ";
cout<<endl;
for(int i=1;i<=n-k+1;i++)
cout<<ans2[i]<<" ";
return 0;
}
by tocek_shiki @ 2018-06-14 21:37:20
哇巨佬啊,还会线段树这么高级的东东
by Altria_Pendragon_ @ 2018-06-14 21:59:40
@SOLDIER_76 单调队列了解一下
by Victorique @ 2018-06-14 21:59:49
正常