Submission #251462

# Submission time Handle Problem Language Result Execution time Memory
251462 2020-07-21T10:13:20 Z atoiz Boat (APIO16_boat) C++14
100 / 100
692 ms 3692 KB
/*input
2
1 2
2 3
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;
 
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORA(i, a) for (auto &i : a)
#define FORB(i, a, b) for (int i = a; i >= b; --i)

const int MOD = 1000000007, MAXN = 507;
int add(int x, int y) { return x -= ((x += y) >= MOD ? MOD : 0); }
int sub(int x, int y) { return x += ((x -= y) < 0 ? MOD : 0); }
int mul(int x, int y) { return (int) ((long long) x * y % MOD); }
int getinv(int x, int m) { return (x == 1 ? 1 : m - (int) ((long long) m * getinv(m % x, x) / x)); }
int N, A[MAXN], B[MAXN], M;
int eval[MAXN * 2][MAXN], inv[MAXN], prefsum[MAXN * 2];
vector<int> vals, dp[MAXN * 2];

void addfunc(vector<int> &a, vector<int> b)
{
	a.resize(max(a.size(), b.size()));
	FOR(i, 0, int(min(a.size(), b.size())) - 1) a[i] = add(a[i], b[i]);
}

int evalat(vector<int> &a, int x)
{
	int ans = 0;
	FOR(i, 0, ((int) a.size()) - 1) ans = add(ans, mul(eval[x][i], a[i]));
	return ans;
}

vector<int> sumfunc(vector<int> &a)
{
	if (a.empty()) return a;
	vector<int> b = a;
	b.insert(b.begin(), 0);
	return b;
}

void addint(vector<int> &a, int x)
{
	if (not a.empty()) a[0] = add(a[0], x);
	else a.push_back(x);
}

void subint(vector<int> &a, int x)
{
	if (not a.empty()) a[0] = sub(a[0], x);
	else a.push_back(x ? MOD - x : x);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	FOR(i, 1, N) {
		cin >> A[i] >> B[i];
		--A[i];
		vals.push_back(A[i]);
		vals.push_back(B[i]);
	}
	vals.push_back(-1);
	sort(vals.begin(), vals.end());
	vals.erase(unique(vals.begin(), vals.end()), vals.end());
	FOR(i, 1, N) {
		A[i] = (int) (lower_bound(vals.begin(), vals.end(), A[i]) - vals.begin());
		B[i] = (int) (lower_bound(vals.begin(), vals.end(), B[i]) - vals.begin());
	}

	FOR(i, 1, N + 5) inv[i] = getinv(i, MOD);
	M = (int) vals.size() - 1;
	FOR(i, 1, M) {
		eval[i][0] = 1;
		FOR(j, 1, N + 5) eval[i][j] = mul(eval[i][j - 1], mul((vals[i] + j - 1) % MOD, inv[j]));
		// FOR(j, 1, N + 5) cout << vals[i] << ' ' << j << ": " << eval[i][j] << endl;
	}

	FOR(id, 1, N) {
		int l = A[id], r = B[id];
		// cout << l << ' ' << r << " - " << vals[l] << ' ' << vals[r] << endl;
		FOR(i, 1, r - 2) {
			vector<int> sumf = sumfunc(dp[i]);
			int cur = sub(evalat(sumf, i + 1), evalat(sumf, i));
			prefsum[i] = add(prefsum[i - 1], cur);
			// cout << vals[i] << ' ' << vals[i + 1] << ": " << cur << ' ' << dp[i].size() << endl;
		}
		// cout << "S" << endl;

		FORB(i, r - 1, l) {
			// cout << vals[i] << ' ' << vals[i + 1] << ": " << dp[i].size();
			vector<int> sumf = sumfunc(dp[i]);
			vector<int> vec = sumf;
			addint(vec, prefsum[i - 1]);
			subint(vec, evalat(sumf, i));
			addint(vec, 1);
			dp[i] = move(vec);
			// cout << " " << dp[i].size() << endl;
		}
	}

	// cout << " - " << endl;
	FOR(i, 1, M - 1) {
		vector<int> sumf = sumfunc(dp[i]);
		int cur = sub(evalat(sumf, i + 1), evalat(sumf, i));
		prefsum[i] = add(prefsum[i - 1], cur);
		// cout << vals[i] << ' ' << vals[i + 1] << ": " << cur << ' ' << dp[i].size() << endl;
	} 
	cout << prefsum[M - 1] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2432 KB Output is correct
2 Correct 13 ms 2432 KB Output is correct
3 Correct 13 ms 2432 KB Output is correct
4 Correct 13 ms 2432 KB Output is correct
5 Correct 13 ms 2432 KB Output is correct
6 Correct 19 ms 2432 KB Output is correct
7 Correct 19 ms 2432 KB Output is correct
8 Correct 22 ms 2432 KB Output is correct
9 Correct 28 ms 2432 KB Output is correct
10 Correct 18 ms 2432 KB Output is correct
11 Correct 19 ms 2432 KB Output is correct
12 Correct 18 ms 2432 KB Output is correct
13 Correct 19 ms 2432 KB Output is correct
14 Correct 29 ms 2432 KB Output is correct
15 Correct 19 ms 2432 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 6 ms 768 KB Output is correct
18 Correct 6 ms 768 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2432 KB Output is correct
2 Correct 13 ms 2432 KB Output is correct
3 Correct 13 ms 2432 KB Output is correct
4 Correct 13 ms 2432 KB Output is correct
5 Correct 13 ms 2432 KB Output is correct
6 Correct 19 ms 2432 KB Output is correct
7 Correct 19 ms 2432 KB Output is correct
8 Correct 22 ms 2432 KB Output is correct
9 Correct 28 ms 2432 KB Output is correct
10 Correct 18 ms 2432 KB Output is correct
11 Correct 19 ms 2432 KB Output is correct
12 Correct 18 ms 2432 KB Output is correct
13 Correct 19 ms 2432 KB Output is correct
14 Correct 29 ms 2432 KB Output is correct
15 Correct 19 ms 2432 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 6 ms 768 KB Output is correct
18 Correct 6 ms 768 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 324 ms 2936 KB Output is correct
22 Correct 315 ms 2940 KB Output is correct
23 Correct 291 ms 2808 KB Output is correct
24 Correct 309 ms 2888 KB Output is correct
25 Correct 335 ms 2936 KB Output is correct
26 Correct 593 ms 3324 KB Output is correct
27 Correct 605 ms 3320 KB Output is correct
28 Correct 603 ms 3348 KB Output is correct
29 Correct 580 ms 3192 KB Output is correct
30 Correct 29 ms 2432 KB Output is correct
31 Correct 29 ms 2432 KB Output is correct
32 Correct 29 ms 2432 KB Output is correct
33 Correct 30 ms 2432 KB Output is correct
34 Correct 30 ms 2432 KB Output is correct
35 Correct 20 ms 2432 KB Output is correct
36 Correct 22 ms 2432 KB Output is correct
37 Correct 21 ms 2432 KB Output is correct
38 Correct 22 ms 2432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 768 KB Output is correct
2 Correct 5 ms 768 KB Output is correct
3 Correct 5 ms 768 KB Output is correct
4 Correct 9 ms 768 KB Output is correct
5 Correct 6 ms 768 KB Output is correct
6 Correct 21 ms 768 KB Output is correct
7 Correct 9 ms 768 KB Output is correct
8 Correct 9 ms 768 KB Output is correct
9 Correct 12 ms 768 KB Output is correct
10 Correct 10 ms 768 KB Output is correct
11 Correct 10 ms 768 KB Output is correct
12 Correct 8 ms 768 KB Output is correct
13 Correct 10 ms 768 KB Output is correct
14 Correct 6 ms 768 KB Output is correct
15 Correct 7 ms 896 KB Output is correct
16 Correct 4 ms 640 KB Output is correct
17 Correct 3 ms 640 KB Output is correct
18 Correct 4 ms 640 KB Output is correct
19 Correct 4 ms 640 KB Output is correct
20 Correct 4 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2432 KB Output is correct
2 Correct 13 ms 2432 KB Output is correct
3 Correct 13 ms 2432 KB Output is correct
4 Correct 13 ms 2432 KB Output is correct
5 Correct 13 ms 2432 KB Output is correct
6 Correct 19 ms 2432 KB Output is correct
7 Correct 19 ms 2432 KB Output is correct
8 Correct 22 ms 2432 KB Output is correct
9 Correct 28 ms 2432 KB Output is correct
10 Correct 18 ms 2432 KB Output is correct
11 Correct 19 ms 2432 KB Output is correct
12 Correct 18 ms 2432 KB Output is correct
13 Correct 19 ms 2432 KB Output is correct
14 Correct 29 ms 2432 KB Output is correct
15 Correct 19 ms 2432 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 6 ms 768 KB Output is correct
18 Correct 6 ms 768 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 324 ms 2936 KB Output is correct
22 Correct 315 ms 2940 KB Output is correct
23 Correct 291 ms 2808 KB Output is correct
24 Correct 309 ms 2888 KB Output is correct
25 Correct 335 ms 2936 KB Output is correct
26 Correct 593 ms 3324 KB Output is correct
27 Correct 605 ms 3320 KB Output is correct
28 Correct 603 ms 3348 KB Output is correct
29 Correct 580 ms 3192 KB Output is correct
30 Correct 29 ms 2432 KB Output is correct
31 Correct 29 ms 2432 KB Output is correct
32 Correct 29 ms 2432 KB Output is correct
33 Correct 30 ms 2432 KB Output is correct
34 Correct 30 ms 2432 KB Output is correct
35 Correct 20 ms 2432 KB Output is correct
36 Correct 22 ms 2432 KB Output is correct
37 Correct 21 ms 2432 KB Output is correct
38 Correct 22 ms 2432 KB Output is correct
39 Correct 6 ms 768 KB Output is correct
40 Correct 5 ms 768 KB Output is correct
41 Correct 5 ms 768 KB Output is correct
42 Correct 9 ms 768 KB Output is correct
43 Correct 6 ms 768 KB Output is correct
44 Correct 21 ms 768 KB Output is correct
45 Correct 9 ms 768 KB Output is correct
46 Correct 9 ms 768 KB Output is correct
47 Correct 12 ms 768 KB Output is correct
48 Correct 10 ms 768 KB Output is correct
49 Correct 10 ms 768 KB Output is correct
50 Correct 8 ms 768 KB Output is correct
51 Correct 10 ms 768 KB Output is correct
52 Correct 6 ms 768 KB Output is correct
53 Correct 7 ms 896 KB Output is correct
54 Correct 4 ms 640 KB Output is correct
55 Correct 3 ms 640 KB Output is correct
56 Correct 4 ms 640 KB Output is correct
57 Correct 4 ms 640 KB Output is correct
58 Correct 4 ms 640 KB Output is correct
59 Correct 356 ms 3064 KB Output is correct
60 Correct 332 ms 3064 KB Output is correct
61 Correct 323 ms 3064 KB Output is correct
62 Correct 361 ms 3192 KB Output is correct
63 Correct 340 ms 3064 KB Output is correct
64 Correct 669 ms 3576 KB Output is correct
65 Correct 670 ms 3576 KB Output is correct
66 Correct 672 ms 3576 KB Output is correct
67 Correct 670 ms 3576 KB Output is correct
68 Correct 692 ms 3692 KB Output is correct
69 Correct 394 ms 3108 KB Output is correct
70 Correct 407 ms 3064 KB Output is correct
71 Correct 414 ms 3124 KB Output is correct
72 Correct 416 ms 3036 KB Output is correct
73 Correct 422 ms 3064 KB Output is correct
74 Correct 69 ms 888 KB Output is correct
75 Correct 77 ms 904 KB Output is correct
76 Correct 71 ms 896 KB Output is correct
77 Correct 73 ms 1016 KB Output is correct
78 Correct 76 ms 888 KB Output is correct