1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <vector>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>

using namespace std;


enum color_t
{
    none = 0,
    yellow = 1,
    blue = 2,
    red = 4,
};

struct order_t
{
    int k;
    uint l;
    uint r;
};

struct node_t
{
    int k;
    bool v;

    // Just so we don't have to calculate
    // this every time.
    uint l;
    uint r;
};


uint closest_power_of_2(uint x)
{
    x--;
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x++;
    return x;
}


inline uint left_idx(uint parent)
{
    return parent * 2 + 1;
}

inline uint right_idx(uint parent)
{
    return parent * 2 + 2;
}

inline bool is_leaf(uint idx, vector<node_t>& T)
{
    return left_idx(idx) >= T.size();
}


inline int update_color(int old_color, int mix)
{
    return old_color | mix;
}

void set_ranges(vector<node_t>& T, uint p, int l, int r)
{
    node_t &parent = T[p];
    parent.l = l;
    parent.r = r;

    if (is_leaf(p, T))
        return;

    uint subrange_size = (r - l) / 2;
    assert(l + subrange_size + 1 == r - subrange_size);
    set_ranges(T, left_idx(p), l, l + subrange_size);
    set_ranges(T, right_idx(p), r - subrange_size, r);
}


void parse_order(uint p, vector<node_t>& T, order_t order, int depth)
{
    //cout << string(depth * 3, ' ') << "[i] Parsing order: l=" << order.l << ", r=" << order.r << ", k=" << order.k << endl;
    node_t &parent = T[p];

    if (is_leaf(p, T)) {
        assert(order.l == parent.l && order.r == parent.r);
        parent.k = update_color(parent.k, order.k);
        parent.v = 1;
        return;
    }

    node_t &left = T[left_idx(p)];
    node_t &right = T[right_idx(p)];

    if (parent.v) {
        // If valid, then push old colors down.
        parse_order(left_idx(p), T, {.k=parent.k, .l=left.l, .r=left.r}, depth+1);
        parse_order(right_idx(p), T, {.k=parent.k, .l=right.l, .r=right.r}, depth+1);
        parent.v = 0;
    }

    if (order.l == parent.l && order.r == parent.r) {
        // Just color the parent.
        parent.k = update_color(parent.k, order.k);
        parent.v = 1;
        return;
    }

    if (order.l <= left.r)
        parse_order(left_idx(p), T, {.k=order.k, .l=order.l, .r=min(order.r, left.r)}, depth+1);
    if (order.r >= right.l)
        parse_order(right_idx(p), T, {.k=order.k, .l=max(order.l, right.l), .r=order.r}, depth+1);
}


uint count_green_cans(vector<node_t>& T, uint limit, uint p)
{
    node_t &parent = T[p];

    if (is_leaf(p, T)) {
        if (parent.k == 3 && p <= limit) 
            return 1;
        return 0;
    }

    node_t &left = T[left_idx(p)];
    node_t &right = T[right_idx(p)];

    if (parent.v) {
        parse_order(left_idx(p), T, {.k=parent.k, .l=left.l, .r=left.r}, 0);
        parse_order(right_idx(p), T, {.k=parent.k, .l=right.l, .r=right.r}, 0);
    }

    return count_green_cans(T, limit, left_idx(p)) +
           count_green_cans(T, limit, right_idx(p));
}


int main()
{
    // load inptut
    uint ncans;
    uint norders;
    cin >> ncans;
    cin >> norders;
    vector<order_t> orders;
    for (uint i = 0; i < norders; i++) {
        uint l, r, k;
        cin >> l;
        cin >> r;
        cin >> k;
        if (k == 3) k = 4; // it's easier to work on bits.
        orders.emplace_back(order_t{.k=k, .l=l - 1, .r=r - 1});
    }

    // Create interval tree.
    uint rounded_nranges = closest_power_of_2(ncans);
    vector<node_t> T(rounded_nranges * 2 - 1);
    set_ranges(T, 0, 0, rounded_nranges - 1);
    T[0].k = none;
    T[0].v=true;


    // Parse orders.
    for (order_t& order : orders) {
        parse_order(0, T, order, 0);
    }
    //cout << "------------" << endl;
    // Find number of green cans.
    //cout << "ncans: " << ncans << ", rounded_nranges: " << rounded_nranges << ", limit: " << rounded_nranges + ncans - 1 << endl;
    uint green_cans = count_green_cans(T, rounded_nranges + ncans - 1, 0);
    cout << green_cans << endl;

    return 0;
}