Submission #705067

# Submission time Handle Problem Language Result Execution time Memory
705067 2023-03-03T10:30:09 Z 600Mihnea Boat (APIO16_boat) C++17
100 / 100
1548 ms 9204 KB
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>

using namespace std;

typedef long long ll;
const int M = (int)1e9 + 7;
const int FACTN = 300000 + 7;

int add(int a, int b) {
	a += b;
	if (a >= M) return a - M;
	if (a < 0) return a + M;
	return a;
}

int mul(int a, int b) {
	return a * (ll)b % M;
}

int add(int a, int b, int c) {
	return add(add(a, b), c);
}

int mul(int a, int b, int c) {
	return mul(mul(a, b), c);
}

int add(int a, int b, int c, int d) {
	return add(add(a, b, c), d);
}

int mul(int a, int b, int c, int d) {
	return mul(mul(a, b, c), d);
}

int add(int a, int b, int c, int d, int e) {
	return add(add(a, b, c, d), e);
}

int mul(int a, int b, int c, int d, int e) {
	return mul(mul(a, b, c, d), e);
}

int add(int a, int b, int c, int d, int e, int f) {
	return add(add(a, b, c, d, e), f);
}

int mul(int a, int b, int c, int d, int e, int f) {
	return mul(mul(a, b, c, d, e), f);
}

int add(int a, int b, int c, int d, int e, int f, int g) {
	return add(add(a, b, c, d, e, f), g);
}

int mul(int a, int b, int c, int d, int e, int f, int g) {
	return mul(mul(a, b, c, d, e, f), g);
}

int add(int a, int b, int c, int d, int e, int f, int g, int h) {
	return add(add(a, b, c, d, e, f, g), h);
}

int mul(int a, int b, int c, int d, int e, int f, int g, int h) {
	return mul(mul(a, b, c, d, e, f, g), h);
}

int pw(int a, int b) {
	int r = 1;
	while (b) {
		if (b & 1) r = mul(r, a);
		a = mul(a, a);
		b /= 2;
	}
	return r;
}

int dv(int a, int b) {
	return mul(a, pw(b, M - 2));
}

void addup(int& a, int b) {
	a = add(a, b);
}

void mulup(int& a, int b) {
	a = mul(a, b);
}

void dvup(int& a, int b) {
	a = dv(a, b);
}

int fact[FACTN], ifact[FACTN];

void computeFACT() {
	fact[0] = 1;
	for (int i = 1; i < FACTN; i++) fact[i] = mul(fact[i - 1], i);
	ifact[FACTN - 1] = dv(1, fact[FACTN - 1]);
	for (int i = FACTN - 2; i >= 0; i--) ifact[i] = mul(ifact[i + 1], i + 1);
}

int getCOMB(int n, int k) {
	return mul(fact[n], mul(ifact[k], ifact[n - k]));
}

int fastComb(int n, int k)
{
	if (k > n)
	{
		return 0;
	}
	// n is very big
	// k is small
	assert(0 <= k && k <= n);
	int up = 1;
	for (int i = 1; i <= k; i++)
	{
		mulup(up, n + 1 - i);
	}
	return mul(up, ifact[k]);
}


int dumb(vector<pair<int, int>> segs)
{
	int n = (int)segs.size();
	vector<vector<int>> dp(n);
	for (int i = 0; i < n; i++)
	{
		assert(segs[i].first <= segs[i].second);
		dp[i].resize(segs[i].second - segs[i].first + 1, 1);
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < i; j++)
		{
			for (int y = segs[i].first; y <= segs[i].second; y++)
			{
				if (segs[j].second <= y - 1)
				{
					addup(dp[i][y - segs[i].first], dp[j].back());
				}
				else
				{
					if (segs[j].first <= y - 1)
					{
						addup(dp[i][y - segs[i].first], dp[j][y - 1 - segs[j].first]);
					}
				}
			}
		}
		int cur = 0;
		for (auto& it : dp[i])
		{
			addup(cur, it);
			it = cur;
		}
	}
	int sol = 0;
	for (int i = 0; i < n; i++)
	{
		addup(sol, dp[i].back());
	}
	return sol;
}


mt19937 rng(228);

int getrng(int l, int r)
{
	assert(l <= r);
	int x = rng() % (r - l + 1) + l;
	assert(l <= x && x <= r);
	return x;
}

vector<pair<int, int>> getrng()
{
	const int N = 10;
	const int L = 10;
	int n = getrng(1, N);
	vector<pair<int, int>> v(n);
	for (auto& it : v)
	{
		it.first = getrng(1, L);
		it.second = getrng(1, L);
		if (it.first > it.second)
		{
			swap(it.first, it.second);
		}
		assert(it.first <= it.second);
	}
	return v;
}

int smart(vector<pair<int, int>> v)
{
	int n = (int)v.size();
	map<int, int> mp;
	for (auto& it : v)
	{
		mp[it.first] = 0;
		mp[it.second + 1] = 0;
	}
	vector<int> xs;
	int y = 0;
	for (auto& it : mp)
	{
		xs.push_back(it.first);
		it.second = y++;
	}
	vector<int> dim;
	for (int i = 0; i + 1 < (int)xs.size(); i++)
	{
		dim.push_back(xs[i + 1] - xs[i]);
	}
	assert((int)dim.size() == y - 1);
	y = (int)dim.size();
	for (auto& it : v)
	{
		assert(mp.count(it.first));
		assert(mp.count(it.second + 1));
		it.first = mp[it.first];
		it.second = mp[it.second + 1] - 1;
	}
	vector<vector<int>> dp(y, vector<int>(n + 1, 0));
	vector<vector<int>> prod(y, vector<int>(n + 1, 0));
	for (int i = 0; i < y; i++)
	{
		prod[i][0] = 1;
		for (int j = 1; j <= n; j++)
		{
			prod[i][j] = mul(prod[i][j - 1], dim[i] + 1 - j);
		}
	}
	for (int i = 0; i < y; i++)
	{
		for (int t = 1; t <= n; t++)
		{
			mulup(prod[i][t], ifact[t]);
		}
	}
	for (auto& seg : v)
	{
		vector<int> sigma(y + 1, 0);
		int l = seg.first, r = seg.second;
		auto ndp = dp; // nu fac nimic aici
		// incep ceva nou aici
		for (int i = l; i <= r; i++)
		{
			addup(ndp[i][1], +1);
		}
		for (int i = 0; i < y; i++)
		{
			int orice = 0;
			for (int t = 1; t <= n; t++)
			{
				addup(orice, mul(dp[i][t], prod[i][t]));
			}
			if (max(l, i + 1) <= min(r, y - 1))
			{
				addup(sigma[max(l, i + 1)], orice);
				addup(sigma[min(r, y - 1) + 1], -orice);
			}
		}
		int cur = 0;
		for (int i = 0; i < y; i++)
		{
			addup(cur, sigma[i]);
			addup(ndp[i][1], cur);
		}
		for (int i = l; i <= r; i++)
		{
			for (int t = 1; t <= n; t++)
			{
				addup(ndp[i][t], dp[i][t - 1]);
			}
		}
		dp = ndp;
	}
	int sol = 0;
	for (int i = 0; i < y; i++)
	{
		for (int t = 0; t <= n; t++)
		{
			addup(sol, mul(dp[i][t], prod[i][t]));
		}
	}
	return sol;
}

