NOIP2012 借教室 (线段树)

Description

Solution

考虑用线段树维护,每借一段时间的教师便在线段树中减去对应的值,不理解为什么网上这么多二分的。。

Code

//Author: Hany01
//Date: Nov 5th. 2017
#include
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define lc (t << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define pb push_back
#define INF (0x3f3f3f3f)
#define Mod (1000000007)
using namespace std;
typedef long long LL;

template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }

int _ , __;
char c_;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("classroom.in" , "r" , stdin);
    freopen("classroom.out" , "w" , stdout);
#endif
}

const int maxn = 1000010;

int tr[maxn << 2], addv[maxn << 2], n, m, dt, x, y;

inline void maintain(int t) { tr[t] = min(tr[lc], tr[rc]); }

inline void pushdown(int t)
{
    if (!addv[t]) return ;
    tr[lc] += addv[t]; tr[rc] += addv[t]; addv[lc] += addv[t]; addv[rc] += addv[t]; addv[t] = 0;
}

void build(int t, int l, int r)
{
    if (l == r) { tr[t] = read(); return; }
    build(lc, l, mid); build(rc, mid + 1, r);
    maintain(t);
}

void update(int t, int l, int r)
{
    if (x <= l && r <= y) { tr[t] += dt; addv[t] += dt; return; }
    pushdown(t);
    if (x <= mid) update(lc, l, mid);
    if (y > mid)  update(rc, mid + 1, r);
    maintain(t);
}

int main()
{
    File();
    n = read(); m = read();
    build(1, 1, n);
    For(Case, 1, m)
    {
        dt = -read(); x = read(); y = read();
        update(1, 1, n);
        if (tr[1] < 0) { printf("-1\n%d\n", Case); return 0; }
    }
    puts("0");
    return 0;
}
//静夜四无邻,荒居旧业贫。
//雨中黄叶树,灯下白头人。
//以我独沉久,愧君相见频。
//平生自有分,况是霍家亲。
//--司空曙《喜见外弟卢纶见宿》

你可能感兴趣的:(NOIP,线段树,二分,树状数组)