結果

問題 No.855 ヘビの日光浴
ユーザー shiomusubi496shiomusubi496
提出日時 2022-03-06 15:32:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 460 ms / 3,153 ms
コード長 25,405 bytes
コンパイル時間 4,525 ms
コンパイル使用メモリ 249,096 KB
実行使用メモリ 29,720 KB
最終ジャッジ日時 2023-09-28 06:32:39
合計ジャッジ時間 13,435 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 3 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 1 ms
4,376 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 1 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 1 ms
4,380 KB
testcase_68 AC 2 ms
4,376 KB
testcase_69 AC 2 ms
4,380 KB
testcase_70 AC 2 ms
4,380 KB
testcase_71 AC 2 ms
4,376 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 124 ms
11,732 KB
testcase_74 AC 100 ms
9,940 KB
testcase_75 AC 54 ms
7,096 KB
testcase_76 AC 253 ms
18,640 KB
testcase_77 AC 186 ms
15,252 KB
testcase_78 AC 298 ms
21,704 KB
testcase_79 AC 12 ms
4,380 KB
testcase_80 AC 18 ms
4,892 KB
testcase_81 AC 282 ms
19,852 KB
testcase_82 AC 313 ms
21,908 KB
testcase_83 AC 460 ms
29,592 KB
testcase_84 AC 442 ms
29,496 KB
testcase_85 AC 455 ms
29,564 KB
testcase_86 AC 452 ms
29,548 KB
testcase_87 AC 450 ms
29,584 KB
testcase_88 AC 450 ms
29,560 KB
testcase_89 AC 449 ms
29,620 KB
testcase_90 AC 447 ms
29,624 KB
testcase_91 AC 450 ms
29,632 KB
testcase_92 AC 456 ms
29,720 KB
testcase_93 AC 1 ms
4,376 KB
testcase_94 AC 171 ms
16,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "library/test/yuki/855_DynamicSegTree-BinarySearch.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/855"
#line 2 "library/other/template.hpp"

#include<bits/stdc++.h>

#ifndef __COUNTER__
#define __COUNTER__ __LINE__
#endif

#define REP_SELECTER(a, b, c, d, e, ...) e
#define REP1_0(b, c) REP1_1(b, c)
#define REP1_1(b, c) for (ll REP_COUNTER_ ## c = 0; REP_COUNTER_ ## c < (ll)(b); ++ REP_COUNTER_ ## c)
#define REP1(b) REP1_0(b, __COUNTER__)
#define REP2(i, b) for (ll i = 0; i < (ll)(b); ++i)
#define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c))
#define rep(...) REP_SELECTER(__VA_ARGS__, REP4, REP3, REP2, REP1) (__VA_ARGS__)
#define RREP2(i, a) for (ll i = (ll)(a) - 1; i >= 0; --i)
#define RREP3(i, a, b) for (ll i = (ll)(a) - 1; i >= (ll)(b); --i)
#define RREP4(i, a, b, c) for (ll i = (ll)(a) - 1; i >= (ll)(b); i -= (ll)(c))
#define rrep(...) REP_SELECTER(__VA_ARGS__, RREP4, RREP3, RREP2) (__VA_ARGS__)
#define REPS2(i, b) for (ll i = 1; i <= (ll)(b); ++i)
#define REPS3(i, a, b) for (ll i = (ll)(a) + 1; i <= (ll)(b); ++i)
#define REPS4(i, a, b, c) for (ll i = (ll)(a) + 1; i <= (ll)(b); i += (ll)(c))
#define reps(...) REP_SELECTER(__VA_ARGS__, REPS4, REPS3, REPS2) (__VA_ARGS__)
#define RREPS2(i, a) for (ll i = (ll)(a); i > 0; --i)
#define RREPS3(i, a, b) for (ll i = (ll)(a); i > (ll)(b); --i)
#define RREPS4(i, a, b, c) for (ll i = (ll)(a); i > (ll)(b); i -= (ll)(c))
#define rreps(...) REP_SELECTER(__VA_ARGS__, RREPS4, RREPS3, RREPS2) (__VA_ARGS__)

#define each_for(...) for (auto&& __VA_ARGS__)
#define each_const(...) for (const auto& __VA_ARGS__)

