用线段树RE两个点

P1440 求m区间内的最小值

Arron_HC @ 2020-01-01 08:30:34

源码:

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,a[20000001],data[2*2000001];
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){
     if(ch=='-') w=-1;
     ch=getchar();
   }
   while(ch>='0'&&ch<='9'){
     s=s*10+ch-'0';
     ch=getchar();
   }
   return s*w;
}
inline void out(int a){
   if(a<0) putchar('-'),a/=-1;
   if(a>9) out(a/10);
   putchar(a%10+'0');
}
int build(int k,int l,int r){
    if(l==r) {
        return data[k]=a[l]; 
    } 
    int mid=(l+r)/2;
    int v1=build(k*2,l,mid);
    int v2=build(k*2+1,mid+1,r);
    return data[k]=min(v1,v2);
}
int dfs(int a,int b,int k,int l,int r){
    if(r<a||l>b) return 2147483647;
    if(a<=l&&b>=r) return data[k];
    else {
        int mid=(l+r)/2;
        int v1=dfs(a,b,k*2,l,mid);
        int v2=dfs(a,b,k*2+1,mid+1,r);
        return min(v1,v2);
    }
}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)
    a[i]=read();
    build(1,1,n);
    cout<<"0\n";
    for(int i=2;i<=n;i++){
        if(i-m<=0) out(dfs(1,i-1,1,1,n)),cout<<endl;
        else out(dfs(i-m,i-1,1,1,n)),cout<<endl;
    }
    return 0;
}

by dblark @ 2020-01-01 08:39:37

这题不是单调队列板子吗

就是卡线段树的吧


by Jia_k666 @ 2020-01-01 08:43:24

这不是得用RMQ思想吗¿


by GIFBMP @ 2020-01-01 09:05:49

这题不是单调队列倒着扫一遍做吗(


by 从蒟蒻到小犇 @ 2020-01-01 09:10:02

线段树空间开得开4倍呀……


by hly1204 @ 2020-01-01 15:24:20

我线段树吸氧过了。。。cout别用endl会刷新流


|