Submission #245328

# Submission time Handle Problem Language Result Execution time Memory
245328 2020-07-06T04:07:02 Z neihcr7j Boat (APIO16_boat) C++14
100 / 100
1004 ms 11128 KB
#include<bits/stdc++.h>

#define M 1000000007
#define maxn 502

using namespace std;
typedef long long ll;

inline ll norm(ll a) {
  return (a < 0 ? a + M : (a >= M ? a - M : a));
}

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

ll power(ll a, ll b) {
  if (b == 0) return 1;
  if (b&1) return mul(power(a, b - 1), a);
  ll t = power(a, b / 2);
  return mul(t, t);
}

ll gt[maxn * 2], igt[maxn * 2];

void precalc(){
  int n = 1000;

  gt[0] = 1;
  for (int i = 1; i <= n; ++i)
    gt[i] = mul(gt[i - 1], i);

  igt[n] = power(gt[n], M - 2);

  for (int i = n - 1; i >= 0; --i)
    igt[i] = mul(igt[i + 1], i + 1);
}

ll C(int n, int k) {
  return mul(mul(gt[n], igt[k]), igt[n - k]);
}


int n;

int l[maxn], r[maxn];
vector < pair < int, int > > seg;
vector < vector < int > > a;
vector < int > leng;

ll dp[maxn * 4][maxn], sumk[maxn * 4];

int main(){
  #define TASK "ABC"
 // freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
  ios_base::sync_with_stdio(0);

  precalc();

  cin >> n;

  vector < int > ret;

  for (int i = 1; i <= n; ++i) {
    cin >> l[i] >> r[i];

    ret.push_back(l[i]);
    ret.push_back(r[i]);
  }

  sort(ret.begin(), ret.end());

  seg.push_back({ret[0], ret[0]});

  for (int i = 1; i < ret.size(); ++i)
    if (ret[i] != ret[i - 1]) {
      if (ret[i - 1] + 1 != ret[i])
        seg.push_back({ret[i - 1] + 1, ret[i] - 1});
      seg.push_back({ret[i], ret[i]});
    }

  a = vector < vector < int > >(seg.size(), vector < int >(0, 0));
  leng = vector < int > (seg.size(), 0);

  for (int i = 0; i < seg.size(); ++i) {
    for (int j = 1; j <= n; ++j)
      if (l[j] <= seg[i].first && seg[i].second <= r[j])
        a[i].push_back(j);
    leng[i] = seg[i].second - seg[i].first + 1;
  }

  ll ans = 0;

  sumk[0] = 1;

  for (int i = 0; i < seg.size(); ++i) {
    ll l = leng[i];

    vector < ll > c(a[i].size() + 1, 0), val(a[i].size() + 1, 0);

    c[0] = 1;

    for (int k = 1; k <= a[i].size(); ++k) {
      if (k <= l)
        c[k] = mul(mul(c[k - 1], l - k + 1), power(k, M - 2));

      for (int x = k; x > 0; --x)
        if (x <= l)
          val[k] = norm(val[k] + mul(c[x], C(k - 1, x - 1)));
    }

     int index = 0;

    for (int k = 0; k <= n; ++k) {

      if (i) sumk[k] = norm(sumk[k] + dp[i - 1][k]);

      ll res = sumk[k];

      while (index < a[i].size() && a[i][index] <= k)
        index++;

      for (int j = index; j < a[i].size(); ++j)
        dp[i][a[i][j]] = norm(dp[i][a[i][j]] + mul(res, val[j - index + 1]));

      ans = norm(ans + dp[i][k]);
    }
  }

  cout << ans;

  return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:75:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 1; i < ret.size(); ++i)
                   ~~^~~~~~~~~~~~
boat.cpp:85:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < seg.size(); ++i) {
                   ~~^~~~~~~~~~~~
boat.cpp:96:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < seg.size(); ++i) {
                   ~~^~~~~~~~~~~~
boat.cpp:103:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int k = 1; k <= a[i].size(); ++k) {
                     ~~^~~~~~~~~~~~~~
boat.cpp:120:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       while (index < a[i].size() && a[i][index] <= k)
              ~~~~~~^~~~~~~~~~~~~
boat.cpp:123:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int j = index; j < a[i].size(); ++j)
                           ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2432 KB Output is correct