#define all(v) std::begin(v), std::end(v)
#define rall(v) std::rbegin(v), std::rend(v)

#if __cplusplus >= 201402L
#define CONSTEXPR constexpr
#else
#define CONSTEXPR
#endif

#ifdef __cpp_if_constexpr
#define IF_CONSTEXPR constexpr
#else
#define IF_CONSTEXPR
#endif

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PLL = std::pair<ll, ll>;
template<class T> using prique = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T> class infinity {
  public:
    static constexpr T value = std::numeric_limits<T>::max() / 2;
    static constexpr T mvalue = std::numeric_limits<T>::min() / 2;
    static constexpr T max = std::numeric_limits<T>::max();
    static constexpr T min = std::numeric_limits<T>::min();
};

#if __cplusplus <= 201402L
template<class T> constexpr T infinity<T>::value;
template<class T> constexpr T infinity<T>::mvalue;
template<class T> constexpr T infinity<T>::max;
template<class T> constexpr T infinity<T>::min;
#endif

#if __cplusplus >= 201402L
template<class T> constexpr T INF = infinity<T>::value;
#endif

constexpr ll inf = infinity<ll>::value;
constexpr ld EPS = 1e-8;
constexpr ld PI = 3.1415926535897932384626;

template<class T, class U> std::ostream& operator<<(std::ostream& ost, const std::pair<T, U>& p) {
    return ost << p.first << ' ' << p.second;
}
template<class T, class U> std::istream& operator>>(std::istream& ist, std::pair<T, U>& p) {
    return ist >> p.first >> p.second;
}

template<class Container,
        typename std::enable_if<!std::is_same<Container, std::string>::value>::type* = nullptr>
auto operator<<(std::ostream& ost, const Container& cont)
        -> decltype(cont.begin(), cont.end(), ost)
{
    for (auto itr = cont.begin(); itr != cont.end(); ++itr) {
        if (itr != cont.begin()) ost << ' ';
        ost << *itr;
    }
    return ost;
}
template<class Container,
        typename std::enable_if<!std::is_same<Container, std::string>::value>::type* = nullptr>
auto operator>>(std::istream& ist, Container& cont)
        -> decltype(cont.begin(), cont.end(), ist)
{
    for (auto itr = cont.begin(); itr != cont.end(); ++itr) ist >> *itr;
    return ist;
}

template<class T, class U> inline constexpr bool chmin(T &a, const U &b) noexcept {
    return a > b ? a = b, true : false;
}
template<class T, class U> inline constexpr bool chmax(T &a, const U &b) noexcept {
    return a < b ? a = b, true : false;
}

inline CONSTEXPR ll gcd(ll a, ll b) noexcept {
    while (b) {
        const ll c = a;
        a = b;
        b = c % b;
    }
    return a;
}
inline CONSTEXPR ll lcm(ll a, ll b) noexcept {
    return a / gcd(a, b) * b;
}

inline CONSTEXPR bool is_prime(ll N) noexcept {
    if (N <= 1) return false;
    for (ll i = 2; i * i <= N; ++i) {
        if (N % i == 0) return false;
    }
    return true;
}
inline std::vector<ll> prime_factor(ll N) noexcept {
    std::vector<ll> res;
    for (ll i = 2; i * i <= N; ++i) {
        while (N % i == 0) {
            res.push_back(i);
            N /= i;
        }
    }
    if (N != 1) res.push_back(N);
    return res;
}

inline CONSTEXPR ll my_pow(ll a, ll b) noexcept {
    ll res = 1;
    while (b) {
        if (b & 1) res *= a;
        b >>= 1;
        a *= a;
    }
    return res;
}
inline CONSTEXPR ll mod_pow(ll a, ll b, ll mod) noexcept {
    a %= mod;
    ll res = 1;
    while (b) {
        if (b & 1) (res *= a) %= mod;
        b >>= 1;
        (a *= a) %= mod;
    }
    return res;
}

