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
| #include <iostream> #include <cstdio> #include <algorithm> #define maxn 1000010 #define maxm 25010 #define cn const node using namespace std;
int n, m;
struct node { int l, r, w, id;
friend bool operator < (cn &u, cn &v) { return u.w > v.w; } } a[maxm];
int fa[maxn]; void init_fa() { for (int i = 1; i <= n + 1; ++i) fa[i] = i; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void merge(int l, int r) { while (l < r) l = fa[l] = find(l + 1); }
bool check(int x) { init_fa(); for (int i = 1; i <= m; ++i) { if (a[i].id > x) continue; int L = a[i].l, R = a[i].r, l = a[i].l, r = a[i].r, w = a[i].w; while (a[i + 1].w == w) { ++i; if (a[i].id > x) continue; L = max(L, a[i].l); l = min(l, a[i].l); R = min(R, a[i].r); r = max(r, a[i].r); } if (L > R || find(L) >= R + 1) return 1; merge(find(l), r + 1); } return 0; }
int main() { freopen("1.in", "r", stdin); cin >> n >> m; for (int i = 1; i <= m; ++i) scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].w), a[i].id = i; sort(a + 1, a + m + 1); int l = 1, r = m, mid, ans = 0; while (l <= r) { mid = l + r >> 1; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } cout << ans << endl; return 0; }
|