线段树根本过不了!!!

P1440 求m区间内的最小值

czy0323 @ 2022-08-07 12:54:30

用线段树只有80分

附上代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e6;
int tree[4*MAXN];
int i,n,m,tl,tr;

inline int read(){
    char ch=getchar();
    int s=0,f=1;
    while( ch<'0' || ch>'9' ){
        if( ch=='-' )
            f=-1;
        ch=getchar();
    }
    while( ch>='0' && ch<='9' ){
        s=s*10+ch-48;
        ch=getchar();
    }
    return s*f;
}

void build(int l,int r,int p){
    if( l==r ){
        tree[p]=read();
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,p*2);
    build(mid+1,r,p*2+1);
    tree[p]=min(tree[p*2],tree[p*2+1]);
}

int query(int nl,int nr,int p){
    if( nl>=tl && nr<=tr )
        return tree[p];
    int ans=1e9,mid=(nl+nr)/2;
    if( mid>=tl )
        ans=min(ans,query(nl,mid,p*2));
    if( mid<tr )
        ans=min(ans,query(mid+1,nr,p*2+1));
    return ans;
}

int main() {
    n=read(),m=read();
    build(1,n,1);
    cout<<0<<endl;
    for(i=2;i<=n;i++){
        if( i<=m )
            tl=1,tr=i-1;
        else
            tl=i-m,tr=i-1;
        cout<<query(1,n,1)<<endl;
    }
    return 0;
}

by 崔化博 @ 2022-08-07 12:55:40

用printf试试


by OoXiao_QioO @ 2022-08-07 12:56:40

不吸氧也能过啊


by OoXiao_QioO @ 2022-08-07 12:57:08

对对,用printf@czy0323


by xyf007 @ 2022-08-07 12:59:34

关同步用 std::cout << '\n'


by Micnation_AFO @ 2022-08-07 13:23:19

endl; 太慢


by czy0323 @ 2022-08-07 13:27:07

@崔化博 好的,我试试


by czy0323 @ 2022-08-07 13:31:17

@崔化博 过了,谢谢大佬的帮助


by czy0323 @ 2022-08-07 13:32:46

@xyf007 @OoXiao_QioO @Leap_hash_jperm 已过,谢谢各位大佬


by ocean2th8 @ 2022-08-08 22:09:36

嘿,突击检查


by Ly_boy @ 2022-10-29 11:03:57

你看看这个代码呢!


/*
    @filename: 区间最小值-线段树做法
    @author: sww
    @date: 2022-10-26 15:41:58  星期三
    @version: V1.0.0
*/
#include <iostream>
#define maxn 2000005
#define lc k << 1
#define rc k << 1 | 1
using namespace std;

int read()
{
    int x = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * w;
}

struct node
{
    int l, r;
    int date;
} tree[maxn * 4];
int a[maxn], n, m, x, y;

void build(int k, int l, int r)
{
    if (l > r)
        return;
    tree[k].l = l;
    tree[k].r = r;
    if (l == r){
        tree[k].date = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lc, l, mid);
    build(rc, mid + 1, r);
    tree[k].date = min(tree[lc].date, tree[rc].date);
}

int find(int k, int l, int r)
{
    if (l <= tree[k].l && r >= tree[k].r)
        return tree[k].date;
    int mid = (tree[k].l + tree[k].r) >> 1;
    int Min = 0x3f3f3f3f;
    if (l <= mid)
        Min = min(Min, find(lc, l, r));
    if (r > mid)
        Min = min(Min, find(rc, l, r));
    return Min;
}

int main()
{
    n = read(), m = read();
    for (int i = 1; i <= n; i++)
        a[i] = read();
    a[0] = 0x3f3f3f3f;
    build(1,1,n);
    for (int i = 1; i <= n; i++)
    {
        x = i - m, y = i - 1;
        if (x < 0) //如果左端点超过了,则将左端点设为1
            x = 1;
        if (x > y)//如果左端点比右端点还大
        {
            printf("%d\n", 0);
            continue;
        }
        printf("%d\n", find(1, x, y));
    }
    return 0;
}

|