Problem:
Donut Decoration
Donut maker's morning is early. Mr. D, who is also called Mr. Donuts, is an awesome donut maker. Also today, he goes to his kitchen and prepares to make donuts before sunrise.
In a twinkling, Mr. D finishes frying $N$ donuts with a practiced hand. But these donuts as they are must not be displayed in a showcase. Filling cream, dipping in chocolate, topping somehow cute, colorful things, etc., several decoration tasks are needed. There are $K$ tasks numbered 1 through $K$, and each of them must be done exactly once in the order $1, 2, ..., K$ to finish the donuts as items on sale.
Instantly, Mr. D arranges the $N$ donuts in a row. He seems to intend to accomplish each decoration tasks sequentially at once. However, what in the world is he doing? Mr. D, who stayed up late at yesterday night, decorates only a part of the donuts in a consecutive interval for each task. It's clearly a mistake! Not only that, he does some tasks zero or several times, and the order of tasks is also disordered. The donuts which are not decorated by correct process cannot be provided as items on sale, so he should trash them.
Fortunately, there are data recording a sequence of tasks he did. The data contain the following information: for each task, the consecutive interval $[l, r]$ of the decorated donuts and the ID $x$ of the task. Please write a program enumerating the number of the donuts which can be displayed in a showcase as items on sale for given recorded data.
Input
The input consists of a single test case. The test case is formatted as follows.
$N$ $K$
$T$
$l_1$ $r_1$ $x_1$
...
$l_T$ $r_T$ $x_T$
The first line contains two integers $N$ and $K$, where $N$ ($1 \leq N \leq 200,000$) is the number of the donuts fried by Mr. D, and $K$ ($1 \leq K \leq 200,000$) is the number of decoration tasks should be applied to the donuts. The second line contains a single integer $T$ ($1 \leq T \leq 200,000$), which means the number of information about tasks Mr. D did. Each of next $T$ lines contains three
integers $l_i$, $r_i$, and $x_i$ representing the $i$-th task Mr. D did: the $i$-th task was applied to the interval $[l_i, r_i]$ ($1 \leq l_i \leq r_i \leq N$) of the donuts inclusive, and has ID $x_i$ ($1 \leq x_i \leq K$).
Output
Output the number of the donuts that can be provided as items on sale.
Sample Input 1
3 2
3
1 2 1
2 3 2
3 3 1
Output for the Sample Input 1
1
Sample Input 2
5 3
6
2 3 1
1 3 2
4 5 1
2 4 3
3 5 2
5 5 3
Output for the Sample Input 2
2
Sample Input 3
10 1
2
2 9 1
5 7 1
Output for the Sample Input 3
5

Submitted solutions:

