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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define FOR(x, b, e) for(long x = b; x <= (e); x++)
#define FORD(x, b, e) for(long x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
#define LL long long

#define DEBUGMODE true

using namespace std;

class Event
{
    public:
    Event(long p_pos, long p_b, bool p_start) { pos=p_pos; b=p_b; start=p_start; };
    long pos;
    long b; // barwnik
    bool start;
};

bool fn(const Event *x, const Event *y)
{
    if (x->pos==y->pos) {
        return (x->start==true && y->start==false);
    } else return (x->pos<y->pos);
}

vector<Event*> events;

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	long n,m;
	cin >> n; cin >> m;
	long l,r,ba;
	FOR(i,1,m) {
	    cin >> l; cin >> r; cin >> ba;
        events.push_back(new Event(l,ba,true)); events.push_back(new Event(r+1,ba,false));
	}
	events.push_back(new Event(n+1,1,true));
	sort(events.begin(), events.end(), fn);
	long b[4]; b[1]=0; b[2]=0; b[3]=0;
	long ile_ziel=0;

	long next=0;
	FOR(p,1,n) {
	    while (events[next]->pos==p) {
            // we have to handle event first:
            if (events[next]->start) b[events[next]->b]++; else b[events[next]->b]--;
            next++;
	    }
	    //cout << p << ": " << b[1] << "/" << b[2] << "/" << b[3] << "\n";
	    if (b[1]>0 && b[2]>0 && b[3]==0) ile_ziel++;
	}
    cout << ile_ziel << "\n";
    return 0;
};