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
#include <bits/stdc++.h>
#define lld int
#define ff first
#define ss second
#define mp make_pair
#define pii pair<lld,lld>
#define pb push_back
using namespace std;
int N,M;
int wyn;
vector < pair < int, pii > > tab;
int l, r, k;
int kol[4];
int pop;
int main()
{
	scanf("%d%d",&N,&M);
	for(int i = 0;i < M ;++i)
	{
		scanf("%d%d%d",&l,&r,&k);
		tab.pb(mp(l,mp(0,k-1)));
		tab.pb(mp(r+1,mp(1,k-1)));
	}
	sort(tab.begin(),tab.end());
	for(int i =0;i<tab.size();++i)
	{
		if(kol[0] > 0 && kol[1] > 0 && kol[2] == 0) wyn+=tab[i].ff-pop;
		if(tab[i].ss.ff==0)++kol[tab[i].ss.ss];
		else --kol[tab[i].ss.ss];
		pop=tab[i].ff;
	}
	printf("%d",wyn);
}