CF 954F Runner's Problem

题目描述

https://codeforces.com/problemset/problem/954/F

Solution

注意到如果某一行被挡了,那么这一行的转移矩阵就是 $0$

我们直接离散化然后分段矩阵乘法即可,注意离散化的时候要将右端点加 $1$ 也离散化

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define maxn 10010
using namespace std;

const int p = 1000000007;

int n; ll m;

struct Matrix {
#define n 3
ll a[n][n];

void clear() { fill(a[0], a[0] + 3 * 3, 0); }

Matrix() { clear(); }

void setone() { for (int i = 0; i < n; ++i) a[i][i] = 1; }

friend Matrix operator * (const Matrix &u, const Matrix &v) {
Matrix w;
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
w.a[i][j] = (w.a[i][j] + (ll) u.a[i][k] * v.a[k][j]) % p;
return w;
}
#undef n
};

Matrix pow(Matrix x, ll n) {
Matrix s; s.setone();
for (; n; n >>= 1) {
if (n & 1) s = s * x;
x = x * x;
}
return s;
}

struct node {
ll k, x, y;
} a[maxn];

ll b[maxn * 3], cnt;
void init_hash() {
int c1 = 0;
for (int i = 1; i <= n; ++i) b[++c1] = a[i].x, b[++c1] = a[i].y, b[++c1] = a[i].y + 1;
b[++c1] = 2; b[++c1] = m;
sort(b + 1, b + c1 + 1); cnt = unique(b + 1, b + c1 + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b;
a[i].y = lower_bound(b + 1, b + cnt + 1, a[i].y) - b;
}
}

int d[3][maxn * 3];

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i].k >> a[i].x >> a[i].y, --a[i].k;
init_hash();
for (int i = 1; i <= n; ++i) ++d[a[i].k][a[i].x], --d[a[i].k][a[i].y + 1];
for (int i = 0; i < 3; ++i)
for (int j = 1; j <= cnt; ++j) d[i][j] += d[i][j - 1];
Matrix ans; ans.a[1][0] = 1;
for (int i = 1; i < cnt; ++i) {
Matrix x;
x.a[0][0] = x.a[0][1] = 1;
x.a[1][0] = x.a[1][1] = x.a[1][2] = 1;
x.a[2][1] = x.a[2][2] = 1;
for (int k = 0; k < 3; ++k)
if (d[k][i]) for (int j = 0; j < 3; ++j) x.a[k][j] = 0;
ans = pow(x, b[i + 1] - b[i]) * ans;
}
cout << ((ll) ans.a[0][0] + ans.a[1][0] + ans.a[2][0]) % p << "\n";
return 0;
}