Submission #653754

# Submission time Handle Problem Language Result Execution time Memory
653754 2022-10-27T22:02:03 Z ksjsjsjsjsjsjs Boat (APIO16_boat) C++14
100 / 100
1246 ms 6348 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
	GOAT
----------------------------------------------------------------
*/
const int M = 1e9 + 7;
const int N = 502;
const int Nx2 = 1002;
int n, m, inv[N], d[Nx2][N][2], d1[Nx2][N];
pi a[N];
vpi seg, seg1;
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n;
    FOR(i, 1, n) cin >> a[i].st >> a[i].nd;
    seg.pb({1, 1e9});
    FOR(i, 1, n) {
    	seg1.clear();
        EACH(j, seg) {
            int l = j.st, r = j.nd;
            if (a[i].st > l && a[i].st <= r && a[i].nd >= r) {
            	j = {l, a[i].st - 1};
            	seg1.pb({a[i].st, r});
            }
            else if (a[i].nd < r && a[i].nd >= l && a[i].st <= l) {
            	j = {l, a[i].nd};
            	seg1.pb({a[i].nd + 1, r});
            }
            else if (l < a[i].st && a[i].nd < r) {
            	j = {l, a[i].st - 1};
            	seg1.pb(a[i]);
            	seg1.pb({a[i].nd + 1, r});
            }
        }
        EACH(j, seg1) seg.pb(j);
    }
    seg.pb({-1, -1});
    sort(all(seg));
    m = sz(seg) - 1;
    inv[1] = 1;
    FOR(i, 2, n) inv[i] = 1LL * (-M / i + M) % M * inv[M % i] % M;
    FOR(i, 1, m) {
        d1[i][0] = 1;
        int cnt = min(n, seg[i].nd - seg[i].st + 1);
        FOR(j, 1, cnt) d1[i][j] = 1LL * d1[i][j - 1] * (seg[i].nd - seg[i].st + 2 - j) % M * inv[j] % M;
    }
    FOR(i, 1, m)
    FOR(j, 0, n)
    FOR(z, 0, 1)
        d[i][j][z] = d1[i][j];
    FOR(i, 1, n)
    FOR(j, 1, m)
    FOR(z, 0, n - i)
    FOR(stat, 0, 1) {
        if (!stat) d[j][z][stat] = 0;
        if (a[i].st <= seg[j].st && seg[j].nd <= a[i].nd) d[j][z][stat] = (d[j][z][stat] + d[j][z + 1][1]) % M;
        if (j > 1) d[j][z][stat] = (d[j][z][stat] + 1LL * d1[j][z] * d[j - 1][0][0] % M) % M;
    }
    cout << (d[m][0][1] - 1 + M) % M;
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:60:5: note: in expansion of macro 'FOR'
   60 |     FOR(i, 1, n) cin >> a[i].st >> a[i].nd;
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:62:5: note: in expansion of macro 'FOR'
   62 |     FOR(i, 1, n) {
      |     ^~~
boat.cpp:34:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   34 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
boat.cpp:64:9: note: in expansion of macro 'EACH'
   64 |         EACH(j, seg) {
      |         ^~~~
boat.cpp:34:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   34 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
boat.cpp:80:9: note: in expansion of macro 'EACH'
   80 |         EACH(j, seg1) seg.pb(j);
      |         ^~~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:86:5: note: in expansion of macro 'FOR'
   86 |     FOR(i, 2, n) inv[i] = 1LL * (-M / i + M) % M * inv[M % i] % M;
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:87:5: note: in expansion of macro 'FOR'
   87 |     FOR(i, 1, m) {
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:90:9: note: in expansion of macro 'FOR'
   90 |         FOR(j, 1, cnt) d1[i][j] = 1LL * d1[i][j - 1] * (seg[i].nd - seg[i].st + 2 - j) % M * inv[j] % M;
      |         ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:92:5: note: in expansion of macro 'FOR'
   92 |     FOR(i, 1, m)
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:93:5: note: in expansion of macro 'FOR'
   93 |     FOR(j, 0, n)
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:94:5: note: in expansion of macro 'FOR'
   94 |     FOR(z, 0, 1)
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:96:5: note: in expansion of macro 'FOR'
   96 |     FOR(i, 1, n)
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:97:5: note: in expansion of macro 'FOR'
   97 |     FOR(j, 1, m)
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:98:5: note: in expansion of macro 'FOR'
   98 |     FOR(z, 0, n - i)
      |     ^~~
boat.cpp:30:31: warning: unnecessary parentheses in declaration of 'stat' [-Wparentheses]
   30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
boat.cpp:99:5: note: in expansion of macro 'FOR'
   99 |     FOR(stat, 0, 1) {
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 1008 ms 6232 KB Output is correct
2 Correct 1012 ms 6236 KB Output is correct
3 Correct 1020 ms 6232 KB Output is correct
4 Correct 1020 ms 6348 KB Output is correct
5 Correct 987 ms 6232 KB Output is correct
6 Correct 994 ms 6236 KB Output is correct
7 Correct 997 ms 6236 KB Output is correct
8 Correct 981 ms 6348 KB Output is correct
9 Correct 987 ms 6236 KB Output is correct
10 Correct 984 ms 6232 KB Output is correct
11 Correct 993 ms 6240 KB Output is correct
12 Correct 1026 ms 6232 KB Output is correct
13 Correct 1002 ms 6236 KB Output is correct
14 Correct 1012 ms 6236 KB Output is correct
15 Correct 968 ms 6232 KB Output is correct
16 Correct 179 ms 1380 KB Output is correct
17 Correct 188 ms 1456 KB Output is correct
18 Correct 179 ms 1412 KB Output is correct
19 Correct 186 ms 1492 KB Output is correct
20 Correct 180 ms 1424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1008 ms 6232 KB Output is correct
2 Correct 1012 ms 6236 KB Output is correct
3 Correct 1020 ms 6232 KB Output is correct
4 Correct 1020 ms 6348 KB Output is correct
5 Correct 987 ms 6232 KB Output is correct
6 Correct 994 ms 6236 KB Output is correct
7 Correct 997 ms 6236 KB Output is correct
8 Correct 981 ms 6348 KB Output is correct
9 Correct 987 ms 6236 KB Output is correct
10 Correct 984 ms 6232 KB Output is correct
11 Correct 993 ms 6240 KB Output is correct
12 Correct 1026 ms 6232 KB Output is correct
13 Correct 1002 ms 6236 KB Output is correct
14 Correct 1012 ms 6236 KB Output is correct
15 Correct 968 ms 6232 KB Output is correct
16 Correct 179 ms 1380 KB Output is correct
17 Correct 188 ms 1456 KB Output is correct
18 Correct 179 ms 1412 KB Output is correct
19 Correct 186 ms 1492 KB Output is correct
20 Correct 180 ms 1424 KB Output is correct
21 Correct 1014 ms 5720 KB Output is correct
22 Correct 1014 ms 5780 KB Output is correct
23 Correct 988 ms 5708 KB Output is correct
24 Correct 990 ms 5652 KB Output is correct
25 Correct 1016 ms 5676 KB Output is correct
26 Correct 1040 ms 5528 KB Output is correct
27 Correct 1041 ms 5616 KB Output is correct
28 Correct 1018 ms 5580 KB Output is correct
29 Correct 1016 ms 5496 KB Output is correct
30 Correct 981 ms 6228 KB Output is correct
31 Correct 979 ms 6348 KB Output is correct
32 Correct 993 ms 6228 KB Output is correct
33 Correct 984 ms 6248 KB Output is correct
34 Correct 972 ms 6212 KB Output is correct
35 Correct 959 ms 6228 KB Output is correct
36 Correct 967 ms 6228 KB Output is correct
37 Correct 977 ms 6228 KB Output is correct
38 Correct 992 ms 6228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1492 KB Output is correct
2 Correct 11 ms 1524 KB Output is correct
3 Correct 10 ms 1524 KB Output is correct
4 Correct 12 ms 1532 KB Output is correct
5 Correct 11 ms 1492 KB Output is correct
6 Correct 11 ms 1520 KB Output is correct
7 Correct 12 ms 1524 KB Output is correct
8 Correct 11 ms 1528 KB Output is correct
9 Correct 11 ms 1528 KB Output is correct
10 Correct 11 ms 1492 KB Output is correct
11 Correct 11 ms 1492 KB Output is correct
12 Correct 10 ms 1492 KB Output is correct
13 Correct 11 ms 1524 KB Output is correct
14 Correct 11 ms 1532 KB Output is correct
15 Correct 10 ms 1492 KB Output is correct
16 Correct 7 ms 1032 KB Output is correct
17 Correct 6 ms 980 KB Output is correct
18 Correct 6 ms 1020 KB Output is correct
19 Correct 6 ms 980 KB Output is correct
20 Correct 6 ms 904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1008 ms 6232 KB Output is correct
2 Correct 1012 ms 6236 KB Output is correct
3 Correct 1020 ms 6232 KB Output is correct
4 Correct 1020 ms 6348 KB Output is correct
5 Correct 987 ms 6232 KB Output is correct
6 Correct 994 ms 6236 KB Output is correct
7 Correct 997 ms 6236 KB Output is correct
8 Correct 981 ms 6348 KB Output is correct
9 Correct 987 ms 6236 KB Output is correct
10 Correct 984 ms 6232 KB Output is correct
11 Correct 993 ms 6240 KB Output is correct
12 Correct 1026 ms 6232 KB Output is correct
13 Correct 1002 ms 6236 KB Output is correct
14 Correct 1012 ms 6236 KB Output is correct
15 Correct 968 ms 6232 KB Output is correct
16 Correct 179 ms 1380 KB Output is correct
17 Correct 188 ms 1456 KB Output is correct
18 Correct 179 ms 1412 KB Output is correct
19 Correct 186 ms 1492 KB Output is correct
20 Correct 180 ms 1424 KB Output is correct
21 Correct 1014 ms 5720 KB Output is correct
22 Correct 1014 ms 5780 KB Output is correct
23 Correct 988 ms 5708 KB Output is correct
24 Correct 990 ms 5652 KB Output is correct
25 Correct 1016 ms 5676 KB Output is correct
26 Correct 1040 ms 5528 KB Output is correct
27 Correct 1041 ms 5616 KB Output is correct
28 Correct 1018 ms 5580 KB Output is correct
29 Correct 1016 ms 5496 KB Output is correct
30 Correct 981 ms 6228 KB Output is correct
31 Correct 979 ms 6348 KB Output is correct
32 Correct 993 ms 6228 KB Output is correct
33 Correct 984 ms 6248 KB Output is correct
34 Correct 972 ms 6212 KB Output is correct
35 Correct 959 ms 6228 KB Output is correct
36 Correct 967 ms 6228 KB Output is correct
37 Correct 977 ms 6228 KB Output is correct
38 Correct 992 ms 6228 KB Output is correct
39 Correct 11 ms 1492 KB Output is correct
40 Correct 11 ms 1524 KB Output is correct
41 Correct 10 ms 1524 KB Output is correct
42 Correct 12 ms 1532 KB Output is correct
43 Correct 11 ms 1492 KB Output is correct
44 Correct 11 ms 1520 KB Output is correct
45 Correct 12 ms 1524 KB Output is correct
46 Correct 11 ms 1528 KB Output is correct
47 Correct 11 ms 1528 KB Output is correct
48 Correct 11 ms 1492 KB Output is correct
49 Correct 11 ms 1492 KB Output is correct
50 Correct 10 ms 1492 KB Output is correct
51 Correct 11 ms 1524 KB Output is correct
52 Correct 11 ms 1532 KB Output is correct
53 Correct 10 ms 1492 KB Output is correct
54 Correct 7 ms 1032 KB Output is correct
55 Correct 6 ms 980 KB Output is correct
56 Correct 6 ms 1020 KB Output is correct
57 Correct 6 ms 980 KB Output is correct
58 Correct 6 ms 904 KB Output is correct
59 Correct 1123 ms 6252 KB Output is correct
60 Correct 1135 ms 6236 KB Output is correct
61 Correct 1096 ms 6228 KB Output is correct
62 Correct 1152 ms 6244 KB Output is correct
63 Correct 1135 ms 6240 KB Output is correct
64 Correct 1204 ms 6248 KB Output is correct
65 Correct 1246 ms 6240 KB Output is correct
66 Correct 1194 ms 6232 KB Output is correct
67 Correct 1214 ms 6236 KB Output is correct
68 Correct 1167 ms 6236 KB Output is correct
69 Correct 1132 ms 6232 KB Output is correct
70 Correct 1123 ms 6260 KB Output is correct
71 Correct 1130 ms 6236 KB Output is correct
72 Correct 1129 ms 6236 KB Output is correct
73 Correct 1121 ms 6240 KB Output is correct
74 Correct 201 ms 1404 KB Output is correct
75 Correct 211 ms 1444 KB Output is correct
76 Correct 221 ms 1420 KB Output is correct
77 Correct 212 ms 1420 KB Output is correct
78 Correct 201 ms 1484 KB Output is correct