Submission #841249

# Submission time Handle Problem Language Result Execution time Memory
841249 2023-09-01T12:04:49 Z Forested Inspections (NOI23_inspections) C++17
100 / 100
228 ms 29220 KB
#ifndef LOCAL
#define FAST_IO
#endif

// ============
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32)(n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;

template <typename T>
using Vec = vector<T>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

#ifdef INT128

using u128 = __uint128_t;
using i128 = __int128_t;

istream &operator>>(istream &is, i128 &x) {
    i64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, i128 x) {
    os << (i64)x;
    return os;
}
istream &operator>>(istream &is, u128 &x) {
    u64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, u128 x) {
    os << (u64)x;
    return os;
}

#endif

[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
// ============

#ifdef DEBUGF
#else
#define DBG(x) (void)0
#endif

// のいみさんいつもありがとう 今回は借ります
// https://noimi.hatenablog.com/entry/2021/05/02/195143
// S : 持つデータの型 T : 範囲の型
template <class S, class T = int> struct IntervalManager {
    struct node {
        T l, r;
        S x;
        node(const T &l, const T &r, const S &x) : l(l), r(r), x(x) {}
        constexpr bool operator<(const node &rhs) const {
            if(l != rhs.l) return l < rhs.l;
            return r < rhs.r;
        }
    };
    const S unit;
    set<node> s;
    IntervalManager(const S &u = S()) : unit(u) {}
    IntervalManager(const vector<T> &a) {
        vector<node> setter;
        for(int l = 0; l < a.size(); l++) {
            int r = l;
            for(; r < a.size() and a[l] == a[r]; r++) {}
            setter.emplace_back(l, r, a[l]);
            l = r - 1;
        }
        s = set<node>(all(setter));
    }

    // x を含んだセグメントのイテレータを返す
    typename set<node>::iterator getIt(const T &x) {
        auto it = s.upper_bound(node(x, numeric_limits<T>::max(), 0));
        if(it == begin(s)) return end(s);
        it = prev(it);
        if(it->l <= x and x < it->r) return it;
        return end(s);
    }

    // x を含んだセグメントの情報を持ってくる
    node getSeg(const T &x) {
        auto it = getIt(x);
        if(it != end(s)) return *it;
        return node(x, x + 1, unit);
    }

    // x 以上をはじめて含むセグメントの iterator が帰ってくる
    typename set<node>::iterator nextIt(const T &x) {
        auto it = s.upper_bound(node(x, numeric_limits<T>::max(), 0));
        if(it == begin(s)) return it;
        return prev(it);
    }

    // x に設定されてる値を取得
    S get(const T &x) {
        auto it = getIt(x);
        if(it != end(s)) return it->x;
        return unit;
    }

    S operator[](T k) const { return get(k); }

    // [l, r) := x を set するときに消える区間加える区間ごとに関数を呼び出す
    // ただし、内包, 結合される [L, r) := x の区間も一旦消す
    template <typename ADD, typename DEL> void update(T l, T r, const S &x, const ADD &add, const DEL &del) {
        auto it = s.lower_bound(node{l, 0, x});
        while(it != end(s) and it->l <= r) {
            if(it->l == r) {
                if(it->x == x) {
                    del(r, it->r, x);
                    r = it->r, it = s.erase(it);
                }
                break;
            }
            if(it->r <= r) {
                del(it->l, it->r, it->x);
                it = s.erase(it);
            } else {
                if(it->x == x) {
                    r = it->r;
                    del(it->l, it->r, it->x);
                    it = s.erase(it);
                } else {
                    del(it->l, r, it->x);
                    node n = *it;
                    it = s.erase(it);
                    it = s.emplace_hint(it, r, n.r, n.x);
                }
            }
        }

        if(it != begin(s)) {
            it = prev(it);
            if(it->r == l) {
                if(it->x == x) {
                    del(it->l, it->r, it->x);
                    l = it->l;
                    it = s.erase(it);
                }
            } else if(l < it->r) {
                if(it->x == x) {
                    del(it->l, it->r, it->x);
                    l = min(l, it->l);
                    r = max(r, it->r);
                    it = s.erase(it);
                } else {
                    if(r < it->r) {
                        it = s.emplace_hint(next(it), r, it->r, it->x);
                        it = prev(it);
                    }
                    del(l, min(r, it->r), it->x);
                    node n = *it;
                    it = s.erase(it);
                    it = s.emplace_hint(it, n.l, l, n.x);
                }
            }
        }
        if(it != end(s)) it = next(it);
        add(l, r, x);
        s.emplace_hint(it, l, r, x);
    }

    void update(T l, T r, const S &x) {
        update(
            l, r, x, [](T, T, S) {}, [](T, T, S) {});
    }

    void show() {
        for(auto e : s) {
            cerr << "("
                 << "[" << e.l << ", " << e.r << "): " << e.x << ") ";
        }
        cerr << endl;
    }
};

int main() {
    i32 n, m, q;
    cin >> n >> m >> q;
    Vec<i32> l(m), r(m);
    REP(i, m) {
        cin >> l[i] >> r[i];
        --l[i];
    }
    Vec<i64> s(q);
    REP(i, q) {
        cin >> s[i];
    }
    IntervalManager<i64> manager;
    i64 day = 0;
    Vec<pair<i64, i64>> pool;
    REP(i, m) {
        i64 tmp = day - l[i];
        auto del = [&pool, &day, &tmp](i32 l, i32 r, i64 x) -> void {
            pool.emplace_back(tmp - x - 1, r - l);
        };
        auto add = [](i32 l, i32 r, i64 x) -> void {};
        manager.update(l[i], r[i], tmp, add, del);
        day += r[i] - l[i];
    }
    sort(ALL(pool));
    if (!pool.empty()) {
        PER(i, pool.size() - 1) {
            pool[i].second += pool[i + 1].second;
        }
    }
    REP(i, q) {
        auto it = lower_bound(ALL(pool), pair<i64, i64>(s[i], 0));
        if (it == pool.end()) {
            cout << 0;
        } else {
            cout << it->second;
        }
        cout << " \n"[i + 1 == q];
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 31 ms 3164 KB Output is correct
14 Correct 28 ms 3148 KB Output is correct
15 Correct 31 ms 3816 KB Output is correct
16 Correct 52 ms 3784 KB Output is correct
17 Correct 30 ms 3652 KB Output is correct
18 Correct 31 ms 3656 KB Output is correct
19 Correct 35 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 32 ms 3364 KB Output is correct
4 Correct 47 ms 4556 KB Output is correct
5 Correct 132 ms 14460 KB Output is correct
6 Correct 118 ms 15272 KB Output is correct
7 Correct 108 ms 10880 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 31 ms 3164 KB Output is correct
14 Correct 28 ms 3148 KB Output is correct
15 Correct 31 ms 3816 KB Output is correct
16 Correct 52 ms 3784 KB Output is correct
17 Correct 30 ms 3652 KB Output is correct
18 Correct 31 ms 3656 KB Output is correct
19 Correct 35 ms 3404 KB Output is correct
20 Correct 42 ms 5232 KB Output is correct
21 Correct 30 ms 3788 KB Output is correct
22 Correct 41 ms 4868 KB Output is correct
23 Correct 36 ms 3616 KB Output is correct
24 Correct 30 ms 3224 KB Output is correct
25 Correct 34 ms 4424 KB Output is correct
26 Correct 41 ms 4620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 388 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 31 ms 3164 KB Output is correct
14 Correct 28 ms 3148 KB Output is correct
15 Correct 31 ms 3816 KB Output is correct
16 Correct 52 ms 3784 KB Output is correct
17 Correct 30 ms 3652 KB Output is correct
18 Correct 31 ms 3656 KB Output is correct
19 Correct 35 ms 3404 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 32 ms 3364 KB Output is correct
23 Correct 47 ms 4556 KB Output is correct
24 Correct 132 ms 14460 KB Output is correct
25 Correct 118 ms 15272 KB Output is correct
26 Correct 108 ms 10880 KB Output is correct
27 Correct 0 ms 316 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 42 ms 5232 KB Output is correct
30 Correct 30 ms 3788 KB Output is correct
31 Correct 41 ms 4868 KB Output is correct
32 Correct 36 ms 3616 KB Output is correct
33 Correct 30 ms 3224 KB Output is correct
34 Correct 34 ms 4424 KB Output is correct
35 Correct 41 ms 4620 KB Output is correct
36 Correct 203 ms 22440 KB Output is correct
37 Correct 179 ms 22696 KB Output is correct
38 Correct 166 ms 23932 KB Output is correct
39 Correct 121 ms 15012 KB Output is correct
40 Correct 147 ms 23976 KB Output is correct
41 Correct 150 ms 28628 KB Output is correct
42 Correct 169 ms 29220 KB Output is correct
43 Correct 167 ms 27076 KB Output is correct
44 Correct 158 ms 28024 KB Output is correct
45 Correct 177 ms 23340 KB Output is correct
46 Correct 228 ms 17252 KB Output is correct
47 Correct 180 ms 17588 KB Output is correct
48 Correct 175 ms 24280 KB Output is correct