/* * Author: ttq012 * Date: 05/20/2025 */ #include #define int long long using namespace std; const int N = 500010; const int mod = 1e9 + 7; vector v[N]; int x[N], a[N], b[N]; struct BIT { int tree[N]; BIT() { memset(tree, 0, sizeof tree); } void add(int x, int v) { ++x; for (; x < N; x += (x &- x)) tree[x] += v; } int query(int x) { ++x; int s = 0; for (; x; x -= (x &- x)) s += tree[x]; return s; } } fwt, fwt2; signed main() { cin.tie(0)->sync_with_stdio(false); int n, m, res = 1e18; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> x[i] >> a[i] >> b[i]; for (int i = 1; i <= n; ++i) v[a[i]].emplace_back(i); int cnt = 0; for (int i = 1; i <= n; ++i) if (x[i] == 1) fwt.add(b[i], 1); else if (x[i] == 2) fwt2.add(b[i], 1); else if (x[i] == 3) ++cnt; for (int i = 100001; ~i; --i) { for (int &j : v[i]) if (x[j] == 1) fwt.add(b[j], -1), fwt2.add(b[j], 1); else if (x[j] == 2) fwt2.add(b[j], -1), ++cnt; else if (!x[j]) fwt.add(b[j], 1); int l = 0, r = 100001, best = -1; while (l <= r) { int mid = l + r >> 1; int oo = cnt + fwt.query(N - 1) + fwt2.query(N - 1) - fwt.query(mid - 1); if (oo >= m) l = mid + 1, best = mid; else r = mid - 1; } if (~best) res = min(res, cnt + fwt2.query(N - 1) - fwt2.query(best - 1)); } cout << res << '\n'; return 0; }