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
#include<bits/stdc++.h>

using namespace std;

struct classcomp
{
    bool operator() (const pair<int,int>& lhs, const pair<int,int>& rhs) const
    {
        return (lhs.second < rhs.first);
    }
};

pair<int,int> Znajdz(set<pair<int,int>, classcomp>* zbior, int i)
{
    auto it = (*zbior).lower_bound({i,i});
    if (i >= (*it).first)
    {
        return {(*it).first, (*it).second};
    }
    else
    {
        return {-1,-1};
    }
}

void Dodaj(set<pair<int,int>, classcomp>* zbior, int p, int k)
{
    auto it1 = (*zbior).lower_bound({p-1, p-1});
    auto it2 = (*zbior).lower_bound({k+1, k+1});

    bool z1 = (p-1 < (*it1).first);
    bool z2 = (k+1 < (*it2).first);

    int x1 = (*it1).first;
    int x2 = (*it2).second;

    if (z1 && !z2)
    {
        (*zbior).erase(it2);
        (*zbior).insert({p, x2});
    }
    else if (!z1 && z2)
    {
        (*zbior).erase(it1);
        (*zbior).insert({x1, k});
    }
    else if (z1 && z2)
    {
        (*zbior).insert({p,k});
    }
    else
    {
        (*zbior).erase(it1);
        (*zbior).erase(it2);
        (*zbior).insert({x1, x2});
    }
}

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;

    set<pair<int,int>, classcomp> set_z;
    set_z.insert({INT_MAX,INT_MAX});
    set<pair<int,int>, classcomp> set_n;
    set_n.insert({INT_MAX,INT_MAX});
    set<pair<int,int>, classcomp> set_c;
    set_c.insert({INT_MAX,INT_MAX});

    vector<pair<int,int>> v_z;
    vector<pair<int,int>> v_n;
    vector<pair<int,int>> v_c;

    int l,r,k;
    for (int i = 0; i < m; ++i)
    {
        cin >> l >> r >> k;

        if (k==1)
            v_z.push_back({l,-r});
        else if (k==2)
            v_n.push_back({l,-r});
        else if (k==3)
            v_c.push_back({l,-r});
    }

    sort(v_z.begin(), v_z.end());
    sort(v_n.begin(), v_n.end());
    sort(v_c.begin(), v_c.end());

    int maximum;
    maximum=-1;
    for (int i = 0; i < v_z.size(); ++i)
    {
        if (-v_z[i].second > maximum)
        {
            Dodaj(&set_z, v_z[i].first, -v_z[i].second);
            maximum=-v_z[i].second;
        }
    }
    maximum=-1;

    for (int i = 0; i < v_n.size(); ++i)
    {
        if (-v_n[i].second > maximum)
        {
            Dodaj(&set_n, v_n[i].first, -v_n[i].second);
            maximum=-v_n[i].second;
        }
    }
    maximum=-1;
    for (int i = 0; i < v_c.size(); ++i)
    {
        if (-v_c[i].second > maximum)
        {
            Dodaj(&set_c, v_c[i].first, -v_c[i].second);
            maximum=-v_c[i].second;
        }
    }


    pair<int,int> p_z;
    pair<int,int> p_n;
    pair<int,int> p_c;
    int wynik = 0;

    for (int i = 1; i <= n; ++i)
    {
        p_z = Znajdz(&set_z, i);
        p_n = Znajdz(&set_n, i);
        p_c = Znajdz(&set_c, i);

        if (p_z.first != -1 && p_n.first != -1 && p_c.first == -1)
        {
            wynik++;
        }
    }
    cout << wynik;
}