PLL extGCD(ll a, ll b) noexcept {
    if (b == 0) return PLL{1, 0};
    PLL p = extGCD(b, a % b);
    std::swap(p.first, p.second);
    p.second -= p.first * (a / b);
    if (p.first < 0) {
        p.first += b;
        p.second -= a;
    }
    return p;
}
ll mod_inv(ll a, ll mod) noexcept {
    const PLL p = extGCD(a, mod);
    assert(p.first * a + p.second * mod == 1);
    return p.first;
}
PLL ChineseRemainder(ll b1, ll m1, ll b2, ll m2) noexcept {
    const PLL p = extGCD(m1, m2);
    const ll g = p.first * m1 + p.second * m2;
    const ll l = m1 / g * m2;
    if ((b2 - b1) % g != 0) return PLL{-1, -1};
    const ll x = (b2 - b1) / g * p.first % (m2 / g);
    return {(x * m1 + b1 + l) % l, l};
}
PLL ChineseRemainders(const std::vector<ll>& b, const std::vector<ll>& m) noexcept {
    PLL res{0, 1};
    rep (i, b.size()) {
        res = ChineseRemainder(res.first, res.second, b[i], m[i]);
        if (res.first == -1) return res;
    }
    return res;
}

template<class F> class RecLambda {
  private:
    F f;
  public:
    explicit constexpr RecLambda(F&& f_) : f(std::forward<F>(f_)) {}
    template<class... Args> constexpr auto operator()(Args&&... args) const
            -> decltype(f(*this, std::forward<Args>(args)...)) {
        return f(*this, std::forward<Args>(args)...);
    }
};

template<class F> inline constexpr RecLambda<F> rec_lambda(F&& f) {
    return RecLambda<F>(std::forward<F>(f));
}

template<class Head, class... Tail> struct multi_dim_vector {
    using type = std::vector<typename multi_dim_vector<Tail...>::type>;
};
template<class T> struct multi_dim_vector<T> {
    using type = T;
};

template<class T, class Arg> constexpr std::vector<T> make_vec(int n, Arg&& arg) {
    return std::vector<T>(n, std::forward<Arg>(arg));
}
template<class T, class... Args>
constexpr typename multi_dim_vector<Args..., T>::type make_vec(int n, Args&&... args) {
    return typename multi_dim_vector<Args..., T>::type (n, make_vec<T>(std::forward<Args>(args)...));
}

inline CONSTEXPR int popcnt(ull x) {
#if __cplusplus >= 202002L
    return std::popcount(x);
#endif
    x = (x & 0x5555555555555555) + ((x >> 1 ) & 0x5555555555555555);
    x = (x & 0x3333333333333333) + ((x >> 2 ) & 0x3333333333333333);
    x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4 ) & 0x0f0f0f0f0f0f0f0f);
    x = (x & 0x00ff00ff00ff00ff) + ((x >> 8 ) & 0x00ff00ff00ff00ff);
    x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff);
    return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff);
}

template<class T, class Comp = std::less<T>> class presser {
  protected:
    std::vector<T> dat;
    Comp cmp;
    bool sorted = false;
  public:
    presser() : presser(Comp()) {}
    presser(const Comp& cmp) : cmp(cmp) {}
    presser(const std::vector<T>& vec, const Comp& cmp = Comp()) : dat(vec), cmp(cmp) {}
    presser(std::vector<T>&& vec, const Comp& cmp = Comp()) : dat(std::move(vec)), cmp(cmp) {}
    presser(std::initializer_list<T> il, const Comp& cmp = Comp()) : dat(il.begin(), il.end()), cmp(cmp) {}
    void reserve(int n) {
        assert(!sorted);
        dat.reserve(n);
    }
    void push_back(const T& v) {
        assert(!sorted);
        dat.push_back(v);
    }
    void push_back(T&& v) {
        assert(!sorted);
        dat.push_back(std::move(v));
    }
    void push(const std::vector<T>& vec) {
        assert(!sorted);
        dat.reserve(dat.size() + vec.size());
        std::copy(all(vec), std::back_inserter(dat));
    }
    int build() {
        assert(!sorted); sorted = true;
        std::sort(all(dat), cmp);
        dat.erase(std::unique(all(dat), [&](const T& a, const T& b) -> bool {
            return !cmp(a, b) && !cmp(b, a);
        }), dat.end());
        return dat.size();
    }
    const T& operator[](int k) const& {
        assert(sorted);
        assert(0 <= k && k < (int)dat.size());
        return dat[k];
    }
    T operator[](int k) && {
        assert(sorted);
        assert(0 <= k && k < (int)dat.size());
        return std::move(dat[k]);
    }
    int get_index(const T& val) const {
        assert(sorted);
        return static_cast<int>(std::lower_bound(all(dat), val, cmp) - dat.begin());
    }
    std::vector<int> pressed(const std::vector<T>& vec) const {
        assert(sorted);
        std::vector<int> res(vec.size());
        rep (i, vec.size()) res[i] = get_index(vec[i]);
        return res;
    }
    void press(std::vector<T>& vec) const {
        static_assert(std::is_integral<T>::value, "template argument must be convertible from int type");
        assert(sorted);
        each_for (i : vec) i = get_index(i);
    }
    int size() const {
        assert(sorted);
        return dat.size();
    }
    const std::vector<T>& data() const& { return dat; }
    std::vector<T> data() && { return std::move(dat); }
};
#line 2 "library/data-struct/segment/DynamicSegmentTree.hpp"

