Coco_T @ 2016-12-25 20:13:11
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node{
int l,r,sum;
};
node tree[4000001];
int n,m;
int a[2000001];
int build(int x,int l,int r)
{
tree[x].l=l;
tree[x].r=r;
if (l==r)
{
tree[x].sum=a[l];
return 0;
}
build(x*2,l,(l+r)/2);
build(x*2+1,(l+r)/2+1,r);
tree[x].sum=min(tree[x*2].sum,tree[x*2+1].sum);
}
int ask(int x,int l,int r)
{
if (tree[x].l>=l&&tree[x].r<=r)
return tree[x].sum;
if (tree[x].l>r||tree[x].r<l)
return 1000000001;
// int mid=(tree[x].r+tree[x].l)/2;
// if(r<=mid) return ask(x*2,l,r);
// else if(l>mid) return ask(x*2+1,l,r);
return min(ask(x*2,l,r),ask(x*2+1,l,r));
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
printf("0\n");
for (int i=2;i<=n;i++)
{
printf("%d\n",ask(1,max(1,i-m),i-1));
}
return 0;
}
by 1124828077ccj @ 2016-12-25 20:24:46
@wutongtong ask函数错了,mid在哪?
return min(ask(x*2,l,r),ask(x*2+1,l,r));
怎么都是l,r?
by Coco_T @ 2016-12-25 20:30:30
mid部分我注释掉了,而且l,r这样写是合法的(虽然可能有些不好)- -~
by Niko @ 2017-05-17 21:40:13
我的也只有80,题解的线段树也只有80.。