poj2823 线段树模板题 点修改(也可以用单调队列)

Posted 方方的小迷妹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2823 线段树模板题 点修改(也可以用单调队列)相关的知识,希望对你有一定的参考价值。

这道题吧 没计算时间 因为给了那么多 一算还可以

就直接写了线段树,刘汝佳那本模板

然后!poj的g++比C++慢大约500ms。。。。。。。g++tle,C++就过了

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 67576   Accepted: 19163
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  -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

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<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int Max[5000100];
int Min[5000100];
int pinf = 2000000000;
int ninf = -2000000000;
int n,k;

void build(int l,int r,int cur){
    if(l == r){
        scanf("%d",&Max[cur]);
        Min[cur] = Max[cur];
        return;
    }
    int mid = (l + r)/2;
    build(l,mid,cur*2);
    build(mid + 1,r,cur*2 + 1);
    Min[cur] = min(Min[cur*2],Min[cur*2 + 1]);
    Max[cur] = max(Max[cur*2],Max[cur*2 + 1]);
    return;
}

int ql,qr;
int queryMax(int l,int r,int cur){
    if(ql <= l&&r <= qr){
        return Max[cur];
    }
    int mid = (l + r)/2;
    int ans = ninf;
    if(ql <= mid){
        ans = max(ans,queryMax(l,mid,cur*2));
    }
    if(qr > mid){
        ans = max(ans,queryMax(mid + 1,r,cur*2 +1));
    }
    return ans;
}
int queryMin(int l,int r,int cur){
    if(ql <= l && r <= qr){
        return Min[cur];
    }
    int mid = (l + r)/2;
    int ans = pinf;
    if(ql <= mid){
        ans = min(ans,queryMin(l,mid,cur*2));
    }
    if(qr > mid){
        ans = min(ans,queryMin(mid + 1,r,cur*2 + 1));
    }
    return ans;
}

int main(){
    while(scanf("%d%d",&n,&k) != EOF){
        build(1,n,1);
        ql = 1;qr = k;
        printf("%d",queryMin(1,n,1));
        ql++;qr++; 
        while(qr <= n){
            printf(" %d",queryMin(1,n,1));
            ql++;qr++;
        }
        printf("\n");
        
        ql = 1;qr = k;
        printf("%d",queryMax(1,n,1));
        ql++;qr++; 
        while(qr <= n){
            printf(" %d",queryMax(1,n,1));
            ql++;qr++;
        }
        printf("\n");
    }
    return 0;
}

 

以上是关于poj2823 线段树模板题 点修改(也可以用单调队列)的主要内容,如果未能解决你的问题,请参考以下文章

Sliding Window POJ - 2823 单调队列模板题

POJ 2823 Sliding Window(单调队列)

Sliding Window

poj 2823 线段树

线段树模板加模板题POJ3468

(树链剖分+线段树)POJ - 3237 Tree