欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj2823线段树(子区间的最值)

程序员文章站 2024-02-27 14:15:39
...

 

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.

Sample Input

<span style="color:#000000">8 3
1 3 -1 -3 5 3 6 7
</span>

Sample Output

<span style="color:#000000">-1 -3 -3 -3 3 3
3 3 5 5 6 7</span>

AC代码如下: 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=1e6+5;
struct node
{
    int left,right;
    int weight;
    int max_w,min_w;
    int getmid(){return (left+right)>>1;}
}tree[MAXN<<2];
int arr[MAXN];
int n,k,len;
int num=0;
int max_ans,min_ans;//子区间的最值
int max_arr[MAXN],min_arr[MAXN];//最值数组

void build(int rt,int l,int r)
{
    tree[rt].left=l;
    tree[rt].right=r;
    tree[rt].weight=-1;
    tree[rt].max_w=-INF;
    tree[rt].min_w=INF;
    if(l==r){
        tree[rt].weight=arr[num];
        tree[rt].max_w=arr[num];
        tree[rt].min_w=arr[num];
        num++;
        return ;
    }
    int mid = tree[rt].getmid();
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    tree[rt].weight=tree[rt<<1].weight+tree[rt<<1|1].weight;
    tree[rt].max_w=max(tree[rt<<1].max_w,tree[rt<<1|1].max_w);//从子节点中选出最值给父节点
    tree[rt].min_w=min(tree[rt<<1].min_w,tree[rt<<1|1].min_w);
}

void query(int rt,int l,int r)
{
    if(tree[rt].left>=l&&tree[rt].right<=r){
        max_ans=max(max_ans,tree[rt].max_w);
        min_ans=min(min_ans,tree[rt].min_w);
        return ;
    }
    int mid = tree[rt].getmid();
    if(l<=mid) query(rt<<1,l,r);
    if(r>=mid+1) query(rt<<1|1,l,r);
}

int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    build(1,0,n-1);
    int m=0;
    for(int i=0;i<n-k+1;i++){
        max_ans=-INF;
        min_ans=INF;
        query(1,i,i+k-1);//查询子区间
        max_arr[m]=max_ans;
        min_arr[m]=min_ans;
        m++;
    }
    for(int i=0;i<m;i++){
        printf("%d ",min_arr[i]);
        //cout<<min_arr[i]<<" ";//超时
    }
    cout<<endl;
    for(int i=0;i<m;i++){
        printf("%d ",max_arr[i]);
        //cout<<max_arr[i]<<" ";
    }
    return 0;
}

 如有不懂的小伙伴欢迎加我QQ:183799815  微信:wang183799815

上一篇: Coverage官方手册翻译

下一篇: