#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 1005;
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,tune=native")
#pragma GCC optimize("O3,unroll-loops")
int n;
vector<int> points;
int L[MAXN],R[MAXN];
int dp[MAXN][MAXN]; // dp(i,c)=number of ways to reach the ith segment with c objects inside the ith segment
int S[MAXN];
int sz[MAXN];
int M[MAXN];
int qexp(int a, int b, int mod) {
a%=mod;
int res = 1;
while(b) {
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
struct hash_pair {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(pair<uint64_t,uint64_t> x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1);
}
};
unordered_map<pi,int,hash_pair>memo;
int choose(int n, int k) {
auto it=memo.find({n,k});
if(it!=memo.end())return it->s;
exit(0);
int x = 1, y = 1;
FOR(i,0,k-1) {
y*=n-i;
x*=k-i;
y%=MOD;
x%=MOD;
}
return memo[{n,k}]=(y*qexp(x,MOD-2,MOD)) % MOD;
}
int arr[MAXN];
void precomp(int n,int mx) {
int x=1,y=1;
FOR(i,1,mx) {
y*=n-i+1;
x*=i;
y%=MOD;
x%=MOD;
memo[{n,i}]=(y*qexp(x,MOD-2,MOD)) % MOD;
}
}
int32_t main()
{
IAMSPEED
cin >> n;
FOR(i,1,n){
cin >> L[i] >> R[i];
points.pb(L[i]);
points.pb(R[i]+1);
}
points.pb(-inf);
sort(all(points));
points.resize(unique(all(points))-points.begin());
// my ith segment is points[i] to points[i+1]
int m=points.size();
FOR(i,0,m-2)sz[i]=points[i+1]-points[i];
FOR(i,1,m-2) {
precomp(sz[i],min(sz[i],n));
}
FOR(i,0,m-1)S[i]=1;
FOR(i,1,n) {
int ls=lower_bound(all(points),L[i])-points.begin();
int rs=lower_bound(all(points),R[i]+1)-points.begin();
DEC(j,rs-1,ls){
DEC(k,min({M[j]+1,i,sz[j]}),1){
if(k>1)dp[j][k]=(dp[j][k]+dp[j][k-1])%MOD;
else if(j)dp[j][k]=(dp[j][k]+S[j-1])%MOD;
if(dp[j][k])chmax(M[j],k);
}
}
memset(S,0,sizeof S);
DEC(j,rs-1,ls){
int val = 0;
if(j==0){
val=1;
}
FOR(k,1,min({M[j],i,sz[j]})){
val=(val+dp[j][k]*choose(sz[j],k)%MOD)%MOD;
}
arr[j]=val;
}
FOR(j,0,m-1){
if(j)S[j]=(S[j-1]+arr[j])%MOD;
else S[j]=1;
}
}
cout<<(S[m-1]-1+MOD)%MOD;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
16764 KB |
Output is correct |
2 |
Correct |
116 ms |
16744 KB |
Output is correct |
3 |
Correct |
113 ms |
16748 KB |
Output is correct |
4 |
Correct |
117 ms |
16860 KB |
Output is correct |
5 |
Correct |
116 ms |
16796 KB |
Output is correct |
6 |
Correct |
142 ms |
16752 KB |
Output is correct |
7 |
Correct |
150 ms |
16808 KB |
Output is correct |
8 |
Correct |
141 ms |
16792 KB |
Output is correct |
9 |
Correct |
140 ms |
16860 KB |
Output is correct |
10 |
Correct |
154 ms |
16864 KB |
Output is correct |
11 |
Correct |
141 ms |
16800 KB |
Output is correct |
12 |
Correct |
157 ms |
16860 KB |
Output is correct |
13 |
Correct |
113 ms |
16756 KB |
Output is correct |
14 |
Correct |
112 ms |
16772 KB |
Output is correct |
15 |
Correct |
111 ms |
16836 KB |
Output is correct |
16 |
Correct |
15 ms |
3428 KB |
Output is correct |
17 |
Correct |
16 ms |
3536 KB |
Output is correct |
18 |
Correct |
17 ms |
3564 KB |
Output is correct |
19 |
Correct |
17 ms |
3556 KB |
Output is correct |
20 |
Correct |
17 ms |
3556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
16764 KB |
Output is correct |
2 |
Correct |
116 ms |
16744 KB |
Output is correct |
3 |
Correct |
113 ms |
16748 KB |
Output is correct |
4 |
Correct |
117 ms |
16860 KB |
Output is correct |
5 |
Correct |
116 ms |
16796 KB |
Output is correct |
6 |
Correct |
142 ms |
16752 KB |
Output is correct |
7 |
Correct |
150 ms |
16808 KB |
Output is correct |
8 |
Correct |
141 ms |
16792 KB |
Output is correct |
9 |
Correct |
140 ms |
16860 KB |
Output is correct |
10 |
Correct |
154 ms |
16864 KB |
Output is correct |
11 |
Correct |
141 ms |
16800 KB |
Output is correct |
12 |
Correct |
157 ms |
16860 KB |
Output is correct |
13 |
Correct |
113 ms |
16756 KB |
Output is correct |
14 |
Correct |
112 ms |
16772 KB |
Output is correct |
15 |
Correct |
111 ms |
16836 KB |
Output is correct |
16 |
Correct |
15 ms |
3428 KB |
Output is correct |
17 |
Correct |
16 ms |
3536 KB |
Output is correct |
18 |
Correct |
17 ms |
3564 KB |
Output is correct |
19 |
Correct |
17 ms |
3556 KB |
Output is correct |
20 |
Correct |
17 ms |
3556 KB |
Output is correct |
21 |
Correct |
37 ms |
4076 KB |
Output is correct |
22 |
Correct |
40 ms |
4052 KB |
Output is correct |
23 |
Correct |
35 ms |
4052 KB |
Output is correct |
24 |
Correct |
34 ms |
4052 KB |
Output is correct |
25 |
Correct |
37 ms |
4052 KB |
Output is correct |
26 |
Correct |
34 ms |
3896 KB |
Output is correct |
27 |
Correct |
33 ms |
4028 KB |
Output is correct |
28 |
Correct |
36 ms |
3852 KB |
Output is correct |
29 |
Correct |
33 ms |
3924 KB |
Output is correct |
30 |
Correct |
103 ms |
17064 KB |
Output is correct |
31 |
Correct |
103 ms |
16880 KB |
Output is correct |
32 |
Correct |
105 ms |
17240 KB |
Output is correct |
33 |
Correct |
110 ms |
17176 KB |
Output is correct |
34 |
Correct |
106 ms |
16884 KB |
Output is correct |
35 |
Correct |
103 ms |
16868 KB |
Output is correct |
36 |
Correct |
103 ms |
17136 KB |
Output is correct |
37 |
Correct |
106 ms |
17428 KB |
Output is correct |
38 |
Correct |
102 ms |
17036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2324 KB |
Output is correct |
2 |
Correct |
11 ms |
2260 KB |
Output is correct |
3 |
Correct |
11 ms |
2260 KB |
Output is correct |
4 |
Correct |
15 ms |
2260 KB |
Output is correct |
5 |
Correct |
13 ms |
2260 KB |
Output is correct |
6 |
Correct |
20 ms |
2260 KB |
Output is correct |
7 |
Correct |
21 ms |
2256 KB |
Output is correct |
8 |
Correct |
22 ms |
2332 KB |
Output is correct |
9 |
Correct |
21 ms |
2308 KB |
Output is correct |
10 |
Correct |
22 ms |
2344 KB |
Output is correct |
11 |
Correct |
14 ms |
2260 KB |
Output is correct |
12 |
Correct |
12 ms |
2296 KB |
Output is correct |
13 |
Correct |
12 ms |
2260 KB |
Output is correct |
14 |
Correct |
13 ms |
2260 KB |
Output is correct |
15 |
Correct |
13 ms |
2280 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
5 ms |
1236 KB |
Output is correct |
18 |
Correct |
6 ms |
1332 KB |
Output is correct |
19 |
Correct |
5 ms |
1236 KB |
Output is correct |
20 |
Correct |
6 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
16764 KB |
Output is correct |
2 |
Correct |
116 ms |
16744 KB |
Output is correct |
3 |
Correct |
113 ms |
16748 KB |
Output is correct |
4 |
Correct |
117 ms |
16860 KB |
Output is correct |
5 |
Correct |
116 ms |
16796 KB |
Output is correct |
6 |
Correct |
142 ms |
16752 KB |
Output is correct |
7 |
Correct |
150 ms |
16808 KB |
Output is correct |
8 |
Correct |
141 ms |
16792 KB |
Output is correct |
9 |
Correct |
140 ms |
16860 KB |
Output is correct |
10 |
Correct |
154 ms |
16864 KB |
Output is correct |
11 |
Correct |
141 ms |
16800 KB |
Output is correct |
12 |
Correct |
157 ms |
16860 KB |
Output is correct |
13 |
Correct |
113 ms |
16756 KB |
Output is correct |
14 |
Correct |
112 ms |
16772 KB |
Output is correct |
15 |
Correct |
111 ms |
16836 KB |
Output is correct |
16 |
Correct |
15 ms |
3428 KB |
Output is correct |
17 |
Correct |
16 ms |
3536 KB |
Output is correct |
18 |
Correct |
17 ms |
3564 KB |
Output is correct |
19 |
Correct |
17 ms |
3556 KB |
Output is correct |
20 |
Correct |
17 ms |
3556 KB |
Output is correct |
21 |
Correct |
37 ms |
4076 KB |
Output is correct |
22 |
Correct |
40 ms |
4052 KB |
Output is correct |
23 |
Correct |
35 ms |
4052 KB |
Output is correct |
24 |
Correct |
34 ms |
4052 KB |
Output is correct |
25 |
Correct |
37 ms |
4052 KB |
Output is correct |
26 |
Correct |
34 ms |
3896 KB |
Output is correct |
27 |
Correct |
33 ms |
4028 KB |
Output is correct |
28 |
Correct |
36 ms |
3852 KB |
Output is correct |
29 |
Correct |
33 ms |
3924 KB |
Output is correct |
30 |
Correct |
103 ms |
17064 KB |
Output is correct |
31 |
Correct |
103 ms |
16880 KB |
Output is correct |
32 |
Correct |
105 ms |
17240 KB |
Output is correct |
33 |
Correct |
110 ms |
17176 KB |
Output is correct |
34 |
Correct |
106 ms |
16884 KB |
Output is correct |
35 |
Correct |
103 ms |
16868 KB |
Output is correct |
36 |
Correct |
103 ms |
17136 KB |
Output is correct |
37 |
Correct |
106 ms |
17428 KB |
Output is correct |
38 |
Correct |
102 ms |
17036 KB |
Output is correct |
39 |
Correct |
12 ms |
2324 KB |
Output is correct |
40 |
Correct |
11 ms |
2260 KB |
Output is correct |
41 |
Correct |
11 ms |
2260 KB |
Output is correct |
42 |
Correct |
15 ms |
2260 KB |
Output is correct |
43 |
Correct |
13 ms |
2260 KB |
Output is correct |
44 |
Correct |
20 ms |
2260 KB |
Output is correct |
45 |
Correct |
21 ms |
2256 KB |
Output is correct |
46 |
Correct |
22 ms |
2332 KB |
Output is correct |
47 |
Correct |
21 ms |
2308 KB |
Output is correct |
48 |
Correct |
22 ms |
2344 KB |
Output is correct |
49 |
Correct |
14 ms |
2260 KB |
Output is correct |
50 |
Correct |
12 ms |
2296 KB |
Output is correct |
51 |
Correct |
12 ms |
2260 KB |
Output is correct |
52 |
Correct |
13 ms |
2260 KB |
Output is correct |
53 |
Correct |
13 ms |
2280 KB |
Output is correct |
54 |
Correct |
5 ms |
1236 KB |
Output is correct |
55 |
Correct |
5 ms |
1236 KB |
Output is correct |
56 |
Correct |
6 ms |
1332 KB |
Output is correct |
57 |
Correct |
5 ms |
1236 KB |
Output is correct |
58 |
Correct |
6 ms |
1236 KB |
Output is correct |
59 |
Execution timed out |
2048 ms |
34676 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |