码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-22 22:20:03      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:uri   second   task   ret   val   esc   term   ali   sub   

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

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

然后!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 线段树模板题 点修改(也可以用单调队列)

标签:uri   second   task   ret   val   esc   term   ali   sub   

原文地址:https://www.cnblogs.com/xuyanqd/p/9073920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!