考虑每个小区间的的贡献,显然是只用到了覆盖了这个小区间的值里面第 \(k\) 大。

倘若我们已经知道了覆盖当前区间的值都有多少个,我们就可以在线段树上二分找第 \(k\)大。

现在我们并不知道,我们可以用差分+线段树上修改的方法来完成对当前 值的出现次数 的维护。

#include<algorithm>
#include<iostream>
#include<cstdio>
#define lson (k<<1)
#define rson ((k<<1)|1)
using namespace std;
int n, m, k, tot, r, a, b, c;
long long ans;
const int N = 100010;
int s[N << 2];
struct node
{
	int x, y, k;
} p[N << 2];
int my(node a, node b) {return a.x < b.x;}
void pushup(int k) {s[k] = s[lson] + s[rson];}
void change(int k, int l, int r, int pos, int val)
{
	if (l == r)
	{
		s[k] += val;
		return;
	}
	int mid = (l + r) >> 1;
	if (pos <= mid)change(lson, l, mid, pos, val);
	else change(rson, mid + 1, r, pos, val);
	pushup(k);
}
int ask(int k, int l, int r, int v)
{
	if (l == r)return l;
	int mid = (l + r) >> 1;
	return v <= s[rson] ? ask(rson, mid + 1, r, v) : ask(lson, l, mid, v - s[rson]);
}
signed main()
{
	cin >> n >> m >> k;
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d%d%d", &c, &a, &b); a += m; b += m; r = max(r, c);
		if (a <= b)p[++tot] = (node) {a, c, 1}, p[++tot] = (node) {b, c, -1};
		else p[++tot] = (node) {a, c, 1}, p[++tot] = (node) {2 * m, c, -1}, p[++tot] = (node) {0, c, 1}, p[++tot] = (node) {b, c, -1};
	}
	sort(p + 1, p + 1 + tot, my);
	for (int i = 1; i <= tot; ++i)
	{
		a = ask(1, 0, r, k); ans += (long long)a * a * (p[i].x - p[i - 1].x);
		change(1, 0, r, p[i].y, p[i].k);
	}
	cout << ans;
	return 0;
}