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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <cstdio>
#include <set>
using namespace std;

int skipWhites() {
    int a = getchar();
    while (a == ' ' || a == '\n') {
        a = getchar();
    }
    return a;
}

int readInt() {
    int result = 0;
    int a = skipWhites();
    while (a != ' ' && a != '\n' && a != EOF) {
        result = result * 10;
        a-= '0';
        result+=a;
        a=getchar();
    }
    return result;
}

enum action {paint, unpaint};

struct fragment {
    int l;
    int r;
};

bool operator<(const fragment &a, const fragment &b)
{
    return a.l < b.l;
}

auto last_less_or_equal_to (int l, set<fragment> &S) {
    fragment example{};
    example.l = l;
    if (!S.empty()) {
        auto candidate = S.end();
        candidate--;
        //////printf("XDDD:%i\n", candidate->l);
        if (candidate->l <= l) {
            return candidate;
        } else if (S.size() > 1) {
            auto result = S.upper_bound(example);
            if(result != S.end()) {
                result--;
                if (result->l <= l) {
                    return result;
                }
            }
        }
    }
    return S.end();
}

int operation(int l, int r, action a, set<fragment> &S) {
    //returns number of painted elements in [l, r] and then performs action
    int painted = 0;
    fragment example{};
    //const set<fragment>::iterator &ub_l = S.upper_bound(example);
    //////printf("szukam dla: %i\n", l);
    auto lb_l = last_less_or_equal_to(l, S); //iteratora do ostatniego elementu mniejszego bądz rownego l
    //////printf("found: %i %i\n", lb_l.operator->()->l, lb_l.operator->()->r);
    if (a == paint && lb_l != S.end() && lb_l.operator->()->l <= l && r <= lb_l.operator->()->r) {
        return r - l + 1;
    }


    //const set<fragment>::iterator &ub_r = S.upper_bound(example);
    auto lb_r = last_less_or_equal_to(r, S);
    //////printf("found: %i %i\n", lb_l.operator->()->l, lb_l.operator->()->r);

    bool is_l_in = false;
    if (lb_l != S.end() && lb_l->l < l && l <= lb_l->r) {
        is_l_in = true;
    }
    bool is_r_in = false; //ostre zeby nie wyrabywac pojedynczych o dlugosci 1 bo one moga zostac
    if (lb_r != S.end() && lb_r->l <= r && r < lb_r->r) {
        is_r_in = true;
    }

    //rozbija ciagi w miejscach przeciecia
    if (lb_l != S.end() && is_l_in && is_r_in && lb_l == lb_r) {
        //mamy poteznego balasa wiec tniemy go na 3 czesci
        fragment cz1{};
        fragment cz2{};
        fragment cz3{};
        cz1.l = lb_l.operator->()->l;
        cz1.r = l - 1;
        cz2.l = l;
        cz2.r = r;
        cz3.l = r + 1;
        cz3.r = lb_l.operator->()->r;
        ////printf("wywalam: %i %i\n", cz1.l, cz3.r);
        S.erase(lb_l);
        ////printf("wrzucam: %i %i\n", cz1.l, cz1.r);
        ////printf("wrzucam: %i %i\n", cz2.l, cz2.r);
        ////printf("wrzucam: %i %i\n", cz3.l, cz3.r);
        S.insert(cz1);
        S.insert(cz2);
        S.insert(cz3);
    } else {
        if (lb_l != S.end() && is_l_in) {
            fragment cz1{};
            fragment cz2{};
            cz1.l = lb_l.operator->()->l;
            cz1.r = l - 1;
            cz2.l = l;
            cz2.r = lb_l.operator->()->r;
            S.erase(lb_l);
            S.insert(cz1);
            S.insert(cz2);
        }
        if (lb_r != S.end() && is_r_in) {
            fragment cz1{};
            fragment cz2{};
            cz1.l = lb_r.operator->()->l;
            cz1.r = r;
            cz2.l = r + 1;
            cz2.r = lb_r.operator->()->r;
            S.erase(lb_r);
            S.insert(cz1);
            S.insert(cz2);
        }
    }

    //zlicza wszystkie pomalowane ciagi zawierajace sie w [l, r] ktore nie zawieraja l i r
    fragment eg = {};
    eg.l = l;
    auto it_inside = S.lower_bound(eg);
    //printf("found: %i %i\n", it_inside.operator->()->l, it_inside.operator->()->r);
    while (it_inside != S.end() && l <= it_inside->l && it_inside->r <= r) {
        painted = painted + it_inside->r - it_inside->l + 1;
        auto remove = it_inside;
        it_inside++;
        //printf("removing: %i %i\n", remove.operator->()->l, remove.operator->()->r);
        S.erase(remove);
    }

    if (a == paint) {
        fragment cz1{};
        cz1.l = l;
        cz1.r = r;
        S.insert(cz1);
        //dodaje jeden ciag [l, r]
    }
    return painted;
}

