zoukankan      html  css  js  c++  java
  • LuoguP1937 [USACO10MAR]Barn Allocation G

    LuoguP1937 [USACO10MAR]Barn Allocation G

    题目描述

    链接

    Solution

    贪心的同时开个线段树判断一下可不可以放

    #include<bits/stdc++.h>
    
    using namespace std;
    
    inline int read()
    {
        int f = 1,x = 0;
        char ch;
        do
        {
            ch = getchar();
            if(ch == '-') f = -1;
        }while(ch<'0'||ch>'9');
        do
        {
            x = (x<<3) + (x<<1) + ch - '0';
            ch = getchar();
        }while(ch>='0'&&ch<='9');
        return f*x;
    }
    
    const int MAXN = 200000 + 10;
    
    int n,m;
    int a[MAXN<<2];
    int T[MAXN<<2]; 
    int add[MAXN<<2];
    
    #define lc o<<1
    #define rc o<<1|1
    
    inline void build(int o,int l,int r)
    {
        if(l == r)
        {
            T[o] = a[l];
            return;
        }
        int mid = (l + r) >> 1;
        build(lc,l,mid);
        build(rc,mid+1,r);
        T[o] = min(T[lc],T[rc]);
        return;
    }
    
    inline void pushdown(int o,int l,int r)
    {
        int mid = (l + r) >> 1;
        add[lc] += add[o];
        add[rc] += add[o];
        T[lc] += add[o];
        T[rc] += add[o];
        add[o] = 0;
        return;
    }
    
    inline void update(int o,int L,int R,int l,int r,int v)
    {
        if(L<=l&&r<=R) 
        {
            T[o] +=  v;
            add[o] += v;
            return; 
        } 
        if(L > r|| l > R) return;
        if(add[o]!=0) pushdown(o,l,r);
        int mid = (l + r) >> 1;
        update(lc,L,R,l,mid,v);
        update(rc,L,R,mid+1,r,v);
        T[o] = min(T[lc],T[rc]);
    }
    
    inline int query(int o,int L,int R,int l,int r)
    {
        if(L<=l&&r<=R) 
        {
            return T[o]; 
        } 
        if(L > r|| l > R) return 1<<30;
        if(add[o]!=0) pushdown(o,l,r);
        int mid = (l + r) >> 1;
        return min(query(lc,L,R,l,mid),query(rc,L,R,mid+1,r));
    }
    
    struct Query
    {
        int l,r;
        friend bool operator < (Query a1,Query a2)
        {
            if(a1.r != a2.r) return a1.r < a2.r;
            else return a1.l > a2.l;
        }
    }q[MAXN];
    
    int main()
    {
        n = read();m = read();
        for(int i=1;i<=n;i++) a[i] = read();
    //    cout << " FUCK" << endl;
        build(1,1,n);
        int ans = 0;
        for(int i=1;i<=m;i++)
        {
            q[i].l = read(),q[i].r = read();
        }
        sort(q+1,q+m+1);
    //    for(int i=1;i<=m;i++) cout << q[i].l << " " << q[i].r << endl;
        for(int i=1;i<=m;i++)
        {
            int v = query(1,q[i].l,q[i].r,1,n);
            //cout << v << endl;
            if(v<=0) continue;
            ans++;
            update(1,q[i].l,q[i].r,1,n,-1);
        }
        cout << ans << endl;
    }
  • 相关阅读:
    TO DO List
    springboot 热部署
    <dependencyManagement>的作用
    人体工程学座椅
    temp
    temp
    声明式编程和命令式编程的本质区别
    weak first question
    Spring依赖注入方式和依赖来源
    SpringBoot 整合 H2 数据库
  • 原文地址:https://www.cnblogs.com/wlzs1432/p/13821646.html
Copyright © 2011-2022 走看看