#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
const int mod = 1e9 + 7;
int N, K;
int a[1005], b[1005], st[1005], dr[1005];
int dp[1005][4005], sum[1005][4005]; /// primele i, ultimul este in intervalul j
int fct[100005], ifct[100005], inv[100005];
int cmb[100005];
int cc[4005][1005];
int cntall[4005];
class Normalize
{
public:
map<int, int> mp;
vector<int> ord;
void add(int x)
{
if(mp.count(x)) return;
ord.push_back(x);
mp[x];
}
void normalize()
{
sort(ord.begin(), ord.end());
for(int i = 0; i < ord.size(); i++)
mp[ ord[i] ] = i;
}
int get(int x) { return mp[x]; }
}nrm;
int power(int x, int y)
{
if(y <= 0) return 1;
int ans = power( (1LL * x * x) % mod, y >> 1 );
if(y & 1) ans = (1LL * x * ans) % mod;
return ans;
}
void pre(int K)
{
fct[0] = 1;
for(int i = 1; i <= K; i++) fct[i] = (1LL * fct[i - 1] * i) % mod;
ifct[K] = power(fct[K], mod - 2);
for(int i = K - 1; i >= 0; i--) ifct[i] = (1LL * ifct[i + 1] * (i + 1)) % mod;
for(int i = 1; i <= K; i++) inv[i] = power(i, mod - 2);
}
int C(int N, int K)
{
if(K > N) return 0;
int ans = (1LL * fct[N] * ifct[K]) % mod;
ans = (1LL * ans * ifct[N - K]) % mod;
return ans;
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
pre(1e4);
scanf("%d", &N);
for(int i = 1; i <= N; i++)
{
scanf("%d%d", &a[i], &b[i]);
nrm.add(a[i]);
nrm.add(b[i]);
}
nrm.normalize();
for(int i = 1; i <= N; i++)
{
st[i] = nrm.get(a[i]);
dr[i] = nrm.get(b[i]);
for(int j = 2 * st[i] + 2; j < 2 * dr[i] + 2; j += 2) cntall[j]++;
}
K = nrm.ord.size();
for(int i = 2; i <= 2 * K - 1; i += 2)
{
int lft = nrm.ord[i / 2 - 1];
int rgt = nrm.ord[i / 2];
int sz = rgt - lft - 1;
cmb[0] = 1;
for(int k = 1; k <= cntall[i]; k++)
{
cmb[k] = (1LL * cmb[k - 1] * (sz - k + 1)) % mod;
cmb[k] = (1LL * cmb[k] * inv[k]) % mod;
}
for(int j = 1; j <= cntall[i]; j++)
for(int k = 0; k < j; k++)
cc[i][j] = (cc[i][j] + (1LL * cmb[k + 1] * C(j - 1, k)) % mod) % mod;
}
for(int i = 0; i <= 2 * K - 1; i++) dp[0][i] = sum[0][i] = 1;
int ans = 0;
for(int i = 1; i <= N; i++)
{
sum[i][0] = sum[i - 1][0];
for(int j = 1; j <= 2 * K - 1; j++)
{
dp[i][j] = dp[i][j - 1];
if(j & 1)
{
int x = nrm.ord[j / 2];
if(a[i] <= x && x <= b[i])
dp[i][j] = (dp[i][j] + sum[i - 1][j - 1]) % mod;
}
else
{
int lft = nrm.ord[j / 2 - 1];
int rgt = nrm.ord[j / 2];
int sz = rgt - lft - 1;
if(a[i] <= lft && rgt <= b[i])
{
cmb[0] = 1;
for(int k = 1; k <= i; k++)
{
cmb[k] = (1LL * cmb[k - 1] * (sz - k + 1)) % mod;
cmb[k] = (1LL * cmb[k] * inv[k]) % mod;
}
int cnt = 0;
for(int k = i; k > 0; k--)
{
if(a[k] <= lft && rgt <= b[k])
cnt++;
int ans = cc[j][cnt];
dp[i][j] += (1LL * dp[k - 1][j - 1] * ans) % mod;
dp[i][j] %= mod;
}
}
}
sum[i][j] = (sum[i - 1][j] + dp[i][j]) % mod;
}
ans = (ans + dp[i][2 * K - 1]) % mod;
}
printf("%d\n", ans);
return 0;
}
Compilation message
boat.cpp: In member function 'void Normalize::normalize()':
boat.cpp:33:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ord.size(); i++)
^
boat.cpp: In function 'int main()':
boat.cpp:75:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
^
boat.cpp:78:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i], &b[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
8440 KB |
Output is correct |
2 |
Correct |
13 ms |
8440 KB |
Output is correct |
3 |
Correct |
12 ms |
8488 KB |
Output is correct |
4 |
Correct |
13 ms |
8652 KB |
Output is correct |
5 |
Correct |
13 ms |
8652 KB |
Output is correct |
6 |
Correct |
13 ms |
8652 KB |
Output is correct |
7 |
Correct |
13 ms |
8652 KB |
Output is correct |
8 |
Correct |
13 ms |
8652 KB |
Output is correct |
9 |
Correct |
13 ms |
8652 KB |
Output is correct |
10 |
Correct |
13 ms |
8652 KB |
Output is correct |
11 |
Correct |
12 ms |
8652 KB |
Output is correct |
12 |
Correct |
13 ms |
8652 KB |
Output is correct |
13 |
Correct |
13 ms |
8652 KB |
Output is correct |
14 |
Correct |
14 ms |
8660 KB |
Output is correct |
15 |
Correct |
13 ms |
8684 KB |
Output is correct |
16 |
Correct |
9 ms |
8684 KB |
Output is correct |
17 |
Correct |
9 ms |
8684 KB |
Output is correct |
18 |
Correct |
8 ms |
8684 KB |
Output is correct |
19 |
Correct |
9 ms |
8684 KB |
Output is correct |
20 |
Correct |
9 ms |
8684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
8440 KB |
Output is correct |
2 |
Correct |
13 ms |
8440 KB |
Output is correct |
3 |
Correct |
12 ms |
8488 KB |
Output is correct |
4 |
Correct |
13 ms |
8652 KB |
Output is correct |
5 |
Correct |
13 ms |
8652 KB |
Output is correct |
6 |
Correct |
13 ms |
8652 KB |
Output is correct |
7 |
Correct |
13 ms |
8652 KB |
Output is correct |
8 |
Correct |
13 ms |
8652 KB |
Output is correct |
9 |
Correct |
13 ms |
8652 KB |
Output is correct |
10 |
Correct |
13 ms |
8652 KB |
Output is correct |
11 |
Correct |
12 ms |
8652 KB |
Output is correct |
12 |
Correct |
13 ms |
8652 KB |
Output is correct |
13 |
Correct |
13 ms |
8652 KB |
Output is correct |
14 |
Correct |
14 ms |
8660 KB |
Output is correct |
15 |
Correct |
13 ms |
8684 KB |
Output is correct |
16 |
Correct |
9 ms |
8684 KB |
Output is correct |
17 |
Correct |
9 ms |
8684 KB |
Output is correct |
18 |
Correct |
8 ms |
8684 KB |
Output is correct |
19 |
Correct |
9 ms |
8684 KB |
Output is correct |
20 |
Correct |
9 ms |
8684 KB |
Output is correct |
21 |
Correct |
1129 ms |
16344 KB |
Output is correct |
22 |
Correct |
1212 ms |
16548 KB |
Output is correct |
23 |
Correct |
1008 ms |
16548 KB |
Output is correct |
24 |
Correct |
1101 ms |
16548 KB |
Output is correct |
25 |
Correct |
1060 ms |
16548 KB |
Output is correct |
26 |
Correct |
1617 ms |
16548 KB |
Output is correct |
27 |
Correct |
1698 ms |
16548 KB |
Output is correct |
28 |
Correct |
1625 ms |
16548 KB |
Output is correct |
29 |
Correct |
1634 ms |
16548 KB |
Output is correct |
30 |
Correct |
34 ms |
16548 KB |
Output is correct |
31 |
Correct |
34 ms |
16548 KB |
Output is correct |
32 |
Correct |
30 ms |
16548 KB |
Output is correct |
33 |
Correct |
31 ms |
16548 KB |
Output is correct |
34 |
Correct |
28 ms |
16548 KB |
Output is correct |
35 |
Correct |
18 ms |
16548 KB |
Output is correct |
36 |
Correct |
18 ms |
16548 KB |
Output is correct |
37 |
Correct |
20 ms |
16548 KB |
Output is correct |
38 |
Correct |
21 ms |
16548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16548 KB |
Output is correct |
2 |
Correct |
14 ms |
16548 KB |
Output is correct |
3 |
Correct |
14 ms |
16548 KB |
Output is correct |
4 |
Correct |
15 ms |
16548 KB |
Output is correct |
5 |
Correct |
15 ms |
16548 KB |
Output is correct |
6 |
Correct |
20 ms |
16548 KB |
Output is correct |
7 |
Correct |
20 ms |
16548 KB |
Output is correct |
8 |
Correct |
20 ms |
16548 KB |
Output is correct |
9 |
Correct |
20 ms |
16548 KB |
Output is correct |
10 |
Correct |
20 ms |
16548 KB |
Output is correct |
11 |
Correct |
14 ms |
16548 KB |
Output is correct |
12 |
Correct |
12 ms |
16548 KB |
Output is correct |
13 |
Correct |
13 ms |
16548 KB |
Output is correct |
14 |
Correct |
13 ms |
16548 KB |
Output is correct |
15 |
Correct |
14 ms |
16548 KB |
Output is correct |
16 |
Correct |
10 ms |
16548 KB |
Output is correct |
17 |
Correct |
9 ms |
16548 KB |
Output is correct |
18 |
Correct |
9 ms |
16548 KB |
Output is correct |
19 |
Correct |
9 ms |
16548 KB |
Output is correct |
20 |
Correct |
12 ms |
16548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
8440 KB |
Output is correct |
2 |
Correct |
13 ms |
8440 KB |
Output is correct |
3 |
Correct |
12 ms |
8488 KB |
Output is correct |
4 |
Correct |
13 ms |
8652 KB |
Output is correct |
5 |
Correct |
13 ms |
8652 KB |
Output is correct |
6 |
Correct |
13 ms |
8652 KB |
Output is correct |
7 |
Correct |
13 ms |
8652 KB |
Output is correct |
8 |
Correct |
13 ms |
8652 KB |
Output is correct |
9 |
Correct |
13 ms |
8652 KB |
Output is correct |
10 |
Correct |
13 ms |
8652 KB |
Output is correct |
11 |
Correct |
12 ms |
8652 KB |
Output is correct |
12 |
Correct |
13 ms |
8652 KB |
Output is correct |
13 |
Correct |
13 ms |
8652 KB |
Output is correct |
14 |
Correct |
14 ms |
8660 KB |
Output is correct |
15 |
Correct |
13 ms |
8684 KB |
Output is correct |
16 |
Correct |
9 ms |
8684 KB |
Output is correct |
17 |
Correct |
9 ms |
8684 KB |
Output is correct |
18 |
Correct |
8 ms |
8684 KB |
Output is correct |
19 |
Correct |
9 ms |
8684 KB |
Output is correct |
20 |
Correct |
9 ms |
8684 KB |
Output is correct |
21 |
Correct |
1129 ms |
16344 KB |
Output is correct |
22 |
Correct |
1212 ms |
16548 KB |
Output is correct |
23 |
Correct |
1008 ms |
16548 KB |
Output is correct |
24 |
Correct |
1101 ms |
16548 KB |
Output is correct |
25 |
Correct |
1060 ms |
16548 KB |
Output is correct |
26 |
Correct |
1617 ms |
16548 KB |
Output is correct |
27 |
Correct |
1698 ms |
16548 KB |
Output is correct |
28 |
Correct |
1625 ms |
16548 KB |
Output is correct |
29 |
Correct |
1634 ms |
16548 KB |
Output is correct |
30 |
Correct |
34 ms |
16548 KB |
Output is correct |
31 |
Correct |
34 ms |
16548 KB |
Output is correct |
32 |
Correct |
30 ms |
16548 KB |
Output is correct |
33 |
Correct |
31 ms |
16548 KB |
Output is correct |
34 |
Correct |
28 ms |
16548 KB |
Output is correct |
35 |
Correct |
18 ms |
16548 KB |
Output is correct |
36 |
Correct |
18 ms |
16548 KB |
Output is correct |
37 |
Correct |
20 ms |
16548 KB |
Output is correct |
38 |
Correct |
21 ms |
16548 KB |
Output is correct |
39 |
Correct |
15 ms |
16548 KB |
Output is correct |
40 |
Correct |
14 ms |
16548 KB |
Output is correct |
41 |
Correct |
14 ms |
16548 KB |
Output is correct |
42 |
Correct |
15 ms |
16548 KB |
Output is correct |
43 |
Correct |
15 ms |
16548 KB |
Output is correct |
44 |
Correct |
20 ms |
16548 KB |
Output is correct |
45 |
Correct |
20 ms |
16548 KB |
Output is correct |
46 |
Correct |
20 ms |
16548 KB |
Output is correct |
47 |
Correct |
20 ms |
16548 KB |
Output is correct |
48 |
Correct |
20 ms |
16548 KB |
Output is correct |
49 |
Correct |
14 ms |
16548 KB |
Output is correct |
50 |
Correct |
12 ms |
16548 KB |
Output is correct |
51 |
Correct |
13 ms |
16548 KB |
Output is correct |
52 |
Correct |
13 ms |
16548 KB |
Output is correct |
53 |
Correct |
14 ms |
16548 KB |
Output is correct |
54 |
Correct |
10 ms |
16548 KB |
Output is correct |
55 |
Correct |
9 ms |
16548 KB |
Output is correct |
56 |
Correct |
9 ms |
16548 KB |
Output is correct |
57 |
Correct |
9 ms |
16548 KB |
Output is correct |
58 |
Correct |
12 ms |
16548 KB |
Output is correct |
59 |
Correct |
1184 ms |
17920 KB |
Output is correct |
60 |
Correct |
1149 ms |
17920 KB |
Output is correct |
61 |
Correct |
1103 ms |
17920 KB |
Output is correct |
62 |
Correct |
1184 ms |
17920 KB |
Output is correct |
63 |
Correct |
1147 ms |
17920 KB |
Output is correct |
64 |
Correct |
1843 ms |
18204 KB |
Output is correct |
65 |
Correct |
1862 ms |
18356 KB |
Output is correct |
66 |
Correct |
1939 ms |
18356 KB |
Output is correct |
67 |
Correct |
1909 ms |
18356 KB |
Output is correct |
68 |
Correct |
1950 ms |
18356 KB |
Output is correct |
69 |
Correct |
942 ms |
18356 KB |
Output is correct |
70 |
Correct |
917 ms |
18356 KB |
Output is correct |
71 |
Correct |
896 ms |
18356 KB |
Output is correct |
72 |
Correct |
936 ms |
18356 KB |
Output is correct |
73 |
Correct |
964 ms |
18356 KB |
Output is correct |
74 |
Correct |
120 ms |
18356 KB |
Output is correct |
75 |
Correct |
122 ms |
18356 KB |
Output is correct |
76 |
Correct |
122 ms |
18356 KB |
Output is correct |
77 |
Correct |
122 ms |
18356 KB |
Output is correct |
78 |
Correct |
126 ms |
18356 KB |
Output is correct |