Submission #107038

# Submission time Handle Problem Language Result Execution time Memory
107038 2019-04-21T13:45:07 Z kek Boat (APIO16_boat) C++14
100 / 100
874 ms 8440 KB
// #include "gap.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define all(v) v.begin(), v.end()
#define len(v) ((int)(v).size())
#define pb push_back
#define kek pop_back
#define pii pair<int, int>
#define mp make_pair

#define debug(x) cout << #x << " = " << x << endl;

const int INF = 1e18 + 666;

template<class t1, class t2>
bool cmin(t1 &a, const t2 &b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class t1, class t2>
bool cmax(t1 &a, const t2 &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

// void MinMax(ll, ll, ll*, ll*);

// ll firstGroup(int);
// ll secondGroup(int);

// ll calc_ans(vector<ll> a) {
// 	sort(all(a));
// 	ll ans = 0;
// 	for (int i = 0; i + 1 < len(a); ++i) {
// 		cmax(ans, a[i + 1] - a[i]);
// 	}
// 	return ans;
// }

// ll findGap(int t, int n) {
// 	if (t == 1) {
// 		return firstGroup(n);
// 	} else {
// 		return secondGroup(n);
// 	}
// }

// ll firstGroup(int n) {
// 	vector<ll> a;
// 	ll l = 0, r = 1e18;
// 	while (len(a) < n) {
// 		MinMax(l, r, &l, &r);
// 		if (l == -1) {
// 			break;
// 		}
// 		a.pb(l);
// 		if (l != r) {
// 			a.pb(r);
// 		}
// 		++l;
// 		--r;
// 	}
// 	return calc_ans(a);
// }

// vector<ll> restore(ll l, ll r) {
// 	if (l > r) {
// 		return {};
// 	}
// 	MinMax(l, r, &l, &r);
// 	if (l == -1) {
// 		return {};
// 	}
// 	if (l == r) {
// 		return {l};
// 	}
// 	ll m = (l + r) >> 1;
// 	vector<ll> ans = {l, r};
// 	for (auto &x : restore(l + 1, m)) {
// 		ans.pb(x);
// 	}
// 	for (auto &x : restore(m + 1, r - 1)) {
// 		ans.pb(x);
// 	}
// 	return ans;
// }

// ll secondGroup(int n) {
// 	return calc_ans(restore(0, 1e18));
// }

void run();

signed main() {
	iostream::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}

const int mod = 1e9 + 7;
// const int maxn = 1e9 + 100;

// struct fenv {
// 	unordered_map<int, int> tree;
// 	// int maxn;

// 	fenv() {
// 		tree.rehash(1e7 + 100);
// 	}

// 	int get(int i) {
// 		++i;
// 		int sm = 0;
// 		for (; i > 0; i -= f(i)) {
// 			auto it = tree.find(i);
// 			if (it != tree.end()) {
// 				sm += it->second;
// 				if (sm >= mod) {
// 					sm -= mod;
// 				}
// 			}
// 		}
// 		return sm;
// 	}

// 	void plus(int i, int v) {
// 		++i;
// 		for (; i < maxn; i += f(i)) {
// 			int &cur = tree[i];
// 			cur += v;
// 			// if (cur < 0) {
// 			// 	cur += mod;
// 			// }
// 			if (cur >= mod) {
// 				cur -= mod;
// 			}
// 		}
// 	}

// 	int f(int i) {
// 		return i & (-i);
// 	}
// };

int pow(int a, int b, int m) {
	int res = 1;
	for (; b > 0; b >>= 1) {
		if (b & 1) {
			res *= a;
			res %= m;
		}
		a *= a;
		a %= m;
	}
	return res;
}

pair<vector<int>, vector<pii>> make_kek(const vector<pii> &v) {
	vector<int> points;
	points.pb(0);
	points.pb(1);
	for (auto &x : v) {
		points.pb(x.first);
		points.pb(x.second + 1);
	}
	sort(all(points));
	points.resize(unique(all(points)) - points.begin());
	vector<int> lens;
	for (int i = 0; i + 1 < len(points); ++i) {
		lens.pb(points[i + 1] - points[i]);
	}
	vector<pii> ans;
	for (auto &x : v) {
		int l = lower_bound(all(points), x.first) - points.begin();
		int r = upper_bound(all(points), x.second) - points.begin();
		ans.pb({l, r - 1});
	}
	return {lens, ans};
}

void update(vector<int> &r, int &s, const vector<int> &c) {
	for (int i = len(r) - 1; i > 0; --i) {
		r[i] += r[i - 1];
		s += (r[i - 1] * c[i + 1]) % mod;
		if (r[i] >= mod) {
			r[i] -= mod;
		}
		if (s >= mod) {
			s -= mod;
		}
	}
}

void run() {
	int n;
	cin >> n;
	vector<int> rev(n + 1, 0);
	rev[1] = 1;
	for (int i = 2; i < n + 1; ++i) {
		rev[i] = mod - ((mod / i) * rev[mod % i]) % mod;
		// assert(rev[i] >= 0 && rev[i] < mod);
	}
	vector<pii> v(n);
	for (auto &x : v) {
		cin >> x.first >> x.second;
	}
	vector<int> lens;
	tie(lens, v) = make_kek(v);
	int m = len(lens);
	vector<vector<int>> C(m, vector<int>(n + 1));
	for (int i = 0; i < m; ++i) {
		C[i][0] = 1;
		for (int j = 1; j <= n; ++j) {
			C[i][j] = (C[i][j - 1] * (lens[i] - j + 1)) % mod;
			C[i][j] = (C[i][j] * rev[j]) % mod;
		}
	}
	vector<vector<int>> dp(m, vector<int>(n, 0));
	vector<int> sm(m, 0);
	dp[0][0] = 1;
	sm[0] = 1;
	for (auto &x : v) {
		int psm = 0;
		for (int i = 0; i <= x.second; ++i) {
			psm += sm[i];
		}
		psm %= mod;
		for (int i = x.second; i >= x.first; --i) {
			psm -= sm[i];
			if (psm < 0) {
				psm += mod;
			}
			update(dp[i], sm[i], C[i]);
			dp[i][0] += psm;
			if (dp[i][0] >= mod) {
				dp[i][0] -= mod;
			}
			sm[i] += (psm * lens[i]) % mod;
			if (sm[i] >= mod) {
				sm[i] -= mod;
			}
		}
	}
	int ans = accumulate(all(sm), 0ll) - 1;
	cout << ans % mod << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 8320 KB Output is correct
2 Correct 17 ms 8320 KB Output is correct
3 Correct 16 ms 8320 KB Output is correct
4 Correct 19 ms 8320 KB Output is correct
5 Correct 17 ms 8320 KB Output is correct
6 Correct 18 ms 8320 KB Output is correct
7 Correct 18 ms 8320 KB Output is correct
8 Correct 18 ms 8320 KB Output is correct
9 Correct 18 ms 8320 KB Output is correct
10 Correct 20 ms 8312 KB Output is correct
11 Correct 18 ms 8320 KB Output is correct
12 Correct 18 ms 8320 KB Output is correct
13 Correct 17 ms 8248 KB Output is correct
14 Correct 17 ms 8320 KB Output is correct
15 Correct 18 ms 8320 KB Output is correct
16 Correct 6 ms 1792 KB Output is correct
17 Correct 6 ms 1920 KB Output is correct
18 Correct 7 ms 1792 KB Output is correct
19 Correct 7 ms 1920 KB Output is correct
20 Correct 6 ms 1792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 8320 KB Output is correct
2 Correct 17 ms 8320 KB Output is correct
3 Correct 16 ms 8320 KB Output is correct
4 Correct 19 ms 8320 KB Output is correct
5 Correct 17 ms 8320 KB Output is correct
6 Correct 18 ms 8320 KB Output is correct
7 Correct 18 ms 8320 KB Output is correct
8 Correct 18 ms 8320 KB Output is correct
9 Correct 18 ms 8320 KB Output is correct
10 Correct 20 ms 8312 KB Output is correct
11 Correct 18 ms 8320 KB Output is correct
12 Correct 18 ms 8320 KB Output is correct
13 Correct 17 ms 8248 KB Output is correct
14 Correct 17 ms 8320 KB Output is correct
15 Correct 18 ms 8320 KB Output is correct
16 Correct 6 ms 1792 KB Output is correct
17 Correct 6 ms 1920 KB Output is correct
18 Correct 7 ms 1792 KB Output is correct
19 Correct 7 ms 1920 KB Output is correct
20 Correct 6 ms 1792 KB Output is correct
21 Correct 357 ms 7524 KB Output is correct
22 Correct 362 ms 7800 KB Output is correct
23 Correct 301 ms 7672 KB Output is correct
24 Correct 306 ms 7552 KB Output is correct
25 Correct 394 ms 7552 KB Output is correct
26 Correct 555 ms 7416 KB Output is correct
27 Correct 531 ms 7544 KB Output is correct
28 Correct 494 ms 7416 KB Output is correct
29 Correct 506 ms 7296 KB Output is correct
30 Correct 21 ms 8320 KB Output is correct
31 Correct 19 ms 8272 KB Output is correct
32 Correct 21 ms 8192 KB Output is correct
33 Correct 19 ms 8320 KB Output is correct
34 Correct 19 ms 8320 KB Output is correct
35 Correct 21 ms 8192 KB Output is correct
36 Correct 21 ms 8192 KB Output is correct
37 Correct 20 ms 8192 KB Output is correct
38 Correct 21 ms 8292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 640 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 7 ms 728 KB Output is correct
5 Correct 7 ms 640 KB Output is correct
6 Correct 9 ms 768 KB Output is correct
7 Correct 11 ms 640 KB Output is correct
8 Correct 10 ms 640 KB Output is correct
9 Correct 10 ms 640 KB Output is correct
10 Correct 10 ms 640 KB Output is correct
11 Correct 7 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 640 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 8320 KB Output is correct
2 Correct 17 ms 8320 KB Output is correct
3 Correct 16 ms 8320 KB Output is correct
4 Correct 19 ms 8320 KB Output is correct
5 Correct 17 ms 8320 KB Output is correct
6 Correct 18 ms 8320 KB Output is correct
7 Correct 18 ms 8320 KB Output is correct
8 Correct 18 ms 8320 KB Output is correct
9 Correct 18 ms 8320 KB Output is correct
10 Correct 20 ms 8312 KB Output is correct
11 Correct 18 ms 8320 KB Output is correct
12 Correct 18 ms 8320 KB Output is correct
13 Correct 17 ms 8248 KB Output is correct
14 Correct 17 ms 8320 KB Output is correct
15 Correct 18 ms 8320 KB Output is correct
16 Correct 6 ms 1792 KB Output is correct
17 Correct 6 ms 1920 KB Output is correct
18 Correct 7 ms 1792 KB Output is correct
19 Correct 7 ms 1920 KB Output is correct
20 Correct 6 ms 1792 KB Output is correct
21 Correct 357 ms 7524 KB Output is correct
22 Correct 362 ms 7800 KB Output is correct
23 Correct 301 ms 7672 KB Output is correct
24 Correct 306 ms 7552 KB Output is correct
25 Correct 394 ms 7552 KB Output is correct
26 Correct 555 ms 7416 KB Output is correct
27 Correct 531 ms 7544 KB Output is correct
28 Correct 494 ms 7416 KB Output is correct
29 Correct 506 ms 7296 KB Output is correct
30 Correct 21 ms 8320 KB Output is correct
31 Correct 19 ms 8272 KB Output is correct
32 Correct 21 ms 8192 KB Output is correct
33 Correct 19 ms 8320 KB Output is correct
34 Correct 19 ms 8320 KB Output is correct
35 Correct 21 ms 8192 KB Output is correct
36 Correct 21 ms 8192 KB Output is correct
37 Correct 20 ms 8192 KB Output is correct
38 Correct 21 ms 8292 KB Output is correct
39 Correct 6 ms 640 KB Output is correct
40 Correct 5 ms 640 KB Output is correct
41 Correct 6 ms 640 KB Output is correct
42 Correct 7 ms 728 KB Output is correct
43 Correct 7 ms 640 KB Output is correct
44 Correct 9 ms 768 KB Output is correct
45 Correct 11 ms 640 KB Output is correct
46 Correct 10 ms 640 KB Output is correct
47 Correct 10 ms 640 KB Output is correct
48 Correct 10 ms 640 KB Output is correct
49 Correct 7 ms 640 KB Output is correct
50 Correct 6 ms 640 KB Output is correct
51 Correct 6 ms 640 KB Output is correct
52 Correct 6 ms 640 KB Output is correct
53 Correct 7 ms 640 KB Output is correct
54 Correct 5 ms 512 KB Output is correct
55 Correct 5 ms 512 KB Output is correct
56 Correct 4 ms 512 KB Output is correct
57 Correct 4 ms 512 KB Output is correct
58 Correct 5 ms 512 KB Output is correct
59 Correct 531 ms 8312 KB Output is correct
60 Correct 414 ms 8440 KB Output is correct
61 Correct 459 ms 8428 KB Output is correct
62 Correct 463 ms 8440 KB Output is correct
63 Correct 481 ms 8440 KB Output is correct
64 Correct 874 ms 8440 KB Output is correct
65 Correct 705 ms 8328 KB Output is correct
66 Correct 772 ms 8320 KB Output is correct
67 Correct 818 ms 8440 KB Output is correct
68 Correct 827 ms 8340 KB Output is correct
69 Correct 404 ms 8320 KB Output is correct
70 Correct 407 ms 8324 KB Output is correct
71 Correct 425 ms 8440 KB Output is correct
72 Correct 429 ms 8320 KB Output is correct
73 Correct 426 ms 8368 KB Output is correct
74 Correct 80 ms 1792 KB Output is correct
75 Correct 84 ms 1888 KB Output is correct
76 Correct 96 ms 1792 KB Output is correct
77 Correct 78 ms 1936 KB Output is correct
78 Correct 91 ms 1872 KB Output is correct