signed main()
{
	computeFACT();
#ifdef ONPC	
	FILE* stream;
	freopen_s(&stream, "input.txt", "r", stdin);
#else
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif

	if (0)
	{
		vector<pair<int, int>> v;
		v = { {1, 2}, {1, 1} };
		int d = dumb(v);
		int s = smart(v);
		if (d != s)
		{
			cout << "WA!\n";
			cout << (int)v.size() << "\n";
			for (auto& it : v)
			{
				cout << it.first << " " << it.second << "\n";
			}
			cout << "good = " << d << "\n";
			cout << "me   = " << s << "\n";
			exit(0);
		}
		cout << "AC!\n";
		exit(0);
	}
	
	if (0)
	{
		cout << "debug time!\n";
		long long printtime = (long long)1e4;
		for (long long tc = 1; 1; tc++)
		{
			vector<pair<int, int>> v = getrng();
			int d = dumb(v);
			int s = smart(v);
			if (tc % printtime == 0)
			{
				cout << "done " << tc << ", n = " << (int)v.size() << ", sol = " << s << "\n";
			}
			if (d != s)
			{
				cout << "WA!\n";
				cout << (int)v.size() << "\n";
				for (auto& it : v)
				{
					cout << it.first << " " << it.second << "\n";
				}
				cout << "good = " << d << "\n";
				cout << "me   = " << s << "\n";
				exit(0);
			}
		}
		exit(0);
	}

	int n;
	cin >> n;
	vector<pair<int, int>> v(n);
	for (auto& it : v)
	{
		cin >> it.first >> it.second;
	}
	
	//cout << dumb(v) << "\n";
	cout << smart(v) << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1047 ms 8996 KB Output is correct
2 Correct 1022 ms 8796 KB Output is correct
3 Correct 1006 ms 8832 KB Output is correct
4 Correct 1020 ms 8984 KB Output is correct
5 Correct 1012 ms 8780 KB Output is correct
6 Correct 1041 ms 9040 KB Output is correct
7 Correct 1032 ms 8816 KB Output is correct
8 Correct 1037 ms 8976 KB Output is correct
9 Correct 1027 ms 8908 KB Output is correct
10 Correct 1010 ms 8788 KB Output is correct
11 Correct 1000 ms 8908 KB Output is correct
12 Correct 1053 ms 8868 KB Output is correct
13 Correct 1033 ms 8780 KB Output is correct
14 Correct 1010 ms 8888 KB Output is correct
15 Correct 1049 ms 8976 KB Output is correct
16 Correct 163 ms 3780 KB Output is correct
17 Correct 175 ms 3816 KB Output is correct
18 Correct 172 ms 3908 KB Output is correct
19 Correct 180 ms 4220 KB Output is correct
20 Correct 170 ms 3800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1047 ms 8996 KB Output is correct
2 Correct 1022 ms 8796 KB Output is correct
3 Correct 1006 ms 8832 KB Output is correct
4 Correct 1020 ms 8984 KB Output is correct
5 Correct 1012 ms 8780 KB Output is correct
6 Correct 1041 ms 9040 KB Output is correct
7 Correct 1032 ms 8816 KB Output is correct
8 Correct 1037 ms 8976 KB Output is correct
9 Correct 1027 ms 8908 KB Output is correct
10 Correct 1010 ms 8788 KB Output is correct
11 Correct 1000 ms 8908 KB Output is correct
12 Correct 1053 ms 8868 KB Output is correct
13 Correct 1033 ms 8780 KB Output is correct
14 Correct 1010 ms 8888 KB Output is correct
15 Correct 1049 ms 8976 KB Output is correct
16 Correct 163 ms 3780 KB Output is correct
17 Correct 175 ms 3816 KB Output is correct
18 Correct 172 ms 3908 KB Output is correct
19 Correct 180 ms 4220 KB Output is correct
20 Correct 170 ms 3800 KB Output is correct
21 Correct 1076 ms 8260 KB Output is correct
22 Correct 1071 ms 8296 KB Output is correct
23 Correct 1082 ms 8328 KB Output is correct
24 Correct 1046 ms 8496 KB Output is correct
25 Correct 1022 ms 8396 KB Output is correct
26 Correct 1109 ms 8164 KB Output is correct
27 Correct 1122 ms 8288 KB Output is correct
28 Correct 1121 ms 8156 KB Output is correct
29 Correct 1128 ms 8144 KB Output is correct
30 Correct 1047 ms 9092 KB Output is correct
31 Correct 1004 ms 8820 KB Output is correct
32 Correct 1016 ms 9164 KB Output is correct
33 Correct 1068 ms 8768 KB Output is correct
34 Correct 1049 ms 9200 KB Output is correct
35 Correct 1066 ms 8912 KB Output is correct
36 Correct 1034 ms 8920 KB Output is correct
37 Correct 1031 ms 9016 KB Output is correct
38 Correct 1052 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2900 KB Output is correct
2 Correct 14 ms 2936 KB Output is correct
3 Correct 15 ms 2940 KB Output is correct
4 Correct 14 ms 2944 KB Output is correct
5 Correct 15 ms 2900 KB Output is correct
6 Correct 16 ms 2900 KB Output is correct
7 Correct 16 ms 2900 KB Output is correct
8 Correct 16 ms 2900 KB Output is correct
9 Correct 16 ms 2892 KB Output is correct
10 Correct 16 ms 2948 KB Output is correct
11 Correct 15 ms 2900 KB Output is correct
12 Correct 15 ms 2948 KB Output is correct
13 Correct 14 ms 2860 KB Output is correct
14 Correct 15 ms 2940 KB Output is correct
15 Correct 15 ms 2940 KB Output is correct
16 Correct 10 ms 2832 KB Output is correct
17 Correct 10 ms 2804 KB Output is correct
18 Correct 10 ms 2828 KB Output is correct
19 Correct 10 ms 2828 KB Output is correct
20 Correct 10 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1047 ms 8996 KB Output is correct
2 Correct 1022 ms 8796 KB Output is correct
3 Correct 1006 ms 8832 KB Output is correct
4 Correct 1020 ms 8984 KB Output is correct
5 Correct 1012 ms 8780 KB Output is correct
6 Correct 1041 ms 9040 KB Output is correct
7 Correct 1032 ms 8816 KB Output is correct
8 Correct 1037 ms 8976 KB Output is correct
9 Correct 1027 ms 8908 KB Output is correct
10 Correct 1010 ms 8788 KB Output is correct
11 Correct 1000 ms 8908 KB Output is correct
12 Correct 1053 ms 8868 KB Output is correct
13 Correct 1033 ms 8780 KB Output is correct
14 Correct 1010 ms 8888 KB Output is correct
15 Correct 1049 ms 8976 KB Output is correct
16 Correct 163 ms 3780 KB Output is correct
17 Correct 175 ms 3816 KB Output is correct
18 Correct 172 ms 3908 KB Output is correct
19 Correct 180 ms 4220 KB Output is correct
20 Correct 170 ms 3800 KB Output is correct
21 Correct 1076 ms 8260 KB Output is correct
22 Correct 1071 ms 8296 KB Output is correct
23 Correct 1082 ms 8328 KB Output is correct
24 Correct 1046 ms 8496 KB Output is correct
25 Correct 1022 ms 8396 KB Output is correct
26 Correct 1109 ms 8164 KB Output is correct
27 Correct 1122 ms 8288 KB Output is correct
28 Correct 1121 ms 8156 KB Output is correct
29 Correct 1128 ms 8144 KB Output is correct
30 Correct 1047 ms 9092 KB Output is correct
31 Correct 1004 ms 8820 KB Output is correct
32 Correct 1016 ms 9164 KB Output is correct
33 Correct 1068 ms 8768 KB Output is correct
34 Correct 1049 ms 9200 KB Output is correct
35 Correct 1066 ms 8912 KB Output is correct
36 Correct 1034 ms 8920 KB Output is correct
37 Correct 1031 ms 9016 KB Output is correct
38 Correct 1052 ms 8960 KB Output is correct
39 Correct 14 ms 2900 KB Output is correct
40 Correct 14 ms 2936 KB Output is correct
41 Correct 15 ms 2940 KB Output is correct
42 Correct 14 ms 2944 KB Output is correct
43 Correct 15 ms 2900 KB Output is correct
44 Correct 16 ms 2900 KB Output is correct
45 Correct 16 ms 2900 KB Output is correct
46 Correct 16 ms 2900 KB Output is correct
47 Correct 16 ms 2892 KB Output is correct
48 Correct 16 ms 2948 KB Output is correct
49 Correct 15 ms 2900 KB Output is correct
50 Correct 15 ms 2948 KB Output is correct
51 Correct 14 ms 2860 KB Output is correct
52 Correct 15 ms 2940 KB Output is correct
53 Correct 15 ms 2940 KB Output is correct
54 Correct 10 ms 2832 KB Output is correct
55 Correct 10 ms 2804 KB Output is correct
56 Correct 10 ms 2828 KB Output is correct
57 Correct 10 ms 2828 KB Output is correct
58 Correct 10 ms 2772 KB Output is correct
59 Correct 1335 ms 8848 KB Output is correct
60 Correct 1321 ms 9204 KB Output is correct
61 Correct 1318 ms 8920 KB Output is correct
62 Correct 1342 ms 8936 KB Output is correct
63 Correct 1351 ms 8940 KB Output is correct
64 Correct 1548 ms 8880 KB Output is correct
65 Correct 1534 ms 9016 KB Output is correct
66 Correct 1511 ms 8764 KB Output is correct
67 Correct 1546 ms 8972 KB Output is correct
68 Correct 1529 ms 8968 KB Output is correct
69 Correct 1334 ms 9108 KB Output is correct
70 Correct 1351 ms 9008 KB Output is correct
71 Correct 1339 ms 8740 KB Output is correct
72 Correct 1385 ms 8920 KB Output is correct
73 Correct 1353 ms 8876 KB Output is correct
74 Correct 210 ms 3852 KB Output is correct
75 Correct 212 ms 3804 KB Output is correct
76 Correct 217 ms 3800 KB Output is correct
77 Correct 214 ms 4092 KB Output is correct
78 Correct 212 ms 3940 KB Output is correct