#line 2 "library/other/bitop.hpp"

#line 4 "library/other/bitop.hpp"

namespace bitop {

#define KTH_BIT(b, k) (((b) >> (k)) & 1)
#define POW2(k) (1ull << (k))

    inline ull next_combination(int n, ull x) {
        if (n == 0) return 1;
        ull a = x & -x;
        ull b = x + a;
        return (x & ~b) / a >> 1 | b;
    }

#define rep_comb(i, n, k) for (ull i = (1ull << (k)) - 1; i < (1ull << (n)); i = bitop::next_combination((n), i))

    inline CONSTEXPR int msb(ull x) {
        int res = x ? 0 : -1;
        if (x & 0xFFFFFFFF00000000) x &= 0xFFFFFFFF00000000, res += 32;
        if (x & 0xFFFF0000FFFF0000) x &= 0xFFFF0000FFFF0000, res += 16;
        if (x & 0xFF00FF00FF00FF00) x &= 0xFF00FF00FF00FF00, res +=  8;
        if (x & 0xF0F0F0F0F0F0F0F0) x &= 0xF0F0F0F0F0F0F0F0, res +=  4;
        if (x & 0xCCCCCCCCCCCCCCCC) x &= 0xCCCCCCCCCCCCCCCC, res +=  2;
        return res + ((x & 0xAAAAAAAAAAAAAAAA) ? 1 : 0);
    }

    inline CONSTEXPR int ceil_log2(ull x) {
        return x ? msb(x - 1) + 1 : 0;
    }
}
#line 2 "library/other/monoid.hpp"

#line 4 "library/other/monoid.hpp"

namespace Monoid {

template<class T> struct Sum {
    using value_type = T;
    static constexpr T op(const T& a, const T& b) { return a + b; }
    static constexpr T id() { return T{0}; }
    static constexpr T inv(const T& a, const T& b) { return a - b; }
    static constexpr T get_inv(const T& a) { return -a; }
};

template<class T, T max_value = infinity<T>::max> struct Min {
    using value_type = T;
    static constexpr T op(const T& a, const T& b) { return a > b ? b : a; }
    static constexpr T id() { return max_value; }
};

template<class T, T min_value = infinity<T>::min> struct Max {
    using value_type = T;
    static constexpr T op(const T& a, const T& b) { return a < b ? b : a;}
    static constexpr T id() { return min_value; }
};

template<class T> struct Assign {
    using value_type = T;
    static constexpr T op(const T&, const T& b) { return b; }
};


template<class T, T max_value = infinity<T>::max> struct AssignMin {
    using M = Min<T, max_value>;
    using E = Assign<T>;
    static constexpr T op(const T& a, const T&) { return a; }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T&) { return a; }
};

template<class T, T min_value = infinity<T>::min> struct AssignMax {
    using M = Max<T, min_value>;
    using E = Assign<T>;
    static constexpr T op(const T& a, const T&) { return a; }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T&) { return a; }
};

template<class T> struct AssignSum {
    using M = Sum<T>;
    using E = Assign<T>;
    static constexpr T op(const T& a, const T&) { return a; }
    static constexpr T mul(const T& a, int b) { return a * b; }
    static constexpr T mul_op(const T& a, int b, const T&) { return a * b; }
};

