#define _GLIBCXX_DEBUG_PEDANTIC
#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
struct Seg {
int l;
int r;
Seg(int _l = -1, int _r = -1) : l(_l), r(_r) {}
bool empty() const {
return r - l + 1 <= 0;
}
bool operator<(const Seg& other) const {
return tie(l, r) < tie(other.l, other.r);
}
bool operator==(const Seg& other) const {
return tie(l, r) == tie(other.l, other.r);
}
friend Seg intersect(const Seg& lhs, const Seg& rhs) {
return {max(lhs.l, rhs.l), min(lhs.r, rhs.r)};
}
};
const int MOD = 1e9 + 7;
const int INV2 = 500000004;
int add(int a, int b) {
int res = a + b;
if (res >= MOD) res -= MOD;
return res;
}
int mul(long long a, int b) {
return a * b % MOD;
}
const int MAX_N = 500 + 1;
Seg segs[MAX_N];
int dp[MAX_N][MAX_N * MAX_N];
int main_idx[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> segs[i].l >> segs[i].r;
}
vector<Seg> states;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (segs[i].l <= segs[j].r) {
states.emplace_back(segs[i].l, segs[j].r);
}
if (segs[i].l <= segs[j].l - 1) {
states.emplace_back(segs[i].l, segs[j].l - 1);
}
}
}
sort(states.begin(), states.end());
states.resize(unique(states.begin(), states.end()) - states.begin());
int m = states.size();
segs[0] = {-1, -1};
dp[0][0] = 1;
int ans = 0;
for (int i = 1; i <= n; ++i) {
main_idx[i] = lower_bound(states.begin(), states.end(), segs[i]) - states.begin();
for (int j = 0; j < m; ++j) {
auto [l1, r1] = states[j];
if (segs[i].l > l1 || r1 > segs[i].r) continue;
for (int k = 0; k < i; ++k) {
auto [l2, r2] = segs[k];
if (r1 < l2) {
continue;
}
if (r2 < l1) {
dp[i][j] = add(dp[i][j], mul(dp[k][main_idx[k]], r1 - l1 + 1));
continue;
}
auto [l3, r3] = intersect(states[j], segs[k]);
dp[i][j] = add(dp[i][j], mul(dp[k][main_idx[k]], r1 - r3));
int idx1 = lower_bound(states.begin(), states.end(), Seg{l3, r3}) - states.begin();
dp[i][j] = add(dp[i][j], mul(dp[k][idx1], mul(r3 - l3, INV2)));
if (l3 - 1 >= l2) {
int idx2 = lower_bound(states.begin(), states.end(), Seg{l2, l3 - 1}) - states.begin();
dp[i][j] = add(dp[i][j], mul(r3 - l1 + 1, dp[k][idx2]));
}
}
}
ans = add(ans, dp[i][main_idx[i]]);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
5308 KB |
Output is correct |
2 |
Correct |
118 ms |
5376 KB |
Output is correct |
3 |
Correct |
115 ms |
5364 KB |
Output is correct |
4 |
Correct |
114 ms |
5316 KB |
Output is correct |
5 |
Correct |
125 ms |
5252 KB |
Output is correct |
6 |
Correct |
113 ms |
5352 KB |
Output is correct |
7 |
Correct |
111 ms |
5260 KB |
Output is correct |
8 |
Correct |
112 ms |
5368 KB |
Output is correct |
9 |
Correct |
118 ms |
5352 KB |
Output is correct |
10 |
Correct |
115 ms |
5288 KB |
Output is correct |
11 |
Correct |
119 ms |
5480 KB |
Output is correct |
12 |
Correct |
111 ms |
5376 KB |
Output is correct |
13 |
Correct |
115 ms |
5320 KB |
Output is correct |
14 |
Correct |
114 ms |
5268 KB |
Output is correct |
15 |
Correct |
112 ms |
5292 KB |
Output is correct |
16 |
Correct |
23 ms |
5384 KB |
Output is correct |
17 |
Correct |
22 ms |
5392 KB |
Output is correct |
18 |
Correct |
22 ms |
5400 KB |
Output is correct |
19 |
Correct |
21 ms |
5396 KB |
Output is correct |
20 |
Correct |
22 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
5308 KB |
Output is correct |
2 |
Correct |
118 ms |
5376 KB |
Output is correct |
3 |
Correct |
115 ms |
5364 KB |
Output is correct |
4 |
Correct |
114 ms |
5316 KB |
Output is correct |
5 |
Correct |
125 ms |
5252 KB |
Output is correct |
6 |
Correct |
113 ms |
5352 KB |
Output is correct |
7 |
Correct |
111 ms |
5260 KB |
Output is correct |
8 |
Correct |
112 ms |
5368 KB |
Output is correct |
9 |
Correct |
118 ms |
5352 KB |
Output is correct |
10 |
Correct |
115 ms |
5288 KB |
Output is correct |
11 |
Correct |
119 ms |
5480 KB |
Output is correct |
12 |
Correct |
111 ms |
5376 KB |
Output is correct |
13 |
Correct |
115 ms |
5320 KB |
Output is correct |
14 |
Correct |
114 ms |
5268 KB |
Output is correct |
15 |
Correct |
112 ms |
5292 KB |
Output is correct |
16 |
Correct |
23 ms |
5384 KB |
Output is correct |
17 |
Correct |
22 ms |
5392 KB |
Output is correct |
18 |
Correct |
22 ms |
5400 KB |
Output is correct |
19 |
Correct |
21 ms |
5396 KB |
Output is correct |
20 |
Correct |
22 ms |
5324 KB |
Output is correct |
21 |
Execution timed out |
2072 ms |
18796 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
429 ms |
2744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
5308 KB |
Output is correct |
2 |
Correct |
118 ms |
5376 KB |
Output is correct |
3 |
Correct |
115 ms |
5364 KB |
Output is correct |
4 |
Correct |
114 ms |
5316 KB |
Output is correct |
5 |
Correct |
125 ms |
5252 KB |
Output is correct |
6 |
Correct |
113 ms |
5352 KB |
Output is correct |
7 |
Correct |
111 ms |
5260 KB |
Output is correct |
8 |
Correct |
112 ms |
5368 KB |
Output is correct |
9 |
Correct |
118 ms |
5352 KB |
Output is correct |
10 |
Correct |
115 ms |
5288 KB |
Output is correct |
11 |
Correct |
119 ms |
5480 KB |
Output is correct |
12 |
Correct |
111 ms |
5376 KB |
Output is correct |
13 |
Correct |
115 ms |
5320 KB |
Output is correct |
14 |
Correct |
114 ms |
5268 KB |
Output is correct |
15 |
Correct |
112 ms |
5292 KB |
Output is correct |
16 |
Correct |
23 ms |
5384 KB |
Output is correct |
17 |
Correct |
22 ms |
5392 KB |
Output is correct |
18 |
Correct |
22 ms |
5400 KB |
Output is correct |
19 |
Correct |
21 ms |
5396 KB |
Output is correct |
20 |
Correct |
22 ms |
5324 KB |
Output is correct |
21 |
Execution timed out |
2072 ms |
18796 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |