线段树 tle*3 HELP!

P1886 滑动窗口 /【模板】单调队列

长颈鹿 @ 2017-08-12 16:50:19

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000001;
int tree1[maxn*4];
int tree2[maxn*4]; 
int a[maxn];
int n,k;
int pp1[maxn];
int pp2[maxn];
inline int read()
{
    int out=0,flag=1;char c=getchar();
    while(c<48||c>57) {if(c=='-') flag=-1;c=getchar();}
    while(c>=48&&c<=57) {out=out*10+c-48;c=getchar();}
    return out*flag;
}
inline void maketree(int node,int l,int r){
    if(l==r){
        tree1[node]=a[l];
        tree2[node]=a[l];
        return ;    }
    int mid=l+(r-l)/2;
    maketree(node*2,l,mid);
    maketree(node*2+1,mid+1,r);
    tree1[node]=min(tree1[node*2],tree1[node*2+1]);
    tree2[node]=max(tree2[node*2],tree2[node*2+1]);
}
long long find1(int node,int l,int r,int x,int y){
    if(x==l&&y==r){
        return tree1[node];
    }
    int mid=l+(r-l)/2;
    if(y<=mid)return find1(node*2,l,mid,x,y);
    if(x>mid)return find1(node*2+1,mid+1,r,x,y);
    return min(find1(node*2,l,mid,x,mid),find1(node*2+1,mid+1,r,mid+1,y));
}
long long find2(int node,int l,int r,int x,int y){
    if(x==l&&y==r){
        return tree2[node];
    }
    int mid=l+(r-l)/2;
    if(y<=mid)return find2(node*2,l,mid,x,y);
    if(x>mid)return find2(node*2+1,mid+1,r,x,y);
    return max(find2(node*2,l,mid,x,mid),find2(node*2+1,mid+1,r,mid+1,y));
}
int main(){
    n=read();
    k=read();
    k--;
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    maketree(1,1,n);
    int cc=0;
    for(int i=1;i<=n-k;i++){
        pp1[++cc]=find1(1,1,n,i,i+k);
        pp2[cc]=find2(1,1,n,i,i+k);
    }
    for(int i=1;i<=cc;i++){
        cout<<pp1[i]<<" ";
    }
    cout<<endl;
    for(int i=1;i<=cc;i++){
        cout<<pp2[i]<<" ";
    }
    return 0;
}

by 夏色祭 @ 2017-09-08 17:36:50

我也是啊。。。。


by 夏色祭 @ 2017-09-08 17:37:22

单调队列AC,线段树70。。。


|