• 售票系统 (线段树)


    仔细分析,其实是一个非常水的题目

    我们把一个区间已经用的票记录在线段树上

    就相当于对于一个区间查找区间最大值,如果可行,那么继续修改

    注意坐车相当于查询区间 ([a,b)),建树是 (build(1,1,n+1))

    其他就没什么了

    #include <map>
    #include <set>
    #include <ctime>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <vector>
    #include <bitset>
    #include <cstdio>
    #include <cctype>
    #include <string>
    #include <numeric>
    #include <cstring>
    #include <cassert>
    #include <climits>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std ;
    //#define int long long
    #define rep(i, a, b) for (int i = (a); i <= (b); i++)
    #define per(i, a, b) for (int i = (a); i >= (b); i--)
    #define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
    #define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
    #define clr(a) memset(a, 0, sizeof(a))
    #define ass(a, sum) memset(a, sum, sizeof(a))
    #define lowbit(x) (x & -x)
    #define all(x) x.begin(), x.end()
    #define ub upper_bound
    #define lb lower_bound
    #define pq priority_queue
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define iv inline void
    #define enter cout << endl
    #define siz(x) ((int)x.size())
    #define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
    typedef long long ll ;
    typedef unsigned long long ull ;
    typedef pair <int, int> pii ;
    typedef vector <int> vi ;
    typedef vector <pii> vii ;
    typedef queue <int> qi ;
    typedef queue <pii> qii ;
    typedef set <int> si ;
    typedef map <int, int> mii ;
    typedef map <string, int> msi ;
    const int N = 100010 ;
    const int INF = 0x3f3f3f3f ;
    const int iinf = 1 << 30 ;
    const ll linf = 2e18 ;
    const int MOD = 1000000007 ;
    const double eps = 1e-7 ;
    void print(int x) { cout << x << endl ; exit(0) ; }
    void PRINT(string x) { cout << x << endl ; exit(0) ; }
    void douout(double x){ printf("%lf
    ", x + 0.0000000001) ; }
    template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
    template <class T>void chmax(T &a, T b) { if (a < b) a = b ; }
    
    int n, m, q ;
    
    struct SegTree {
        int l, r, v, tag ;
        #define ls(x) x << 1
        #define rs(x) x << 1 | 1
        #define l(x) tr[x].l
        #define r(x) tr[x].r
        #define sz(x) (tr[x].r - tr[x].l + 1)
        #define v(x) tr[x].v
        #define tag(x) tr[x].tag
    } tr[N << 2] ;
    
    void pushup(int x) {
        v(x) = max(v(ls(x)), v(rs(x))) ;
    }
    
    void pushdown(int x) {
        if (tag(x)) {
            tag(ls(x)) += tag(x) ;
            tag(rs(x)) += tag(x) ;
            v(ls(x)) += tag(x) ;
            v(rs(x)) += tag(x) ;
            tag(x) = 0 ;
        }
    }
    
    void build(int x, int l, int r) {
        l(x) = l, r(x) = r ;
        if (l(x) == r(x)) return ;
        int mid = (l + r) >> 1 ;
        build(ls(x), l, mid) ;
        build(rs(x), mid + 1, r) ;
    }
    
    void modify(int x, int l, int r, int v) {
        if (l <= l(x) && r(x) <= r) {
            tag(x) += v ; v(x) += v ;
            return ;
        }
        pushdown(x) ;
        int mid = (l(x) + r(x)) >> 1 ;
        if (l <= mid) modify(ls(x), l, r, v) ;
        if (mid < r) modify(rs(x), l, r, v) ;
        pushup(x) ;
    }
    
    int query(int x, int l, int r) {
        if (l <= l(x) && r(x) <= r) return v(x) ;
        pushdown(x) ;
        int mid = (l(x) + r(x)) >> 1, ans = 0 ;
        if (l <= mid) ans = max(ans, query(ls(x), l, r)) ;
        if (mid < r) ans = max(ans, query(rs(x), l, r)) ;
        return ans ;
    }
    
    signed main(){
        scanf("%d%d%d", &n, &m, &q) ;
        build(1, 1, n + 1) ;
        while (q--) {
            int s, e, v ; scanf("%d%d%d", &s, &e, &v) ; e-- ;
            int hv = query(1, s, e) ;
            if (m - hv >= v) {
                puts("YES") ;
                modify(1, s, e, v) ;
            } else {
                puts("NO") ;
            }
        }
        return 0 ;
    }
    
    /*
    写代码时请注意:
        1.ll?数组大小,边界?数据范围?
        2.精度?
        3.特判?
        4.至少做一些
    思考提醒:
        1.最大值最小->二分?
        2.可以贪心么?不行dp可以么
        3.可以优化么
        4.维护区间用什么数据结构?
        5.统计方案是用dp?模了么?
        6.逆向思维?
    */
    
    
    
    
    加油ヾ(◍°∇°◍)ノ゙
  • 相关阅读:
    RESTful API 设计指南
    理解RESTful架构
    django-mysqlclient_1193错误
    获取当前脚本所在的目录和路径
    20191007
    20191005
    20191001
    20190927
    20190922
    莫比乌斯反演证明
  • 原文地址:https://www.cnblogs.com/harryhqg/p/10533302.html
Copyright © 2020-2023  润新知