1 #include 2 #include 3 #include 4 #define N 1000011 5 #define min(x, y) ((x) < (y) ? (x) : (y)) 6 #define max(x, y) ((x) > (y) ? (x) : (y)) 7 using namespace std; 8 int n, q, ans; 9 int f[N];10 11 struct node12 {13 int x, y, z;14 }p[N], t[N];15 16 inline int read()17 {18 int x = 0, f = 1;19 char ch = getchar();20 for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;21 for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';22 return x * f;23 }24 25 inline bool cmp(node x, node y)26 {27 return x.z > y.z;28 }29 30 inline int find(int x)31 {32 return x == f[x] ? x : f[x] = find(f[x]);33 }34 35 inline bool check(int k)36 {37 int i, j, x, y, lmin, lmax, rmin, rmax;38 for(i = 1; i <= n + 1; i++) f[i] = i;39 for(i = 1; i <= k; i++) t[i] = p[i];40 std::sort(t + 1, t + k + 1, cmp);41 lmin = lmax = t[1].x;42 rmin = rmax = t[1].y;43 for(i = 2; i <= k; i++)44 {45 if(t[i].z < t[i - 1].z)46 {47 if(find(lmax) > rmin) return 1;48 for(j = find(lmin); j <= rmax; j++)49 f[find(j)] = find(rmax + 1);50 lmin = lmax = t[i].x;51 rmin = rmax = t[i].y;52 }53 else54 {55 lmin = min(lmin, t[i].x);56 lmax = max(lmax, t[i].x);57 rmin = min(rmin, t[i].y);58 rmax = max(rmax, t[i].y);59 if(lmax > rmin) return 1;60 }61 }62 // cout< < rmin) return 1;64 return 0;65 }66 67 int main()68 {69 freopen("number.in","r",stdin);70 freopen("number.out","w",stdout);71 int i, x, y, mid;72 n = read();73 q = read();74 for(i = 1; i <= q; i++)75 p[i].x = read(), p[i].y = read(), p[i].z = read();76 x = 1, y = q;77 //cout< < > 1;83 if(check(mid)) ans = mid, y = mid - 1;84 else x = mid + 1;85 }86 printf("%d\n", ans);87 return 0;88 }