template<class T, T max_value = infinity<T>::max> struct AddMin {
    using M = Min<T, max_value>;
    using E = Sum<T>;
    static constexpr T op(const T& a, const T& b) { return b + a; }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T& c) { return c + a; }
};

template<class T, T min_value = infinity<T>::min> struct AddMax {
    using M = Max<T, min_value>;
    using E = Sum<T>;
    static constexpr T op(const T& a, const T& b) { return b + a; }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T& c) { return c + a; }
};

template<class T> struct AddSum {
    using M = Sum<T>;
    using E = Sum<T>;
    static constexpr T op(const T& a, const T& b) { return b + a; }
    static constexpr T mul(const T& a, int b) { return a * b; }
    static constexpr T mul_op(const T& a, int b, const T& c) { return c + a * b; }
};

template<class T, T max_value = infinity<T>::max> struct ChminMin {
    using M = Min<T, max_value>;
    using E = Min<T>;
    static constexpr T op(const T& a, const T& b) { return std::min(b, a); }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T& c) { return std::min(c, a); }
};

template<class T, T min_value = infinity<T>::min> struct ChminMax {
    using M = Max<T, min_value>;
    using E = Min<T>;
    static constexpr T op(const T& a, const T& b) { return std::min(b, a); }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T& c) { return std::min(c, a); }
};

template<class T, T max_value = infinity<T>::max> struct ChmaxMin {
    using M = Min<T, max_value>;
    using E = Max<T>;
    static constexpr T op(const T& a, const T& b) { return std::max(b, a); }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T& c) { return std::max(c, a); }
};

template<class T, T min_value = infinity<T>::min> struct ChmaxMax {
    using M = Max<T, min_value>;
    using E = Max<T>;
    static constexpr T op(const T& a, const T& b) { return std::max(b, a); }
    static constexpr T mul(const T& a, int) { return a; }
    static constexpr T mul_op(const T& a, int, const T& c) { return std::max(c, a); }
};


template<class M> struct ReverseMonoid {
    using value_type = typename M::value_type;
    static value_type op(const value_type& a, const value_type& b) {
        return M::op(b, a);
    }
    static value_type id() { return M::id(); }
    static value_type get_inv(const value_type& a) { return M::get_inv(a); }
};

template<class M_> struct AttachEffector {
    using M = M_;
    using E = M_;
    using T = typename M_::value_type;
    static T op(const T& a, const T& b) { return M_::op(b, a); }
};

template<class E_> struct AttachMonoid {
    using M = E_;
    using E = E_;
    using T = typename E_::value_type;
    static T op(const T& a, const T& b) { return E_::op(b, a); }
};


template<class M, class = void> class has_id : public std::false_type {};
template<class M> class has_id<M, decltype((void)M::id)> : public std::true_type {};

template<class M, class = void> class has_inv : public std::false_type {};
template<class M> class has_inv<M, decltype((void)M::inv)> : public std::true_type {};

template<class M, class = void> class has_get_inv : public std::false_type {};
template<class M> class has_get_inv<M, decltype((void)M::get_inv)> : public std::true_type {};


template<class A, class = void> class has_mul : public std::false_type {};
template<class A> class has_mul<A, decltype((void)A::mul)> : public std::true_type {};

template<class A, class = void> class has_mul_op : public std::false_type {};
template<class A> class has_mul_op<A, decltype((void)A::mul_op)> : public std::true_type {};


template<class T, class = void> class is_semigroup : public std::false_type {};;
template<class T> class is_semigroup<T, decltype(std::declval<typename T::value_type>(), (void)T::op)> : public std::true_type {};

template<class T, class = void> class is_monoid : public std::false_type {};;
template<class T> class is_monoid<T, decltype(std::declval<typename T::value_type>(), (void)T::op, (void)T::id)> : public std::true_type {};

template<class T, class = void> class is_group : public std::false_type {};;
template<class T> class is_group<T, decltype(std::declval<typename T::value_type>(), (void)T::op, (void)T::id, (void)T::get_inv)> : public std::true_type {};

template<class T, class = void> class is_action : public std::true_type {};
template<class T> class is_action<T, decltype(std::declval<typename T::M>(), std::declval<typename T::E>(), (void)T::op)> : public std::false_type {};

} // namespace Monoid
#line 6 "library/data-struct/segment/DynamicSegmentTree.hpp"

