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

struct Node{
    int rest = 0;
    int blue = 0;
    int yello = 0;
    int green = 0;
    bool toBeColored[3] = {false, false, false};

    Node(int a=0, int b=0, int c=0, int d=0): rest(a), blue(b), yello(c), green(d) {}
    Node& operator=(const Node &n);
    Node& operator=(const int &i);

    Node operator+(Node const &a){
        Node n;
        n.rest = a.rest + this->rest;
        n.blue = a.blue + this->blue;
        n.yello = a.yello + this->yello;
        n.green = a.green + this->green;

        return n;
    }
};

Node& Node::operator=(const Node &n){
    if(this != &n){
        rest = n.rest;
        blue = n.blue;
        yello = n.yello;
        green = n.green;
    }

    return *this;
}

Node& Node::operator=(const int &i){
    rest = i;
    blue = i;
    yello = i;
    green = i;

    return *this;
}

ostream& operator<<(ostream &os, const Node &n){
    os<<"{r="<<n.rest<<",b="<<n.blue<<",y="<<n.yello<<",g="<<n.green<<"}\n";
    return os;
}

int n, m;
int leaf;
vector<Node> tree;

int findTreeSize(int n){
    int h = log2(n);
    int size = pow(2, h);
    if(size < n) size*=2;
    return size;
}

void initTree(){
    leaf = findTreeSize(n);
    tree.resize(2*leaf + 1);
}

int sum(const Node &n){
    return n.blue + n.green + n.yello + n.rest;
}

void color(const int &v, const int &k){
    const int h = log2(v);
    const int d = log2(leaf);
    const int total = pow(2, d-h);
    const int white = total - sum(tree[v]);
    switch(k){
        case 1: // yellow
            tree[v].green += tree[v].blue;
            tree[v].yello += white;
            tree[v].blue = 0;
            break;
        case 2: // blue
            tree[v].green += tree[v].yello;
            tree[v].blue += white;
            tree[v].yello = 0;
            break;
        case 3: // red
            tree[v] = 0;
            tree[v].rest = total;
            break;
    }
    tree[v].toBeColored[k-1] = true;
}

void push(const int &v){
    for(int i=0 ; i<3; i++){
        if(tree[v].toBeColored[i]){
            color(2*v, i+1);
            color(2*v+1, i+1);
        }
        tree[v].toBeColored[i] = false;
    }
}

/**
 * Finding segments in indexes from 0 to leaf-1
 * @param v index
 * @param a left to find
 * @param b right to find
 * @param L left bound
 * @param R right bound
 **/
void findSeg(const int &v, const int &a, const int &b, const int &L, const int &R, const int &k){
    if(a==L && b==R){
        color(v, k);
        return;
    }
    push(v);
    
    const int mid = (L+R)/2;
    if(b <= mid){
        findSeg(2*v, a, b, L, mid, k);
    }else if(a > mid){
        findSeg(2*v+1, a, b, mid+1, R, k);
    }else{
        findSeg(2*v, a, mid, L, mid, k);
        findSeg(2*v+1, mid+1, b, mid+1, R, k);
    }
    tree[v] = tree[2*v] + tree[2*v+1]; // update tree
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    scanf("%d%d",&n,&m);
    initTree();

    for(int i=0 ; i<m ; i++){
        int l, r, k;
        scanf("%d%d%d", &l, &r, &k);
        findSeg(1, l-1, r-1, 0, leaf-1, k);
    }
    printf("%d", tree[1].green);
    return 0;
}