Submission #757808

# Submission time Handle Problem Language Result Execution time Memory
757808 2023-06-13T18:47:44 Z taher Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
76 ms 6468 KB
#include <bits/stdc++.h>
 
using namespace std;

constexpr int md = 1000000007;

struct Mint {
  int val = 0;  
  Mint() : val{} {}
  template<typename T> Mint(T x) {
    if (x >= -md && x < md) {
      val = x;
    } else {
      val = x % md;
    }
    if (val < 0) {
      val += md;
    }
  }
  int& operator()() { return val; }
  Mint& operator+=(Mint x) {
    if ((val += x.val) >= md) {
      val -= md;
    }
    return *this;
  }
  Mint& operator-=(Mint x) {
    if ((val -= x.val) < 0) {
      val += md;
    }
    return *this;
  }
  Mint& operator*=(Mint x) {
    val = int((1LL * val * x.val) % md);      
    return *this;
  }
};
template<typename T> Mint power(Mint x, T p) {
  Mint res = 1;
  while (p > 0) {
    if (p & 1) {
      res *= x;
    }
    x *= x;
    p >>= 1;
  }
  return res;
}
Mint& operator/=(Mint& x, Mint y) {
  return x *= power(y, md - 2);
}
 
Mint operator+(Mint x, Mint y) {
  return x += y;
}
Mint operator-(Mint x, Mint y) {
  return x -= y;
}
Mint operator*(Mint x, Mint y) {
  return x *= y;
}
Mint operator/(Mint x, Mint y) {
  return x /= y;
}
string to_string(Mint x) {
  return to_string(x());
}
ostream& operator<<(ostream& out, Mint x) {
  return out << x();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i].second;
  }
  // {n, m}
  vector<long long> pref(n);
  for (int i = 0; i < n; i++) {
    if (i > 0) pref[i] = pref[i - 1];
    pref[i] += a[i].second;
  }
  vector<long long> ans(n), rmv(n);
  auto Left = [&]() {
    vector<pair<int, int>> stk;
    for (int i = 0; i < n; i++) {
      while (!stk.empty() && stk.back().first > a[i].first) {
        stk.pop_back();
      }
      int new_idx = (stk.empty() ? -1 : stk.back().second);
      while (!stk.empty() && stk.back().first == a[i].first) {
        stk.pop_back();
      }
      int idx = (stk.empty() ? -1 : stk.back().second);
      stk.emplace_back(a[i].first, i);
      ans[i] += pref[i] - (idx >= 0 ? pref[idx] : 0);
      rmv[i] = max(rmv[i], (new_idx >= 0 ? a[new_idx].first : 0LL));
    }
  };
  auto Right = [&]() {
    vector<pair<int, int>> stk;
    for (int i = n - 1; i >= 0; i--) {
      while (!stk.empty() && stk.back().first >= a[i].first) {
        stk.pop_back();
      }
      int idx = (stk.empty() ? n : stk.back().second);
      stk.emplace_back(a[i].first, i);
      ans[i] += pref[idx - 1] - pref[i];
      rmv[i] = max(rmv[i], (idx < n ? a[idx].first : 0LL));
    }
  };
  Left();
  Right();
  Mint res = 0;
  for (int i = 0; i < n; i++) {
    Mint N = a[i].first;
    Mint M = ans[i];
    res += (N * (N + 1)) / 2 * (M * (M + 1)) / 2;
    res -= (rmv[i] * (rmv[i] + 1)) / 2 * (M * (M + 1)) / 2;
  }
  cout << res << '\n';
  return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 34 ms 2468 KB Output is correct
4 Correct 66 ms 4564 KB Output is correct
5 Correct 65 ms 4552 KB Output is correct
6 Correct 66 ms 4616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 9 ms 724 KB Output is correct
3 Correct 36 ms 2840 KB Output is correct
4 Correct 72 ms 5400 KB Output is correct
5 Correct 75 ms 5500 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 7 ms 832 KB Output is correct
4 Correct 38 ms 2856 KB Output is correct
5 Correct 75 ms 5320 KB Output is correct
6 Correct 75 ms 5500 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 8 ms 980 KB Output is correct
9 Correct 43 ms 3264 KB Output is correct
10 Correct 73 ms 6348 KB Output is correct
11 Correct 69 ms 6468 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 428 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 33 ms 2396 KB Output is correct
12 Correct 66 ms 4616 KB Output is correct
13 Correct 66 ms 4564 KB Output is correct
14 Correct 64 ms 4616 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 8 ms 724 KB Output is correct
17 Correct 35 ms 2752 KB Output is correct
18 Correct 70 ms 5320 KB Output is correct
19 Correct 74 ms 5440 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 8 ms 972 KB Output is correct
22 Correct 37 ms 3264 KB Output is correct
23 Correct 74 ms 6348 KB Output is correct
24 Correct 72 ms 6452 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 9 ms 712 KB Output is correct
31 Correct 8 ms 724 KB Output is correct
32 Correct 36 ms 2756 KB Output is correct
33 Correct 39 ms 2852 KB Output is correct
34 Correct 72 ms 5204 KB Output is correct
35 Correct 69 ms 5196 KB Output is correct
36 Correct 71 ms 5344 KB Output is correct
37 Correct 73 ms 5332 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 75 ms 5396 KB Output is correct
40 Correct 76 ms 5432 KB Output is correct
41 Correct 69 ms 5532 KB Output is correct
42 Correct 73 ms 6220 KB Output is correct