template<class M> class DynamicSegmentTree {
  protected:
    using T = typename M::value_type;
    struct Node;
    using Node_ptr = std::unique_ptr<Node>;
    struct Node {
        T val;
        Node_ptr l, r;
        Node_ptr& get_l() {
            if (l == nullptr) l = std::make_unique<Node>();
            return l;
        }
        Node_ptr& get_r() {
            if (r == nullptr) r = std::make_unique<Node>();
            return r;
        }
        void update() {
            val = M::id();
            if (l != nullptr) val = M::op(val, l->val);
            if (r != nullptr) val = M::op(val, r->val);
        }
        Node() : val(M::id()), l(nullptr), r(nullptr) {}
    };
    ll ori, h, n;
    Node_ptr root;
    template<class Upd> void update(Node_ptr& nd, ll a, ll b, ll k, const Upd& upd) {
        if (a + 1 == b) {
            nd->val = upd(nd->val);
            return;
        }
        ll m = (a + b) >> 1;
        if (k < m) update(nd->get_l(), a, m, k, upd);
        else update(nd->get_r(), m, b, k, upd);
        nd->update();
    }
    T prod(const Node_ptr& nd, ll a, ll b, ll l, ll r) const {
        if (nd == nullptr) return M::id();
        if (l <= a && b <= r) return nd->val;
        if (r <= a || b <= l) return M::id();
        ll m = (a + b) >> 1;
        return M::op(prod(nd->l, a, m, l, r), prod(nd->r, m, b, l, r));
    }
    template<class Cond> ll max_right(const Node_ptr& nd, ll a, ll b, ll l, const Cond& cond, T& sm) const {
        if (nd == nullptr || b <= l) return n;
        if (l <= a && cond(M::op(sm, nd->val))) {
            sm = M::op(sm, nd->val);
            return n;
        }
        if (a + 1 == b) return a;
        ll m = (a + b) >> 1;
        ll res = max_right(nd->l, a, m, l, cond, sm);
        if (res != n) return res;
        return max_right(nd->r, m, b, l, cond, sm);
    }
    template<class Cond> ll min_left(const Node_ptr& nd, ll a, ll b, ll r, const Cond& cond, T& sm) const {
        if (nd == nullptr || r <= a) return 0;
        if (b <= r && cond(M::op(nd->val, sm))) {
            sm = M::op(nd->val, sm);
            return 0;
        }
        if (a + 1 == b) return b;
        ll m = (a + b) >> 1;
        ll res = min_left(nd->r, m, b, r, cond, sm);
        if (res != 0) return res;
        return min_left(nd->l, a, m, r, cond, sm);
    }
    void reset(Node_ptr& nd, ll a, ll b, ll l, ll r) {
        if (nd == nullptr) return;
        if (r <= a || b <= l) return;
        if (l <= a && b <= r) {
            if (nd == root) nd = std::make_unique<Node>();
            else nd.reset();
            return;
        }
        ll m = (a + b) >> 1;
        reset(nd->l, a, m, l, r);
        reset(nd->r, m, b, l, r);
        nd->update();
    }
  public:
    DynamicSegmentTree() : DynamicSegmentTree(inf) {}
    DynamicSegmentTree(ll n_) { init(n_); }
    void init(ll n_) {
        ori = n_;
        h = bitop::ceil_log2(ori);
        n = 1ull << h;
        root = std::make_unique<Node>();
    }
    template<class Upd> void update(ll k, const Upd& upd) {
        assert(0 <= k && k < ori);
        update(root, 0, n, k, upd);
    }
    void set(ll k, T x) {
        update(k, [&](T) -> T { return x; });
    }
    void apply(ll k, T x) {
        update(k, [&](T a) -> T { return M::op(a, x); });
    }
    T prod(ll l, ll r) const {
        assert(0 <= l && l <= r && r <= ori);
        return prod(root, 0, n, l, r);
    }
    T all_prod() const { return root->val; }
    T get(ll k) const { return prod(k, k + 1); }
    template<class Cond> ll max_right(ll l, const Cond& cond) const {
        assert(0 <= l && l <= ori);
        if (l == n) return n;
        T sm = M::id();
        assert(cond(sm));
        return std::min(max_right(root, 0, n, l, cond, sm), ori);
    }
    template<class Cond> ll min_left(ll r, const Cond& cond) const {
        assert(0 <= r && r <= ori);
        if (0 == r) return 0;
        T sm = M::id();
        assert(cond(sm));
        return min_left(root, 0, n, r, cond, sm);
    }
    void reset(ll l, ll r) { reset(root, 0, n, l, r); }
    void reset(ll k) { reset(root, 0, n, k, k + 1); }
};

