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
#include<bits/stdc++.h>
using namespace std;
const int M =(1<<20);
const int N=2*M+5;
int Tree[N][3];
int Tree2[N][3];
int ans[N];
void update_ansR(int v,int col,int siz)
{
    Tree[v][col]=siz;
    if(col==2)
    {
        Tree[v][0]=0;
        Tree[v][1]=0;
    }
    v/=2;
    siz*=2;
    while(v>0)
    {
        Tree[v][2]=max(Tree[v][2],Tree[2*v][2]+Tree[2*v+1][2]);
        if(Tree2[v][0])  Tree[v][0]=siz-Tree[v][2];
        else    Tree[v][0]=Tree[2*v][0]+Tree[2*v+1][0];
        if(Tree2[v][1])  Tree[v][1]=siz-Tree[v][2];
        else    Tree[v][1]=Tree[2*v][1]+Tree[2*v+1][1];
         if(Tree2[v][2])
        {
            Tree[v][0]=0;
            Tree[v][1]=0;
        }
        v/=2;
        siz*=2;
    }
}

void update_ans(int v)
{
    if(Tree2[v][0]&&Tree2[v][1]) ans[v]=Tree[v][1]-Tree[v][2];
    else ans[v]=(Tree2[v][2] || v>=M? 0:min(Tree[v][0],Tree[v][1]));
    
    v/=2;
    while(v>0)
    {
        ans[v]= (Tree2[v][2]? 0:(Tree2[v][1] || Tree2[v][0]? min(Tree[v][0],Tree[v][1]):(ans[v*2]+ans[v*2+1])));
        v/=2;
    }
}
void update(int x1,int x2,int col,int v=1,int s=0, int end=M-1)
{
    if(x2 < s || x1> end || s>end )  return ;
    //if(Tree2[v][2] || Tree2[v][col]) return;
    if(x1<= s && x2>=end)  
    {
        //cout<<"saa"<<v<<"\n";
        Tree2[v][col]=1;
        update_ansR(v,col,end-s+1);
        update_ans(v);
        return;
    }
    int mid=(s+end)/2;
    update(x1,x2,col,v*2,s,mid);
    update(x1,x2,col,v*2+1,mid+1,end);
    return;
}
void print ()
{
    for (int i = 1; i < N; i++)
    {
        cout<<i%10<<" ";
    }
    cout<<"\n";
    for (int i = 1; i < N; i++)
    {
        cout<<Tree[i][0]<<" ";
    }
    cout<<"\n";
    for (int i = 1; i < N; i++)
    {
        cout<<Tree[i][1]<<" ";
    }
    cout<<"\n";
    for (int i = 1; i < N; i++)
    {
        cout<<Tree[i][2]<<" ";
    }
    cout<<"\n";

    for (int i = 1; i < N; i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<"\n\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,z,x1,x2,col;
    cin>>n>>z;
    for (int i = 0; i < z; i++)
    {
        cin>>x1>>x2>>col;
        //suma(x1,x2);
        update(x1,x2,col-1);
        //print();
    }

    cout<<ans[1]<<"\n";
    //print();

}
// 9 5
// 2 8 1
// 4 5 2
// 6 7 3
// 5 6 2
// 1 2 2