code_snippet: #include<vector>
#include<cstdio>
#include<cstdlib> 
#include<cstring>
#include<algorithm>
using namespace std;
#define M 800010
#define rep(i,x,y) for(int i=(x);i<=(y);i++)
#define For(i,x,y) for(int i=(x);i<(y);i++)
inline int read(){
	char ch=getchar();int x=0,f=1;
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,k,T,q[M];

inline void pushdown(int o){
	if(q[o]==0||q[o]==-2)return;
	if(q[2*o]!=-1)q[2*o]=q[o];
	if(q[2*o+1]!=-1)q[2*o+1]=q[o];
}

inline void add(int l,int r,int o,int x,int y,int z){
	if(x<=l&&r<=y){
		if(q[o]==z-1){q[o]=z;return;}
		else if(q[o]!=-2){q[o]=-1;return;}
		if(q[o]==-1)return;
		if(l==r){q[o]=-1;return;}int mid=(l+r)/2;pushdown(o);
		add(l,mid,2*o,x,y,z);add(mid+1,r,2*o+1,x,y,z);
		if(q[2*o]==q[2*o+1])q[o]=q[2*o];
		else if(q[2*o]==-1) q[o]=q[2*o+1];
		else if(q[2*o+1]==-1) q[o]=q[2*o];
		else q[o]=-2;return;
	}
	int mid=(l+r)/2;pushdown(o);
	if(x<=mid&&q[2*o]!=-1)add(l,mid,2*o,x,y,z);
	if(y>mid&&q[2*o+1]!=-1)add(mid+1,r,2*o+1,x,y,z);
	if(q[2*o]==q[2*o+1])q[o]=q[2*o];
	else if(q[2*o]==-1) q[o]=q[2*o+1];
	else if(q[2*o+1]==-1) q[o]=q[2*o];
	else q[o]=-2;
}

inline int calc(int l,int r,int o){
	if(l==r){if(q[o]==k)return 1;return 0;}
	int mid=(l+r)/2;pushdown(o);
	return calc(l,mid,2*o)+calc(mid+1,r,2*o+1);
}

int main(){
	scanf("%d",&n);k=read(),T=read();while(T--){
			int l=read(),r=read(),x=read();
			add(1,n,1,l,r,x);
		}printf("%d\n",calc(1,n,1));
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5048
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int Correct = 0;
int Wrong = 0;
set<pair<int, int>> Set;

void Add(pair<int, int> r) {
    auto itr = Set.lower_bound(r);
    auto itrR = itr; pair<int, int> prvR = make_pair(-1, -1);
    auto itrL = itr; pair<int, int> prvL = make_pair(-1, -1);
    if (itrL != Set.begin()) { itrL--; prvL = (*itrL); }
    if (itrR != Set.end()) { prvR = (*itrR); }

    // Decrease
    if (prvL != make_pair(-1, -1) && prvR != make_pair(-1, -1)) {
        if ((prvR.second - prvL.second) == 1) Correct -= 1;
        else Wrong -= 1;
    }

    // Increase
    if (prvL != make_pair(-1, -1)) {
        if ((r.second - prvL.second) == 1) Correct += 1;
        else Wrong += 1;
    }
    if (prvR != make_pair(-1, -1)) {
        if ((prvR.second - r.second) == 1) Correct += 1;
        else Wrong += 1;
    }
    Set.insert(r);
}

void Delete(pair<int, int> r) {
    auto itr = Set.lower_bound(r);
    auto itrR = itr; pair<int, int> prvR = make_pair(-1, -1); itrR++;
    auto itrL = itr; pair<int, int> prvL = make_pair(-1, -1);
    if (itrL != Set.begin()) { itrL--; prvL = (*itrL); }
    if (itrR != Set.end()) { prvR = (*itrR); }

    // Increase
    if (prvL != make_pair(-1, -1) && prvR != make_pair(-1, -1)) {
        if ((prvR.second - prvL.second) == 1) Correct += 1;
        else Wrong += 1;
    }

    // Decrease
    if (prvL != make_pair(-1, -1)) {
        if ((r.second - prvL.second) == 1) Correct -= 1;
        else Wrong -= 1;
    }
    if (prvR != make_pair(-1, -1)) {
        if ((prvR.second - r.second) == 1) Correct -= 1;
        else Wrong -= 1;
    }
    Set.erase(r);
}

int main() {
    // Step 1. Input
    int N; cin >> N;
    int K; cin >> K;
    int T; cin >> T;
    vector<int> L(T, 0), R(T, 0), X(T, 0);
    for (int i = 0; i < T; i++) cin >> L[i] >> R[i] >> X[i];
    for (int i = 0; i < T; i++) L[i] -= 1;
    for (int i = 0; i < T; i++) R[i] -= 1;

    // Step 2. Solve Init
    vector<vector<pair<int, int>>> List(N + 1);
    for (int i = 0; i < T; i++) List[L[i] + 0].push_back(make_pair(i, +1));
    for (int i = 0; i < T; i++) List[R[i] + 1].push_back(make_pair(i, -1));

    // Step 3. Solve Main
    int Count = 0;
    for (int i = 0; i < N; i++) {
        for (pair<int, int> x : List[i]) {
            if (x.second == +1) Add(make_pair(x.first, X[x.first]));
            if (x.second == -1) Delete(make_pair(x.first, X[x.first]));
        }
        if (Correct == K - 1 && Wrong == 0 && Set.size() == K) Count += 1;
        // cerr << i << " " << Correct << " " << Wrong << endl;
    }
    cout << Count << endl;
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 23492
final_rank: 14

code_snippet: // AOJ #2734
// Donut Decoration 2025.2.11

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}
 
const int TREE_LEAF_COUNT = 1 << 18;
const int TREE_SIZE = 1 << 19;
const int INVALID_STATE = 100000000;

vector< pair<int,int> > segmentTree(TREE_SIZE, make_pair(0, 0));

inline void applyLazyUpdate(int nodeIndex, const pair<int,int>& updatePair) {
    if (updatePair.first == 0 && updatePair.second == 0)
        return;
    if (segmentTree[nodeIndex] == make_pair(0, 0))
        segmentTree[nodeIndex] = updatePair;
    else if (segmentTree[nodeIndex].second + 1 == updatePair.first)
        segmentTree[nodeIndex].second = updatePair.second;
    else segmentTree[nodeIndex] = make_pair(INVALID_STATE, INVALID_STATE);
}

void updateRange(int updateLeft, int updateRight, int task,
                 int nodeIndex = 0, int nodeLeft = 0, int nodeRight = TREE_LEAF_COUNT) {
    if (updateRight <= nodeLeft || nodeRight <= updateLeft)
        return;
    if (updateLeft <= nodeLeft && nodeRight <= updateRight) {
        applyLazyUpdate(nodeIndex, make_pair(task, task));
        return;
    }
    applyLazyUpdate(nodeIndex * 2 + 1, segmentTree[nodeIndex]);
    applyLazyUpdate(nodeIndex * 2 + 2, segmentTree[nodeIndex]);
    segmentTree[nodeIndex] = make_pair(0, 0);
    
    int mid = (nodeLeft + nodeRight) / 2;
    updateRange(updateLeft, updateRight, task, nodeIndex * 2 + 1, nodeLeft, mid);
    updateRange(updateLeft, updateRight, task, nodeIndex * 2 + 2, mid, nodeRight);
}

void propagateLazy(int nodeIndex, int nodeLeft, int nodeRight) {
    if (nodeRight - nodeLeft == 1) return;
    applyLazyUpdate(nodeIndex * 2 + 1, segmentTree[nodeIndex]);
    applyLazyUpdate(nodeIndex * 2 + 2, segmentTree[nodeIndex]);
    segmentTree[nodeIndex] = make_pair(0, 0);
    
    int mid = (nodeLeft + nodeRight) / 2;
    propagateLazy(nodeIndex * 2 + 1, nodeLeft, mid);
    propagateLazy(nodeIndex * 2 + 2, mid, nodeRight);
}

int main(){
    int N = Cin(), K = Cin();
    int T = Cin();
    
    for (int i = 0; i < T; i++){
        int l = Cin()-1, r = Cin(), x = Cin();
        updateRange(l, r, x);
    }
    
    propagateLazy(0, 0, TREE_LEAF_COUNT);
    
    int validDonutCount = 0;
    std::function< pair<int,int>(int, int, int, int) > queryLeaf =
        [&](int pos, int nodeIndex, int nodeLeft, int nodeRight) -> pair<int,int> {
            if (nodeRight - nodeLeft == 1)
                return segmentTree[nodeIndex];
            int mid = (nodeLeft + nodeRight) / 2;
            if (pos < mid)
                return queryLeaf(pos, nodeIndex * 2 + 1, nodeLeft, mid);
            else return queryLeaf(pos, nodeIndex * 2 + 2, mid, nodeRight);
        };
    
    for (int i = 0; i < N; i++){
        pair<int,int> leafValue = queryLeaf(i, 0, 0, TREE_LEAF_COUNT);
        if (leafValue == make_pair(1, K)) validDonutCount++;
    }
    Cout(validDonutCount);
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 7144
final_rank: 3

code_snippet: // AOJ #2734
// Donut Decoration 2025.2.11

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}
 
const int TREE_LEAF_COUNT = 1 << 18;
const int TREE_SIZE = 1 << 19;
const int INVALID_STATE = 100000000;

vector< pair<int,int> > segmentTree(TREE_SIZE, make_pair(0, 0));

void applyLazyUpdate(int nodeIndex, const pair<int,int>& updatePair) {
    if (updatePair.first == 0 && updatePair.second == 0) return;
    if (segmentTree[nodeIndex] == make_pair(0, 0))
        segmentTree[nodeIndex] = updatePair;
    else if (segmentTree[nodeIndex].second + 1 == updatePair.first)
        segmentTree[nodeIndex].second = updatePair.second;
    else segmentTree[nodeIndex] = make_pair(INVALID_STATE, INVALID_STATE);
}

void updateRange(int updateLeft, int updateRight, int taskNumber,
                 int nodeIndex = 0, int nodeLeft = 0, int nodeRight = TREE_LEAF_COUNT)
{
    if (updateRight <= nodeLeft || nodeRight <= updateLeft) return;

    if (updateLeft <= nodeLeft && nodeRight <= updateRight) {
        applyLazyUpdate(nodeIndex, make_pair(taskNumber, taskNumber));
        return;
    }
    applyLazyUpdate(nodeIndex * 2 + 1, segmentTree[nodeIndex]);
    applyLazyUpdate(nodeIndex * 2 + 2, segmentTree[nodeIndex]);
    segmentTree[nodeIndex] = make_pair(0, 0);
    
    int mid = (nodeLeft + nodeRight) / 2;
    updateRange(updateLeft, updateRight, taskNumber, nodeIndex * 2 + 1, nodeLeft, mid);
    updateRange(updateLeft, updateRight, taskNumber, nodeIndex * 2 + 2, mid, nodeRight);
}

int main() {
    int N = Cin(), K = Cin();
    int T = Cin();
    
    for (int i = 0; i < T; i++){
        int l = Cin()-1, r = Cin(), x = Cin();
        updateRange(l, r, x);
    }
    
    for (int i = 0; i < N; i++) updateRange(i, i + 1, 0);
    
    int validDonutCount = 0;
    int leafStartIndex = TREE_LEAF_COUNT - 1;
    for (int i = 0; i < N; i++){
        int leafIndex = i + leafStartIndex;
        if (segmentTree[leafIndex] == make_pair(1, K))
            validDonutCount++;
    }
    Cout(validDonutCount);
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 7144
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    set<pair<int, int>> st;
    st.insert({0, 0});
    st.insert({n, k + 1});


    vector<pair<int, int>> ngvec;

    int inf = 1 << 30;

    int q;
    cin >> q;
    rep(_, q) {
        int le, ri, x;
        cin >> le >> ri >> x;
        le --;
        
        {
            auto itr = st.upper_bound(make_pair(le, inf));
            itr --;
            int xp = itr->second;
            if(itr->first == le) itr = st.erase(itr);
            else itr = next(itr);
            int p = le;
            while(1) {
                if(itr->first < ri) {
                    if(xp != x - 1) {
                        
                        ngvec.push_back({p, itr->first});
                    }
                    p = itr->first;
                    xp = itr->second;
                    itr = st.erase(itr);
                } else {
                    if(xp != x - 1) {
                        // if(p <= 4 and 4 < itr->first) {
                        //     cout << le << " " << ri << " " << x << " " << p << " " << itr->first << " " << xp <<  endl;
                        // }
                        ngvec.push_back({p, ri});
                    }
                    break;
                }
            }
            st.insert({le, x});
            if(st.lower_bound(make_pair(ri, -1))->first != ri) st.insert({ri, xp});
        }
        // for(auto [a, b] : st) {
        //     cout << a << " " << b << endl;
        // }
        // cout << endl;
    }
    vector<int> cnt(n + 1, 0);
    for(auto [x, y] : ngvec) {
        cnt[x] ++;
        cnt[y] --;
    }
    rep(i, n) cnt[i + 1] += cnt[i];
    vector<int> ok(n + 1, 0);
    int p = -1, px = -1;
    
    for(auto [x, y] : st) {
        if(p != -1 and px == k) {
            for(int i = p; i < x; i ++) ok[i] = 1;
        }
        p = x;
        px = y;
    }
    // cout << cnt[4] << " " << ok[4] << endl;
    int ans = 0;
    rep(i, n) if(cnt[i] == 0 and ok[i]) {
        ans ++;
        // cout << i + 1 << " ";
    }
    // cout << endl;
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 9888
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(const vector<MonoidType>& v) {
        n = 1, h = 0;
        while (n < (int)v.size()) {
            n *= 2;
            h++;
        }
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < (int)v.size(); i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        int p = i + n;
        for (int j = h; j > 0; j--) propagate(p >> j);
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        {
            int pl = l + n, pr = r + n;
            for (int i = h; i > 0; i--) propagate(pl >> i), propagate(pr >> i);
        }
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        {
            int pl = l + n, pr = r + n;
            while (pl >>= 1) dat[pl] = Monoid::op(dat[pl << 1], dat[pl << 1 | 1]);
            while (pr >>= 1) dat[pr] = Monoid::op(dat[pr << 1], dat[pr << 1 | 1]);
        }
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        {
            int pl = l + n, pr = r + n;
            for (int i = h; i > 0; i--) propagate(pl >> i), propagate(pr >> i);
        }
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n, h;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}

int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 0.8787878787878788
time_ms: 230
memory_kb: 13008
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(const vector<MonoidType>& v) {
        n = 1, h = 1;
        while (n < (int)v.size()) {
            n *= 2;
            h++;
        }
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < (int)v.size(); i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        int p = i + n;
        for (int j = h; j > 0; j--) propagate(p >> j);
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        {
            int pl = l + n, pr = r + n - 1;
            for (int i = h; i > 0; i--) propagate(pl >> i), propagate(pr >> i);
        }
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        {
            int pl = l + n, pr = r + n - 1;
            while (pl >>= 1) dat[pl] = Monoid::op(dat[pl << 1], dat[pl << 1 | 1]);
            while (pr >>= 1) dat[pr] = Monoid::op(dat[pr << 1], dat[pr << 1 | 1]);
        }
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        {
            int pl = l + n, pr = r + n - 1;
            for (int i = h; i > 0; i--) propagate(pl >> i), propagate(pr >> i);
        }
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n, h;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}

int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 0.8787878787878788
time_ms: 230
memory_kb: 12940
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(const vector<MonoidType>& v) {
        n = 1, h = 0;
        while (n < (int)v.size()) {
            n *= 2;
            h++;
        }
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < (int)v.size(); i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        int p = i + n;
        for (int j = h; j > 0; j--) propagate(p >> j);
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        {
            int pl = l + n, pr = r + n - 1;
            for (int i = h; i > 0; i--) propagate(pl >> i), propagate(pr >> i);
        }
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        {
            int pl = l + n, pr = r + n - 1;
            while (pl >>= 1) {
                dat[pl] = Monoid::op(dat[pl << 1], dat[pl << 1 | 1]);
            }
            while (pr >>= 1) {
                dat[pr] = Monoid::op(dat[pr << 1], dat[pr << 1 | 1]);
            }
        }
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        {
            int pl = l + n, pr = r + n - 1;
            for (int i = h; i > 0; i--) propagate(pl >> i), propagate(pr >> i);
        }
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n, h;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}

int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 0.8787878787878788
time_ms: 230
memory_kb: 13012
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(int n) {
        this->n = n;
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
    }
    SegTreeLazy(const vector<MonoidType>& v) {
        this->n = v.size();
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < n; i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        generate_indices(i, i + 1);
        pushdown();
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        generate_indices(l, r);
        pushdown();
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        pushup();
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        generate_indices(l, r);
        pushdown();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    vector<int> indices;

    void generate_indices(int l, int r) {
        indices.clear();
        l += n, r += n;
        {
            bool ok = false;
            while (l) {
                if (ok) indices.push_back(l);
                if (l & 1) ok = true;
                l /= 2;
            }
        }
        swap(l, r);
        {
            bool ok = false;
            while (l) {
                if (ok) indices.push_back(l);
                if (l & 1) ok = true;
                l /= 2;
            }
        }
        sort(indices.rbegin(), indices.rend());
        indices.erase(unique(indices.begin(), indices.end()), indices.end());
    }

    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
    void pushdown() {
        for (int j = (int)indices.size() - 1; j >= 0; j--) {
            int i = indices[j];
            propagate(i);
        }
    }
    void pushup() {
        for (int j = 0; j < (int)indices.size(); j++) {
            int i = indices[j];
            dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
        }
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}

int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 350
memory_kb: 10940
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(int n) {
        this->n = n;
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
    }
    SegTreeLazy(const vector<MonoidType>& v) {
        this->n = v.size();
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < n; i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        generate_indices(i, i + 1);
        pushdown();
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        generate_indices(l, r);
        pushdown();
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        pushup();
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        generate_indices(l, r);
        pushdown();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    vector<int> indices;

    void generate_indices(int l, int r) {
        indices.clear();
        l += n, r += n;
        while (l) {
            l >>= 1;
            indices.push_back(l);
        }
        while (r) {
            r >>= 1;
            indices.push_back(r);
        }
        sort(indices.rbegin(), indices.rend());
        indices.erase(unique(indices.begin(), indices.end()), indices.end());
    }

    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
    void pushdown() {
        for (int j = (int)indices.size() - 1; j >= 0; j--) {
            int i = indices[j];
            propagate(i);
        }
    }
    void pushup() {
        for (int j = 0; j < (int)indices.size(); j++) {
            int i = indices[j];
            dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
        }
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}

int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 0.8181818181818182
time_ms: 290
memory_kb: 10968
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(int n) {
        this->n = n;
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
    }
    SegTreeLazy(const vector<MonoidType>& v) {
        this->n = v.size();
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < n; i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        generate_indices(i, i + 1);
        pushdown();
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        generate_indices(l, r);
        pushdown();
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        pushup();
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        generate_indices(l, r);
        pushdown();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    vector<int> indices;

    void generate_indices(int l, int r) {
        indices.clear();
        l += n, r += n;
        while (l) {
            l >>= 1;
            indices.push_back(l);
        }
        while (r) {
            r >>= 1;
            indices.push_back(r);
        }
    }

    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
    void pushdown() {
        for (int j = (int)indices.size() - 1; j >= 0; j--) {
            int i = indices[j];
            propagate(i);
        }
    }
    void pushup() {
        for (int j = 0; j < (int)indices.size(); j++) {
            int i = indices[j];
            dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
        }
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}

int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 0.8181818181818182
time_ms: 260
memory_kb: 10964
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Monoid, typename Operator, auto mapping>
struct SegTreeLazy {
    using MonoidType = typename Monoid::Type;
    using OperatorType = typename Operator::Type;

    SegTreeLazy() = default;
    SegTreeLazy(int n) {
        this->n = n;
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
    }
    SegTreeLazy(const vector<MonoidType>& v) {
        this->n = v.size();
        dat.assign(n << 1, Monoid::id());
        lazy.assign(n << 1, Operator::id());
        for (int i = 0; i < n; i++) dat[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }

    void set(int i, MonoidType x) {
        generate_indices(i, i + 1);
        pushdown();
        i += n;
        dat[i] = x;
        while (i >>= 1) dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
    }
    void apply(int l, int r, OperatorType x) {
        if (l == r) return;
        generate_indices(l, r);
        pushdown();
        l += n, r += n;
        while (l < r) {
            if (l & 1) {
                lazy[l] = Operator::op(lazy[l], x);
                dat[l] = mapping(dat[l], x);
                l++;
            }
            if (r & 1) {
                r--;
                lazy[r] = Operator::op(lazy[r], x);
                dat[r] = mapping(dat[r], x);
            }
            l >>= 1, r >>= 1;
        }
        pushup();
    }
    MonoidType fold(int l, int r) {
        if (l == r) return Monoid::id();
        generate_indices(l, r);
        pushdown();
        MonoidType retl = Monoid::id(), retr = Monoid::id();
        l += n, r += n;
        while (l < r) {
            if (l & 1) retl = Monoid::op(retl, dat[l++]);
            if (r & 1) retr = Monoid::op(dat[--r], retr);
            l >>= 1, r >>= 1;
        }
        return Monoid::op(retl, retr);
    }
    template <typename F>
    int find_right(int l, F f) {
        assert(f(Monoid::id()));
        if (l == n) return n;
        generate_indices(l, n);
        pushdown();
        l += n;
        int r = n + n;
        vector<int> cand_l, cand_r;
        while (l < r) {
            if (l & 1) cand_l.push_back(l++);
            if (r & 1) cand_r.push_back(--r);
            l >>= 1, r >>= 1;
        }
        vector<int> cand = cand_l;
        reverse(cand_r.begin(), cand_r.end());
        cand.insert(cand.end(), cand_r.begin(), cand_r.end());
        MonoidType val = Monoid::id();
        for (int i : cand) {
            if (f(Monoid::op(val, dat[i]))) {
                val = Monoid::op(val, dat[i]);
            } else {
                while (i < n) {
                    propagate(i);
                    i <<= 1;
                    if (f(Monoid::op(val, dat[i]))) {
                        val = Monoid::op(val, dat[i]);
                        i |= 1;
                    }
                }
                return i - n;
            }
        }
        return n;
    }
    template <typename F>
    int find_left(int r, F f) {
        assert(f(Monoid::id()));
        if (r == 0) return 0;
        generate_indices(0, r);
        pushdown();
        r += n;
        int l = n;
        vector<int> cand_l, cand_r;
        while (l < r) {
            if (l & 1) cand_l.push_back(l++);
            if (r & 1) cand_r.push_back(--r);
            l >>= 1, r >>= 1;
        }
        vector<int> cand = cand_r;
        reverse(cand_l.begin(), cand_l.end());
        cand.insert(cand.end(), cand_l.begin(), cand_l.end());
        MonoidType val = Monoid::id();
        for (int i : cand) {
            if (f(Monoid::op(dat[i], val))) {
                val = Monoid::op(dat[i], val);
            } else {
                while (i < n) {
                    propagate(i);
                    i = (i << 1) | 1;
                    if (f(Monoid::op(dat[i], val))) {
                        val = Monoid::op(dat[i], val);
                        i ^= 1;
                    }
                }
                return i - n + 1;
            }
        }
        return 0;
    }

    int size() { return n; }
    MonoidType operator[](int i) { return fold(i, i + 1); }

private:
    int n;
    vector<MonoidType> dat;
    vector<OperatorType> lazy;
    vector<int> indices;
    void generate_indices(int l, int r) {
        indices.clear();
        l += n, r += n;
        int lm = (l / (l & -l)) >> 1, rm = (r / (r & -r)) >> 1;
        while (l < r) {
            if (l <= lm) indices.push_back(l);
            if (r <= rm) indices.push_back(r);
            l >>= 1, r >>= 1;
        }
        while (l) {
            indices.push_back(l);
            l >>= 1;
        }
    }
    void propagate(int i) {
        if (i < n) {
            lazy[i << 1] = Operator::op(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = Operator::op(lazy[i << 1 | 1], lazy[i]);
            dat[i << 1] = mapping(dat[i << 1], lazy[i]);
            dat[i << 1 | 1] = mapping(dat[i << 1 | 1], lazy[i]);
        }
        lazy[i] = Operator::id();
    }
    void pushdown() {
        for (int j = (int)indices.size() - 1; j >= 0; j--) {
            int i = indices[j];
            propagate(i);
        }
    }
    void pushup() {
        for (int j = 0; j < (int)indices.size(); j++) {
            int i = indices[j];
            dat[i] = Monoid::op(dat[i << 1], dat[i << 1 | 1]);
        }
    }
};

struct Monoid {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& l, const Type& r) {
        if (l.first == -1) return r;
        return l;
    }
};

struct Operator {
    using Type = pair<int, int>;
    static Type id() { return {-1, -1}; }
    static Type op(const Type& prev, const Type& next) {
        if (prev.first == -1) return next;
        if (next.first == -1) return prev;
        if (prev.second == next.first) return {prev.first, next.second};
        return {-2, -2};
    }
};

pair<int, int> mapping(const pair<int, int>& x, const pair<int, int>& f) {
    if (f.first == -1) return x;
    if (x.second == f.first) return {x.first, f.second};
    return {-2, -2};
}


int main() {
    int N, K, Q;
    cin >> N >> K >> Q;

    SegTreeLazy<Monoid, Operator, mapping> seg(vector<pair<int, int>>(N, {0, 1}));

    while (Q--) {
        int l, r, x;
        cin >> l >> r >> x;
        l--;
        seg.apply(l, r, {x, x + 1});
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto [f, s] = seg.fold(i, i + 1);
        if (f == 0 && s == K + 1) ans++;
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 230
memory_kb: 10964
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

template <typename M, typename N, M (*f)(M, M), M (*g)(M, N), N (*h)(N, N),
          M (*m1)(), N (*n1)()>
class LazySegmentTree {
    int n, sz, height;
    vector<M> data;
    vector<N> lazy;
    void update(int k) {
        data[k] = f(data[k * 2], data[k * 2 + 1]);
    }
    void apply(int k, N x) {
        data[k] = g(data[k], x);
        if (k < sz)
            lazy[k] = h(lazy[k], x);
    }
    void down(int k) {
        apply(k * 2, lazy[k]);
        apply(k * 2 + 1, lazy[k]);
        lazy[k] = n1();
    }

  public:
    LazySegmentTree(int n = 0) : LazySegmentTree(vector<M>(n, m1())) {}
    LazySegmentTree(const vector<M> &a) : n(SZ(a)) {
        sz = 1, height = 0;
        while (sz < (int)a.size())
            sz <<= 1, height++;
        data.assign(2 * sz, m1());
        lazy.assign(sz, n1());
        rep(i, 0, a.size()) data[sz + i] = a[i];
        for (int i = sz - 1; i; i--)
            update(i);
    }
    M operator[](int k) const {
        k += sz;
        rrep(i, 1, height + 1) down(k >> i);
        return data[k];
    }
    vector<M> get() {
        rep(k, 1, sz) down(k);
        return {data.begin() + sz, data.begin() + sz + n};
    }
    void set(int k, M x) {
        k += sz;
        for (int i = height; i; i--)
            down(k >> i);
        data[k] = x;
        for (int i = 1; i <= height; i++)
            update(k >> i);
    }
    void update(int L, int R, N x) {
        if (L >= R)
            return;
        L += sz, R += sz;
        for (int i = height; i; i--) {
            if (((L >> i) << i) != L)
                down(L >> i);
            if (((R >> i) << i) != R)
                down((R - 1) >> i);
        }
        int lb = L, rb = R;
        while (L < R) {
            if (L & 1)
                apply(L++, x);
            if (R & 1)
                apply(--R, x);
            L >>= 1;
            R >>= 1;
        }
        L = lb, R = rb;
        for (int i = 1; i <= height; i++) {
            if (((L >> i) << i) != L)
                update(L >> i);
            if (((R >> i) << i) != R)
                update((R - 1) >> i);
        }
    }
    M query(int L, int R) {
        if (L >= R)
            return m1();
        L += sz, R += sz;
        for (int i = height; i; i--) {
            if (((L >> i) << i) != L)
                down(L >> i);
            if (((R >> i) << i) != R)
                down((R - 1) >> i);
        }
        M lb = m1(), rb = m1();
        while (L < R) {
            if (L & 1)
                lb = f(lb, data[L++]);
            if (R & 1)
                rb = f(data[--R], rb);
            L >>= 1;
            R >>= 1;
        }
        return f(lb, rb);
    }
    template <class F> int max_right(int L, F ch) {
        if (L == n)
            return n;
        L += sz;
        rrep(i, 1, height + 1) down(L >> i);
        M sum = m1();
        do {
            while (L % 2 == 0)
                L >>= 1;
            if (!ch(f(sum, data[L]))) {
                while (L < sz) {
                    down(L);
                    L <<= 1;
                    if (ch(f(sum, data[L])))
                        sum = f(sum, data[L++]);
                }
                return L - sz;
            }
            sum = f(sum, data[L++]);
        } while ((L & -L) != L);
        return n;
    }
    template <class F> int min_left(int R, F ch) {
        if (R == 0)
            return 0;
        R += sz;
        rrep(i, 1, height + 1) down((R - 1) >> i);
        M sum = m1();
        do {
            R--;
            while (R > 1 and (R & 1))
                R >>= 1;
            if (!ch(f(data[R], sum))) {
                while (R < sz) {
                    down(R);
                    R = (R * 2 + 1);
                    if (ch(f(data[R], sum))) {
                        sum = f(data[R--], sum);
                    }
                }
                return R + 1 - sz;
            }
            sum = f(data[R], sum);
        } while ((R & -R) != R);
        return 0;
    }
};

/**
 * @brief Lazy Segment Tree
 */

pair<int,int> f(pair<int,int> A, pair<int,int> B) {
    return {A.first+B.first,A.second+B.second};
}

pair<int,int> g(pair<int,int> A, pair<int,int> B) {
    if (B.first == -1) return A;
    if (A.second == B.first) return {A.first, B.second};
    return {-1,-1};
}

pair<int,int> h(pair<int,int> A, pair<int,int> B) {
    if (A.first == -1) return B;
    if (B.first == -1) return A;
    if (A.second == B.first) return {A.first, B.second};
    return {-1,-1};
}

pair<int,int> e1() {
    return {0,0};
}

pair<int,int> e2() {
    return {-1,-1};
}

int main() {
    int N, M, Q;
    cin >> N >> M >> Q;
    LazySegmentTree<pair<int,int>,pair<int,int>,f,g,h,e1,e2> Seg(N);
    rep(i,0,N) Seg.set(i,{0,0});
    rep(i,0,Q) {
        int L, R, X;
        cin >> L >> R >> X;
        L--;
        Seg.update(L,R,{X-1,X});
    }
    int ANS = 0;
    rep(i,0,N) {
        pair<int,int> Ret = Seg.query(i,i+1);
        if (Ret.first == 0 && Ret.second == M) ANS++;
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 260
memory_kb: 10784
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

template <typename M, typename N, M (*f)(M, M), M (*g)(M, N), N (*h)(N, N),
          M (*m1)(), N (*n1)()>
class LazySegmentTree {
    int n, sz, height;
    vector<M> data;
    vector<N> lazy;
    void update(int k) {
        data[k] = f(data[k * 2], data[k * 2 + 1]);
    }
    void apply(int k, N x) {
        data[k] = g(data[k], x);
        if (k < sz)
            lazy[k] = h(lazy[k], x);
    }
    void down(int k) {
        apply(k * 2, lazy[k]);
        apply(k * 2 + 1, lazy[k]);
        lazy[k] = n1();
    }

  public:
    LazySegmentTree(int n = 0) : LazySegmentTree(vector<M>(n, m1())) {}
    LazySegmentTree(const vector<M> &a) : n(SZ(a)) {
        sz = 1, height = 0;
        while (sz < (int)a.size())
            sz <<= 1, height++;
        data.assign(2 * sz, m1());
        lazy.assign(sz, n1());
        rep(i, 0, a.size()) data[sz + i] = a[i];
        for (int i = sz - 1; i; i--)
            update(i);
    }
    M operator[](int k) const {
        k += sz;
        rrep(i, 1, height + 1) down(k >> i);
        return data[k];
    }
    vector<M> get() {
        rep(k, 1, sz) down(k);
        return {data.begin() + sz, data.begin() + sz + n};
    }
    void set(int k, M x) {
        k += sz;
        for (int i = height; i; i--)
            down(k >> i);
        data[k] = x;
        for (int i = 1; i <= height; i++)
            update(k >> i);
    }
    void update(int L, int R, N x) {
        if (L >= R)
            return;
        L += sz, R += sz;
        for (int i = height; i; i--) {
            if (((L >> i) << i) != L)
                down(L >> i);
            if (((R >> i) << i) != R)
                down((R - 1) >> i);
        }
        int lb = L, rb = R;
        while (L < R) {
            if (L & 1)
                apply(L++, x);
            if (R & 1)
                apply(--R, x);
            L >>= 1;
            R >>= 1;
        }
        L = lb, R = rb;
        for (int i = 1; i <= height; i++) {
            if (((L >> i) << i) != L)
                update(L >> i);
            if (((R >> i) << i) != R)
                update((R - 1) >> i);
        }
    }
    M query(int L, int R) {
        if (L >= R)
            return m1();
        L += sz, R += sz;
        for (int i = height; i; i--) {
            if (((L >> i) << i) != L)
                down(L >> i);
            if (((R >> i) << i) != R)
                down((R - 1) >> i);
        }
        M lb = m1(), rb = m1();
        while (L < R) {
            if (L & 1)
                lb = f(lb, data[L++]);
            if (R & 1)
                rb = f(data[--R], rb);
            L >>= 1;
            R >>= 1;
        }
        return f(lb, rb);
    }
    template <class F> int max_right(int L, F ch) {
        if (L == n)
            return n;
        L += sz;
        rrep(i, 1, height + 1) down(L >> i);
        M sum = m1();
        do {
            while (L % 2 == 0)
                L >>= 1;
            if (!ch(f(sum, data[L]))) {
                while (L < sz) {
                    down(L);
                    L <<= 1;
                    if (ch(f(sum, data[L])))
                        sum = f(sum, data[L++]);
                }
                return L - sz;
            }
            sum = f(sum, data[L++]);
        } while ((L & -L) != L);
        return n;
    }
    template <class F> int min_left(int R, F ch) {
        if (R == 0)
            return 0;
        R += sz;
        rrep(i, 1, height + 1) down((R - 1) >> i);
        M sum = m1();
        do {
            R--;
            while (R > 1 and (R & 1))
                R >>= 1;
            if (!ch(f(data[R], sum))) {
                while (R < sz) {
                    down(R);
                    R = (R * 2 + 1);
                    if (ch(f(data[R], sum))) {
                        sum = f(data[R--], sum);
                    }
                }
                return R + 1 - sz;
            }
            sum = f(data[R], sum);
        } while ((R & -R) != R);
        return 0;
    }
};

/**
 * @brief Lazy Segment Tree
 */

pair<int,int> f(pair<int,int> A, pair<int,int> B) {
    return {A.first+B.first,A.second+B.second};
}

pair<int,int> g(pair<int,int> A, pair<int,int> B) {
    if (B.first == -1) return A;
    if (A.second == B.first) return {A.first, B.second};
    return {-1,-1};
}

pair<int,int> h(pair<int,int> A, pair<int,int> B) {
    if (A.first == -1) return B;
    if (B.first == -1) return A;
    if (A.second == B.first) return {A.first, B.second};
    return {-2,-2};
}

pair<int,int> e1() {
    return {0,0};
}

pair<int,int> e2() {
    return {-1,-1};
}

int main() {
    int N, M, Q;
    cin >> N >> M >> Q;
    LazySegmentTree<pair<int,int>,pair<int,int>,f,g,h,e1,e2> Seg(N);
    rep(i,0,N) Seg.set(i,{0,0});
    rep(i,0,Q) {
        int L, R, X;
        cin >> L >> R >> X;
        L--;
        Seg.update(L,R,{X-1,X});
    }
    int ANS = 0;
    rep(i,0,N) {
        pair<int,int> Ret = Seg.query(i,i+1);
        if (Ret.first == 0 && Ret.second == M) ANS++;
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 260
memory_kb: 10804
final_rank: 9

code_snippet: #include <set>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	// step #1. input
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int N, K, T;
	cin >> N >> K >> T;
	vector<int> L(T), R(T), X(T);
	for (int i = 0; i < T; i++) {
		cin >> L[i] >> R[i] >> X[i];
		L[i]--; X[i]--;
	}
	
	// step #2. preparation
	vector<vector<int> > gl(N + 1), gr(N + 1);
	for (int i = 0; i < T; i++) {
		gl[L[i]].push_back(i);
		gr[R[i]].push_back(i);
	}

	// step #3. sweepline
	set<int> s;
	int last = 0, ans = 0;
	for (int i = 0; i < N; i++) {
		for (int j : gr[i]) {
			s.erase(j);
		}
		for (int j : gl[i]) {
			set<int>::iterator it = s.lower_bound(j);
			if (it != s.end()) {
				if (X[j] >= X[*it]) {
					last = max(last, min(R[j], R[*it]));
				}
			}
			if (it != s.begin()) {
				it--;
				if (X[j] <= X[*it]) {
					last = max(last, min(R[j], R[*it]));
				}
			}
			s.insert(j);
		}
		ans += int(last <= i && s.size() == K);
	}

	// step #4. output
	cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 27784
final_rank: 13

code_snippet: #line 2 "SPJ-Library/icpc/template.hpp"
// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif

// ----------------- write from here -------------------

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, l, r) for(int i = int(l); i < int(r); i++)
#define rep(i, n) rng(i, 0, n)
#define sz(v) int(v.size())
#define foa(s, v) for(auto &s : v)
#define all(v) v.begin(), v.end()
template <class T>
using V = vector<T>;
#line 2 "a.cpp"

using S = int;
using F = pair<int, int>;
constexpr S e = -1;
S op(S a, S b) {
	if(a == -1 || b == -1) return a + b - (-1);
	return a;
}
const F id = make_pair(-1, -1);
const F ng = make_pair(-2, -2);
S mapping(F f, S s) {
	if(f == ng) return -1;
	if(f == id) return s;
	if(f.first == s) return f.second;
	return -1;
}
F composition(F g, F f) {
	if(f == ng || g == ng) return ng;
	if(f == id) return g;
	if(g == id) return f;
	return f.second == g.first ? make_pair(f.first, g.second) : ng;
}

#line 3 "SPJ-Library/icpc/lazy_segment_tree.hpp"

/* usage:
	using S = ll;
	using F = ll;
	constexpr S e = -INF;
	S op(S a, S b) { return max(a, b); }
	constexpr F id = 0LL;
	// f(g(x)) == (composition(f, g))(x)
	F composition(F f, F g) { return f + g; }
	S mapping(F f, S s) { return f + s; }
*/

/* require:
	- operator!= for `F` is defined
	- `op` and `composition` are both associative ( i.e. f(a, f(b, c)) == f(f(a, b), c) )
	- mapping(f, op(a, b)) == op(mapping(f, a), mapping(f, b))
*/

struct node {
	node *l = 0, *r = 0;
	ll lo, hi;
	S val = e;
	F mapp = id;

	// usage:
	// 		node* lst = new node(0, n);
	// 		...
	// 		delete lst;
	node(ll lo, ll hi) : lo(lo), hi(hi) {}

	// memory saving (optional)
	~node() {
		if(l) {
			delete l;
			delete r;
		}
	}

	// optional
	// usage:
	// 		node* lst = new node(v, 0LL, ll(sz(v)));
	node(V<S>& v, ll lo, ll hi) : lo(lo), hi(hi) {
		if(lo + 1 < hi) {
			ll mid = lo + (hi - lo) / 2;
			l = new node(v, lo, mid);
			r = new node(v, mid, hi);
			val = op(l->val, r->val);
		} else
			val = v[lo];
	}

	void push() {
		if(!l) {
			ll mid = lo + (hi - lo) / 2;
			l = new node(lo, mid), r = new node(mid, hi);
		}
		if(mapp != id) l->apply(lo, hi, mapp), r->apply(lo, hi, mapp), mapp = id;
	}

	void apply(ll L, ll R, F f) {
		if(R <= lo || hi <= L) return;
		if(L <= lo && hi <= R) {
			mapp = composition(f, mapp);
			val = mapping(f, val);
			return;
		}
		push(), l->apply(L, R, f), r->apply(L, R, f);
		val = op(l->val, r->val);
	}

	S prod(ll L, ll R) {
		if(R <= lo || hi <= L) return e;
		if(L <= lo && hi <= R) return val;
		push();
		return op(l->prod(L, R), r->prod(L, R));
	}

	// optional
	S get(ll idx) { return prod(idx, idx + 1); }
	S all_prod() { return val; }

	void set(ll pos, S s) {
		if(pos < lo || hi <= pos) return;
		if(lo + 1 == hi) {
			mapp = id;
			val = s;
		} else {
			push(), l->set(pos, s), r->set(pos, s);
			val = op(l->val, r->val);
		}
	}
};
#line 26 "a.cpp"

int n;
void solve() {
	int k, t;
	cin >> k >> t;

	node* lst = new node(0, n);

	rep(i, n) lst->set(i, 0);

	rep(j, t) {
		int l, r, x;
		cin >> l >> r >> x;
		l--;
		lst->apply(l, r, make_pair(x - 1, x));
	}

	int ans = 0;
	rep(i, n) if(lst->get(i) == k) ans++;

	cout << ans << '\n';

	delete lst;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	while(cin >> n) solve();
}
accuracy: 1
time_ms: 250
memory_kb: 28560
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=1050000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}

struct seg_tree{
	int _n;
	int size;
	ll _v;
	vector<pair<ll,ll>> seg;
	seg_tree(int n,ll v):_n(n),_v(v){
		size=1;
		while(size<n) size*=2;
		seg.resize(size*2);
		for(int i=0;i<size*2;i++) seg[i]={1,0};
	}
	pair<ll,ll> op(pair<ll,ll> l,pair<ll,ll> r){
		return {(l.first*r.first)%mod,((l.second*r.first)%mod+r.second)%mod};
	}
	void updata(int k){
		seg[k]=op(seg[k*2],seg[k*2+1]);
	}
	pair<ll,ll> query(int l,int r,int a,int b,int k){
		if(r<=a||b<=l) return {1,0};
		if(l<=a&&b<=r) return seg[k];
		return op(query(l,r,a,(a+b)/2,k*2),query(l,r,(a+b)/2,b,k*2+1));
	}
	pair<ll,ll> calc(int l,int r){
		return query(l,r,0,size,1);
	}
	void updata_ren(int ind){
		if(ind==1) return ;
		ind/=2;
		updata(ind);
		updata_ren(ind);
	}
	void set(int ind,int v){
		ind+=size;
		seg[ind]={_v,v};
		updata_ren(ind);
	}
	void reset(int ind){
		ind+=size;
		seg[ind]={1,0};
		updata_ren(ind);
	}
};

void solve();
// oddloop
int main() {
	solve();
}

void solve(){
	int N,K,T;
	cin>>N>>K>>T;
	ll v=167;
	seg_tree seg(T,v);
	int ans=0;
	ll val=0;
	rep(i,K) val=(val*v+(i+1))%mod;
	vector<vector<pair<int,int>>> G(N+1);
	rep(i,T){
		int l,r,x;
		cin>>l>>r>>x;
		G[l-1].push_back({i,x});
		G[r].push_back({i,-1});
	}
	//cout<<val<<"\n";
	rep(i,N+1){
		for(auto x:G[i]){
			if(x.second==-1) seg.reset(x.first);
			else seg.set(x.first,x.second);
		}
		if(seg.seg[1].second==val) ans++;
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 160
memory_kb: 23224
final_rank: 11

code_snippet: #line 2 "library/bits/stdc++.h"

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#line 2 "Source.cpp"
using namespace std;

#define REP(a, b) for (long long a = 0; a < b; ++a)
#define ll long long
#define ld long double
#define ALL(x) begin(x), end(x)

#line 1 "library/atcoder/lazysegtree.hpp"



#line 5 "library/atcoder/lazysegtree.hpp"
#include <cassert>
#line 8 "library/atcoder/lazysegtree.hpp"

#line 1 "library/atcoder/internal_bit.hpp"



#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder


#line 10 "library/atcoder/lazysegtree.hpp"

namespace atcoder {

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    explicit lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder


#line 10 "Source.cpp"

pair<int, int> op(pair<int, int> a, pair<int, int> b)
{
  if (a.first == -1)
    return b;
  return a;
}

pair<int, int> e()
{
  return {-1, -1};
}

pair<int, int> mapping(pair<int, int> f, pair<int, int> x)
{
  if (f.first == -1)
    return x;
  if (x.second == f.first)
  {
    return {x.first, f.second};
  }
  return {-2, -2};
}

pair<int, int> composition(pair<int, int> f, pair<int, int> g)
{
  swap(f, g);
  if (f.first == -1)
    return g;
  if (g.first == -1)
    return f;
  if (f.second == g.first)
    return {f.first, g.second};
  return {-2, -2};
}

void solve()
{
  int n, k;
  cin >> n >> k;
  atcoder::lazy_segtree<pair<int, int>, op, e, pair<int, int>, mapping, composition, e> seg(n);
  REP(i, n)
  {
    seg.set(i, {0, 1});
  }
  int t;
  cin >> t;
  REP(i, t)
  {
    int a, b, c;
    cin >> a >> b >> c;
    seg.apply(a - 1, b, pair<int, int>{c, c + 1});
    /*
    REP(q, n)
    {
      seg.get(q);
    }
    */
  }
  int ans = 0;
  REP(i, n)
  {
    pair<int, int> now = seg.get(i);
    if (seg.get(i).first == 0 and seg.get(i).second == k + 1)
    {
      ans++;
    }
  }
  cout << ans << endl;
}

#undef int
int main()
{
  iostream::sync_with_stdio(false);
  cout << fixed << setprecision(100);
  solve();
}
accuracy: 1
time_ms: 200
memory_kb: 10932
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

constexpr int SQRT = 512;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K, T;
    cin >> N >> K >> T;

    int sz = (N + SQRT - 1) / SQRT;
    vector<int> ok(N, 0), S_block(sz, -2), T_block(sz, -2);

    auto propagate = [&](int k) {
        if (S_block[k] == -2 and T_block[k] == -2) return;
        int L = k * SQRT, R = min(N, (k + 1) * SQRT);
        for (int i = L; i < R; i++) {
            if (S_block[k] == -1)
                ok[i] = -1;
            else
                ok[i] = (ok[i] == S_block[k] ? T_block[k] : -1);
        }
        S_block[k] = T_block[k] = -2;
    };
    auto update = [&](int l, int r, int x) {
        for (int k = 0; k < sz; k++) {
            int L = k * SQRT, R = min(N, (k + 1) * SQRT);
            if (R <= l or r <= L) continue;
            if (l <= L and R <= r) {
                if (S_block[k] == -2 and T_block[k] == -2)
                    S_block[k] = x, T_block[k] = x + 1;
                else if (T_block[k] == x)
                    T_block[k]++;
                else
                    S_block[k] = T_block[k] = -1;
            } else {
                propagate(k);
                for (int i = max(L, l); i < min(R, r); i++) ok[i] = (ok[i] == x ? ok[i] + 1 : -1);
            }
        }
    };

    for (; T--;) {
        int l, r, x;
        cin >> l >> r >> x;
        update(--l, r, --x);
    }

    int ans = 0;
    for (int k = 0; k < sz; k++) propagate(k);
    for (int i = 0; i < N; i++) ans += (ok[i] == K);
    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 3872
final_rank: 10

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {
    for (size_t i = 0; i < N; i++) {
        os << v[i] << (i + 1 == N ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) void(0)
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }
#pragma endregion

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

constexpr int SQRT = 512;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K, T;
    cin >> N >> K >> T;

    int sz = (N + SQRT - 1) / SQRT;
    vector<int> ok(N, 0),  // 次の処理はどこからか
        S_block(sz, -2),   // 各ブロックの処理の始点
        T_block(sz, -2);   // 各ブロックの処理の終点

    auto propagate = [&](int k) {
        if (S_block[k] == -2 and T_block[k] == -2) return;
        int L = k * SQRT, R = min(N, (k + 1) * SQRT);
        for (int i = L; i < R; i++) {
            if (S_block[k] == -1)
                ok[i] = -1;
            else
                ok[i] = (ok[i] == S_block[k] ? T_block[k] : -1);
        }
        S_block[k] = T_block[k] = -2;
    };
    auto update = [&](int l, int r, int x) {
        for (int k = 0; k < sz; k++) {
            int L = k * SQRT, R = min(N, (k + 1) * SQRT);
            if (R <= l or r <= L) continue;
            if (l <= L and R <= r) {
                if (S_block[k] == -2 and T_block[k] == -2)
                    S_block[k] = x, T_block[k] = x + 1;
                else if (T_block[k] == x)
                    T_block[k]++;
                else
                    S_block[k] = T_block[k] = -1;
            } else {
                propagate(k);
                for (int i = max(L, l); i < min(R, r); i++) ok[i] = (ok[i] == x ? ok[i] + 1 : -1);
            }
        }
    };

    for (; T--;) {
        int l, r, x;
        cin >> l >> r >> x;
        update(--l, r, --x);
    }

    int ans = 0;
    for (int k = 0; k < sz; k++) propagate(k);
    for (int i = 0; i < N; i++) ans += (ok[i] == K);
    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 3872
final_rank: 6