1. 程式人生 > >POJ 2823 線段樹

POJ 2823 線段樹

Sliding Window
Time Limit: 12000MSMemory Limit: 65536K
Total Submissions: 68816Accepted: 19510
Case Time Limit: 5000MS

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 positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

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

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source


用線段樹維護一個區間的最大值最小值即可

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
//#define dbg(x) cout<<#x<<" = "<< (x)<< endl
const int MAX_N = 2222222;
int ans_MAX[MAX_N],ans_MIN[MAX_N];
struct node {
   int ans_max;
   int ans_min;
};
int MAX[MAX_N<<2],MIN[MAX_N<<2];
int Max(int a,int b){
   if(a>b) return a;
   else return b;
}
int Min(int a,int b){
   if(a<b) return a;
   else return b;
}
void up(int p){
   MAX[p] = Max(MAX[p*2],MAX[p*2+1]);
   MIN[p] = Min(MIN[p*2],MIN[p*2+1]);
}
void build(int p,int l,int r){
   if(l==r){
    scanf("%d",&MAX[p]);
    MIN[p] = MAX[p];
    return;
   }
   int mid = (l+r)>>1;
   build(p*2,l,mid);
   build(p*2+1,mid+1,r);
   up(p);
}
node query(int p,int l,int r,int x,int y){
   if(x<=l&&r<=y){
    node tmp;
    tmp.ans_max = MAX[p];
    tmp.ans_min = MIN[p];
    return tmp;
   }
   int mid = (l+r)>>1;
   node tmp1,tmp2,ans_tmp;
   tmp1.ans_max =tmp2.ans_max = -0x3f3f3f3f;
   tmp1.ans_min = tmp2.ans_min = 0x3f3ff3f;
   if(x<=mid){
    tmp1 = query(p*2,l,mid,x,y);
   }
   if(y>mid) tmp2 = query(p*2+1,mid+1,r,x,y);
   ans_tmp.ans_max=Max(tmp1.ans_max,tmp2.ans_max);
   ans_tmp.ans_min=Min(tmp1.ans_min,tmp2.ans_min);
   return ans_tmp;
}
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    build(1,1,n);
    int cnt = 0;
    for(int i=1;i<=n-k+1;i++){
        ans_MAX[cnt] = query(1,1,n,i,i+k-1).ans_max;
        ans_MIN[cnt] = query(1,1,n,i,i+k-1).ans_min;
        //dbg(query(1,1,n,i,i+k-1).ans_max);
        cnt++;
        //dbg(i);
    }
    for(int i=0;i<cnt;i++){
        i==cnt-1?printf("%d\n",ans_MIN[i]):printf("%d ",ans_MIN[i]);
    }
    for(int i=0;i<cnt;i++){
        i==cnt-1?printf("%d\n",ans_MAX[i]):printf("%d ",ans_MAX[i]);
    }
    return 0;
}