2 Correct 11 ms 2432 KB Output is correct
3 Correct 10 ms 2432 KB Output is correct
4 Correct 10 ms 2432 KB Output is correct
5 Correct 10 ms 2432 KB Output is correct
6 Correct 10 ms 2432 KB Output is correct
7 Correct 10 ms 2432 KB Output is correct
8 Correct 11 ms 2432 KB Output is correct
9 Correct 10 ms 2432 KB Output is correct
10 Correct 10 ms 2432 KB Output is correct
11 Correct 11 ms 2432 KB Output is correct
12 Correct 10 ms 2432 KB Output is correct
13 Correct 10 ms 2432 KB Output is correct
14 Correct 10 ms 2432 KB Output is correct
15 Correct 10 ms 2432 KB Output is correct
16 Correct 6 ms 896 KB Output is correct
17 Correct 6 ms 896 KB Output is correct
18 Correct 6 ms 1024 KB Output is correct
19 Correct 7 ms 1024 KB Output is correct
20 Correct 7 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2432 KB Output is correct
2 Correct 11 ms 2432 KB Output is correct
3 Correct 10 ms 2432 KB Output is correct
4 Correct 10 ms 2432 KB Output is correct
5 Correct 10 ms 2432 KB Output is correct
6 Correct 10 ms 2432 KB Output is correct
7 Correct 10 ms 2432 KB Output is correct
8 Correct 11 ms 2432 KB Output is correct
9 Correct 10 ms 2432 KB Output is correct
10 Correct 10 ms 2432 KB Output is correct
11 Correct 11 ms 2432 KB Output is correct
12 Correct 10 ms 2432 KB Output is correct
13 Correct 10 ms 2432 KB Output is correct
14 Correct 10 ms 2432 KB Output is correct
15 Correct 10 ms 2432 KB Output is correct
16 Correct 6 ms 896 KB Output is correct
17 Correct 6 ms 896 KB Output is correct
18 Correct 6 ms 1024 KB Output is correct
19 Correct 7 ms 1024 KB Output is correct
20 Correct 7 ms 896 KB Output is correct
21 Correct 366 ms 8492 KB Output is correct
22 Correct 352 ms 8568 KB Output is correct
23 Correct 331 ms 8440 KB Output is correct
24 Correct 357 ms 8568 KB Output is correct
25 Correct 349 ms 8312 KB Output is correct
26 Correct 484 ms 8696 KB Output is correct
27 Correct 493 ms 8824 KB Output is correct
28 Correct 488 ms 8824 KB Output is correct
29 Correct 491 ms 8864 KB Output is correct
30 Correct 20 ms 7808 KB Output is correct
31 Correct 20 ms 7680 KB Output is correct
32 Correct 19 ms 7680 KB Output is correct
33 Correct 19 ms 7808 KB Output is correct
34 Correct 21 ms 7808 KB Output is correct
35 Correct 13 ms 4352 KB Output is correct
36 Correct 13 ms 4352 KB Output is correct
37 Correct 14 ms 4352 KB Output is correct
38 Correct 13 ms 4352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2048 KB Output is correct
2 Correct 11 ms 2048 KB Output is correct
3 Correct 12 ms 2048 KB Output is correct
4 Correct 12 ms 2048 KB Output is correct
5 Correct 13 ms 2048 KB Output is correct
6 Correct 16 ms 2048 KB Output is correct
7 Correct 17 ms 2048 KB Output is correct
8 Correct 16 ms 2048 KB Output is correct
9 Correct 17 ms 2048 KB Output is correct
10 Correct 16 ms 2048 KB Output is correct
11 Correct 13 ms 2048 KB Output is correct
12 Correct 11 ms 2048 KB Output is correct
13 Correct 12 ms 2048 KB Output is correct
14 Correct 12 ms 2048 KB Output is correct
15 Correct 13 ms 2048 KB Output is correct
16 Correct 8 ms 1024 KB Output is correct
17 Correct 8 ms 1024 KB Output is correct
18 Correct 8 ms 1024 KB Output is correct
19 Correct 8 ms 1024 KB Output is correct
20 Correct 9 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2432 KB Output is correct
2 Correct 11 ms 2432 KB Output is correct
3 Correct 10 ms 2432 KB Output is correct
4 Correct 10 ms 2432 KB Output is correct
5 Correct 10 ms 2432 KB Output is correct
6 Correct 10 ms 2432 KB Output is correct
7 Correct 10 ms 2432 KB Output is correct
8 Correct 11 ms 2432 KB Output is correct
9 Correct 10 ms 2432 KB Output is correct
10 Correct 10 ms 2432 KB Output is correct
11 Correct 11 ms 2432 KB Output is correct
12 Correct 10 ms 2432 KB Output is correct
13 Correct 10 ms 2432 KB Output is correct
14 Correct 10 ms 2432 KB Output is correct
15 Correct 10 ms 2432 KB Output is correct
16 Correct 6 ms 896 KB Output is correct
17 Correct 6 ms 896 KB Output is correct
18 Correct 6 ms 1024 KB Output is correct
19 Correct 7 ms 1024 KB Output is correct
20 Correct 7 ms 896 KB Output is correct
21 Correct 366 ms 8492 KB Output is correct
22 Correct 352 ms 8568 KB Output is correct
23 Correct 331 ms 8440 KB Output is correct
24 Correct 357 ms 8568 KB Output is correct
25 Correct 349 ms 8312 KB Output is correct
26 Correct 484 ms 8696 KB Output is correct
27 Correct 493 ms 8824 KB Output is correct
28 Correct 488 ms 8824 KB Output is correct
29 Correct 491 ms 8864 KB Output is correct
30 Correct 20 ms 7808 KB Output is correct
31 Correct 20 ms 7680 KB Output is correct
32 Correct 19 ms 7680 KB Output is correct
33 Correct 19 ms 7808 KB Output is correct
34 Correct 21 ms 7808 KB Output is correct
35 Correct 13 ms 4352 KB Output is correct
36 Correct 13 ms 4352 KB Output is correct
37 Correct 14 ms 4352 KB Output is correct
38 Correct 13 ms 4352 KB Output is correct
39 Correct 12 ms 2048 KB Output is correct
40 Correct 11 ms 2048 KB Output is correct
41 Correct 12 ms 2048 KB Output is correct
42 Correct 12 ms 2048 KB Output is correct
43 Correct 13 ms 2048 KB Output is correct
44 Correct 16 ms 2048 KB Output is correct
45 Correct 17 ms 2048 KB Output is correct
46 Correct 16 ms 2048 KB Output is correct
47 Correct 17 ms 2048 KB Output is correct
48 Correct 16 ms 2048 KB Output is correct
49 Correct 13 ms 2048 KB Output is correct
50 Correct 11 ms 2048 KB Output is correct
51 Correct 12 ms 2048 KB Output is correct
52 Correct 12 ms 2048 KB Output is correct
53 Correct 13 ms 2048 KB Output is correct
54 Correct 8 ms 1024 KB Output is correct
55 Correct 8 ms 1024 KB Output is correct
56 Correct 8 ms 1024 KB Output is correct
57 Correct 8 ms 1024 KB Output is correct
58 Correct 9 ms 1152 KB Output is correct
59 Correct 623 ms 10116 KB Output is correct
60 Correct 566 ms 9980 KB Output is correct
61 Correct 533 ms 9976 KB Output is correct
62 Correct 611 ms 10232 KB Output is correct
63 Correct 596 ms 9980 KB Output is correct
64 Correct 1004 ms 11064 KB Output is correct
65 Correct 1002 ms 11076 KB Output is correct
66 Correct 997 ms 10976 KB Output is correct
67 Correct 995 ms 10912 KB Output is correct
68 Correct 991 ms 11128 KB Output is correct
69 Correct 480 ms 9848 KB Output is correct
70 Correct 484 ms 10024 KB Output is correct
71 Correct 476 ms 9976 KB Output is correct
72 Correct 499 ms 9976 KB Output is correct
73 Correct 504 ms 9976 KB Output is correct
74 Correct 66 ms 1400 KB Output is correct
75 Correct 61 ms 1348 KB Output is correct
76 Correct 64 ms 1400 KB Output is correct
77 Correct 67 ms 1400 KB Output is correct
78 Correct 66 ms 1400 KB Output is correct