C:多彩的线段
作者:
隗恬
,
2024-05-30 14:33:42
,
所有人可见
,
阅读 6
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 500010, P = 998244353;
int n, m;
PII d[N];
int tr[N << 1];
vector<int> g;
bool cmp(PII a, PII b)
{
return a.y < b.y;
}
int lowbit(int x)
{
return x & -x;
}
void update(int x, int c)
{
for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
int query(int x)
{
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int find(int x)
{
return lower_bound(g.begin(), g.end(), x) - g.begin() + 1;
}
int main()
{
int T;
cin >> T;
while (T -- )
{
g.clear();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
{
scanf("%d%d", &d[i].x, &d[i].y);
g.push_back(d[i].x), g.push_back(d[i].y);
}
sort(d + 1, d + n + 1, cmp);
sort(g.begin(), g.end());
g.erase(unique(g.begin(), g.end()), g.end());
LL res = 1;
for (int i = n; i; i -- )
{
d[i].x = find(d[i].x), d[i].y = find(d[i].y);
res = res * max(0, m - query(d[i].y)) % P;
update(d[i].x, 1);
}
for (int i = 1; i <= n; i ++ ) update(d[i].x, -1);
printf("%lld\n", res);
}
return 0;
}
/*
2
4 3
4 7
3 4
5 8
1 3
2 1000
100 200
300 400
*/