/**
 * @brief DynamicSegmentTree(動的セグメント木)
 * @docs docs/DynamicSegmentTree.md
 */
#line 4 "library/test/yuki/855_DynamicSegTree-BinarySearch.test.cpp"
using namespace std;
int main() {
    ll H, W, N; cin >> H >> W >> N;
    DynamicSegmentTree<Monoid::Max<ll>> segl(H + 2), segu(W + 2);
    DynamicSegmentTree<Monoid::Min<ll>> segr(H + 2), segd(W + 2);
    map<ll, ll> mpl, mpr, mpu, mpd;
    rep (N) {
        ll x, y, l; cin >> x >> y >> l;
        bool flg = false;
        if (y == 0 || y == H + 1) {
            swap(H, W);
            swap(x, y);
            swap(segl, segu); swap(segr, segd);
            swap(mpl, mpu); swap(mpr, mpd);
            flg = true;
        }
        if (x == 0) {
            if (mpl.count(y)) {
                l += mpl[y];
                mpl.erase(y);
                segl.reset(y);
            }
            ll nx = min(l, W);
            ll idx = infinity<ll>::max, kd = -1;
            if (l >= W + 1) {
                idx = W + 1; kd = 3;
            }
            if (mpr.count(y) && mpr[y] <= nx) {
                if (chmin(idx, mpr[y])) kd = 0;
            }
            if (segu.prod(0, nx + 1) >= y) {
                if (chmin(idx, segu.max_right(0, [&](ll a) { return a < y; }))) kd = 1;
            }
            if (segd.prod(0, nx + 1) <= y) {
                if (chmin(idx, segd.max_right(0, [&](ll a) { return a > y; }))) kd = 2;
            }
            if (kd == 0) {
                mpr.erase(y);
                segr.reset(y);
            }
            else if (kd == 1) {
                mpu.erase(idx);
                segu.reset(idx);
            }
            else if (kd == 2) {
                mpd.erase(idx);
                segd.reset(idx);
            }
            else if (kd == -1) {
                mpl[y] = nx;
                segl.set(y, nx);
            }
        }
        if (x == W + 1) {
            if (mpr.count(y)) {
                l += W + 1 - mpr[y];
                mpr.erase(y);
                segr.reset(y);
            }
            ll nx = max(W - l + 1, 0ll);
            ll idx = infinity<ll>::min, kd = -1;
            if (l >= W + 1) {
                idx = 0; kd = 3;
            }
            if (mpl.count(y) && nx <= mpl[y]) {
                if (chmax(idx, mpl[y])) kd = 0;
            }
            if (segu.prod(nx, W + 2) >= y) {
                if (chmax(idx, segu.min_left(W + 2, [&](ll a) { return a < y; }) - 1)) kd = 1;
            }
            if (segd.prod(nx, W + 2) <= y) {
                if (chmax(idx, segd.min_left(W + 2, [&](ll a) { return a > y; }) - 1)) kd = 2;
            }
            if (kd == 0) {
                mpl.erase(y);
                segl.reset(y);
            }
            else if (kd == 1) {
                mpu.erase(idx);
                segu.reset(idx);
            }
            else if (kd == 2) {
                mpd.erase(idx);
                segd.reset(idx);
            }
            else if (kd == -1) {
                mpr[y] = nx;
                segr.set(y, nx);
            }
        }
LABEL:;
        if (flg) {
            swap(H, W);
            swap(x, y);
            swap(segl, segu); swap(segr, segd);
            swap(mpl, mpu); swap(mpr, mpd);
        }
    }
    ll ans = 0;
    each_const (p : mpl) ans += p.second;
    each_const (p : mpr) ans += W + 1 - p.second;
    each_const (p : mpu) ans += p.second;
    each_const (p : mpd) ans += H + 1 - p.second;
    cout << ans << endl;
}
0