Luogu4085 [USACO17DEC]Haybale Feast (线段树,单调队列)

\(10^18\)是要long long的。
\(nlogn\)单调队列上维护\(logn\)线段树。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 100007;

int n;

#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
int t[N << 2];
inline void Pushup(int rt){
    t[rt] = Max(t[rt << 1], t[rt << 1 | 1]);
}
inline void Updata(int rt, int l, int r, int x, int w){
    if(l == r){
        t[rt] = w;
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid)
        Updata(lson, x, w);
    else
        Updata(rson, x, w);
    Pushup(rt);
}
inline int Query(int rt, int l, int r, int L, int R){
    if(L <= l && r <= R){
        return t[rt];
    }
    int mid = (l + r) >> 1, maxx = 0;
    if(L <= mid) maxx = max(maxx, Query(lson, L, R));
    if(R > mid)  maxx = max(maxx, Query(rson, L, R));
    return maxx;
}

long long sum[N];

int main(){
//FileOpen();
    long long Val;
    io >> n >> Val;
    R(i,1,n){
        int x, y;
        io >> x >> y;
        sum[i] = sum[i - 1] + x;
        Updata(1, 1, n, i, y);
    }
    
    int ans  = 2147483647;
    int r = 1;
    R(l,1,n){
        while(r <= n && sum[r] - sum[l - 1] < Val) ++r;
        if(r > n) break;
        int newAns = Query(1, 1, n, l, r);
        ans = Min(ans, newAns);
    }
    
    printf("%d", ans);
    
    return 0;
}
上一篇:bzoj 3309 DZY Loves Math


下一篇:AC自动机模板题