void print(set<fragment> &S) {
    for (auto elem : S) {
        printf("%i - %i     ", elem.l, elem.r);
    }
    printf("\n");
}

void paint_set_with_set(set<fragment> &S, set<fragment> &BRUSH, int &sum) {
    for (auto el : BRUSH) {
        int painted = operation(el.l, el.r, paint, S);
        int all = el.r - el.l + 1;
        // zobaczyc ile w przedziale l r jest pomalowanych
        //domalowac w przedziale l r niedomalowane
        //do sumy dodac ilosc niedomalowanych to jest all - pomalowane
        sum += all - painted;
    }
}

void clear_set_with_set(set<fragment> &S, set<fragment> &RUBBER, int &sum) {
    for (auto el : RUBBER) {
        // zobaczyc ile w przedziale l r jest pomalowanych
        // wszystkie pomalowane w przedziale l r wyrąbac
        //od sumy odjac ilosc pomalowanych
        int painted = operation(el.l, el.r, unpaint, S);
        sum-=painted;
    }
}

//void merge_fragments(set<fragment> &A) {
//    auto it = A.begin();
//    auto previ = it;
//    it++;
//    while (it != A.end()) {
//        printf("previous: %i %i, now: %i %i\n", previ.operator->()->l, previ.operator->()->r,it.operator->()->l, it.operator->()->r);
//        if (previ.operator->()->r == it.operator->()->l) {
//
//        }
//        previ++;
//        it++;
//    }
//}

int main() {
//    ios base::sync with stdio(false);
//    cin.tie(nullptr);
    int n = readInt();
    int m = readInt();
    int sum = 0;
    set<fragment> Z;
    set<fragment> N;
    set<fragment> C;

    for (int i = 1; i <= m; i++) {
        fragment a = fragment{readInt(),readInt()};
        int typ = readInt();
        if (typ == 1) {
            Z.insert(a);
        } else if (typ == 2) {
            N.insert(a);
        } else if (typ == 3){
            C.insert(a);
        }
    }
    int whatever = 0;

    set<fragment> W_Z;
    int sumW_Z = 0;
    paint_set_with_set(W_Z, Z, sumW_Z);
    clear_set_with_set(W_Z, C, sumW_Z);
    set<fragment> W_N;
    int sumW_N = 0;
    paint_set_with_set(W_N, N, sumW_N);
    clear_set_with_set(W_N, C, sumW_N);

    set<fragment> W_NminusW_Z = W_N;
    clear_set_with_set(W_NminusW_Z, W_Z, whatever);

    set<fragment> W_ZminusW_N = W_Z;
    clear_set_with_set(W_ZminusW_N, W_N, whatever);

    set<fragment> W_N_sumW_Z = W_Z;
    int sumW_N_sumW_Z = sumW_Z;
    paint_set_with_set(W_N_sumW_Z, W_N, sumW_N_sumW_Z);
    clear_set_with_set(W_N_sumW_Z, W_NminusW_Z, sumW_N_sumW_Z);
    clear_set_with_set(W_N_sumW_Z, W_ZminusW_N, sumW_N_sumW_Z);
    printf("%i\n", sumW_N_sumW_Z);
}