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
#define _CRT_SECURE_NO_WARNINGS 
#define ONLINE_JUDGE
#include <cstdio>
#include <vector>
#include <cassert>
using namespace std;

enum Color
{
    White = 0,
    Yellow = 1,
    Blue = 2,
    Red = 4,
    Green = Yellow | Blue,
    Orange = Yellow | Red,
    Purple = Blue | Red,
    Brown = Yellow | Blue | Red
};

struct Operation
{
    Operation()
    {
    }

    Operation(int b, int e, Color c)
    {
        beginning = b;
        end = e;
        color = c;
    }
    int beginning;
    int end;
    Color color;
};

struct Input
{
    int cans;
    vector<Operation> operations;
};

Input readInputFromStdIn()
{
    Input result;
    int operations, beginning, end, color;
    
    scanf("%d %d", &result.cans, &operations);
    for (int i = 0; i < operations; ++i)
    {
        scanf("%d %d %d", &beginning, &end, &color);
        --color;
        result.operations.push_back(Operation(beginning, end, (Color)(1 << color)));
    }

    return result;
}

class Tree
{
private:
    int numberOfLeafs;
    vector<int> treeRepresentation;
public:
    Tree(int numberOfElementsToStore)
    {
        numberOfLeafs = numberOfElementsToStore;
        treeRepresentation = vector<int>(2 * numberOfLeafs, 0);
    }

    void addSegmentValue(int beginning, int end, Color color)
    {
        int treeBeginning = beginning + numberOfLeafs - 1;
        int treeEnd = end + numberOfLeafs - 1;

        while (treeBeginning < treeEnd)
        {
            if (treeBeginning & 1)
                treeRepresentation[treeBeginning++] |= color;
            if (!(treeEnd & 1))
                treeRepresentation[treeEnd--] |= color;

            treeBeginning >>= 1;
            treeEnd >>= 1;
        }

        if (treeBeginning == treeEnd)
            treeRepresentation[treeBeginning] |= color;
    }

    Color getPointValue(int point)
    {
        point += numberOfLeafs - 1;
        int result = 0;

        while (point > 0)
        {
            result |= treeRepresentation[point];
            point >>= 1;
        }

        return (Color)result;
    }
};

class Solver
{
public:
    static int solve(const Input& input)
    {
        Tree tree(input.cans);
        for (auto operation : input.operations)
            tree.addSegmentValue(operation.beginning, operation.end, operation.color);
        
        int result = 0;
        for (int can = 1; can <= input.cans; ++can)
        {
            const Color currentCanColor = tree.getPointValue(can);
            if (currentCanColor == Green)
                ++result;
        }

        return result;
    }
};

void runTest1()
{
    Input input;
    input.cans = 9;
    input.operations = vector<Operation>
    {
        Operation(2, 8, Yellow),
        Operation(4, 5, Blue),
        Operation(6, 7, Red),
        Operation(5, 6, Blue),
        Operation(1, 2, Blue)
    };

    assert(Solver::solve(input) == 3);
}

void runTest2()
{
    Input input;
    input.cans = 1;
    input.operations;

    assert(Solver::solve(input) == 0);

    input.operations = vector<Operation>{ Operation(1, 1, Yellow) };
    assert(Solver::solve(input) == 0);

    input.operations = vector<Operation>{ Operation(1, 1, Yellow), Operation(1, 1, Yellow) };
    assert(Solver::solve(input) == 0);

    input.operations = vector<Operation>{ Operation(1, 1, Yellow), Operation(1, 1, Red) };
    assert(Solver::solve(input) == 0);

    input.operations = vector<Operation>{ Operation(1, 1, Yellow), Operation(1, 1, Blue) };
    assert(Solver::solve(input) == 1);

    input.operations = vector<Operation>{ Operation(1, 1, Yellow), Operation(1, 1, Blue), Operation(1, 1, Red) };
    assert(Solver::solve(input) == 0);
}

void runTest3()
{
    Input input;
    input.cans = 1e6;
    input.operations;

    assert(Solver::solve(input) == 0);

    input.operations = vector<Operation>{ Operation(1, 1e6, Yellow) };
    assert(Solver::solve(input) == 0);

    input.operations = vector<Operation>{ Operation(1, 1e6, Yellow), Operation(1, 1e6, Blue) };
    assert(Solver::solve(input) == 1e6);

    input.operations = vector<Operation>{ Operation(1, 5e5, Yellow), Operation(1, 5e5, Blue) };
    assert(Solver::solve(input) == 5e5);

    input.operations = vector<Operation>{ Operation(1, 5e5, Yellow), Operation(5e5, 1e6, Blue) };
    assert(Solver::solve(input) == 1);

    vector<Operation> operations;
    for (int i = 0; i < 1e6 - 1; ++i)
    {
        operations.push_back(Operation(i + 1, i + 1, Yellow));
        operations.push_back(Operation(i + 1, i + 1, Blue));
    }

    input.operations = operations;
    assert(Solver::solve(input) == 1e6 - 1);
}

void runTests()
{
    runTest1();
    runTest2();
    runTest3();
    printf("Tests finished.\n");
}

int main()
{
#ifndef ONLINE_JUDGE
    runTests();
#endif
    Input input = readInputFromStdIn();
    printf("%d\n", Solver::solve(input));
#ifndef ONLINE_JUDGE
    system("pasue");
#endif
    return 0;
}