답안 #680350

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680350 2023-01-10T16:03:42 Z peijar Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
55 ms 6304 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int i = 0; i < (int)vec.size(); ++i) {
    out << vec[i];
    if (i + 1 < (int)vec.size())
      out << ", ";
  }
  return out << "]";
}
} // namespace std

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << H;
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

template <const int32_t MOD> struct ModInt {
  int32_t x;
  ModInt() : x(0) {}
  ModInt(long long u) : x(u % MOD) {
    if (x < 0)
      x += MOD;
  }
  friend bool operator==(const ModInt &a, const ModInt &b) {
    return a.x == b.x;
  }
  friend bool operator!=(const ModInt &a, const ModInt &b) {
    return a.x != b.x;
  }
  friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
  friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
  friend bool operator<=(const ModInt &a, const ModInt &b) {
    return a.x <= b.x;
  }
  friend bool operator>=(const ModInt &a, const ModInt &b) {
    return a.x >= b.x;
  }
  static ModInt sign(long long k) {
    return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
  }

  ModInt &operator+=(const ModInt &m) {
    x += m.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &m) {
    x -= m.x;
    if (x < 0LL)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &m) {
    x = (1LL * x * m.x) % MOD;
    return *this;
  }

  friend ModInt operator-(const ModInt &a) {
    ModInt res(a);
    if (res.x)
      res.x = MOD - res.x;
    return res;
  }
  friend ModInt operator+(const ModInt &a, const ModInt &b) {
    return ModInt(a) += ModInt(b);
  }
  friend ModInt operator-(const ModInt &a, const ModInt &b) {
    return ModInt(a) -= ModInt(b);
  }
  friend ModInt operator*(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b);
  }

  static long long fp(long long u, long long k) {
    long long res = 1LL;
    while (k > 0LL) {
      if (k & 1LL)
        res = (res * u) % MOD;
      u = (u * u) % MOD;
      k /= 2LL;
    }
    return res;
  }

  static constexpr int mod() { return MOD; }

  ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
  ModInt inv() {
    assert(x);
    return ModInt(fp(x, MOD - 2));
  }
  ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
  friend ModInt operator/(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b).inv();
  }

  friend ostream &operator<<(ostream &out, const ModInt &a) {
    return out << a.x;
  }
  friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};

const int MOD = 1e9 + 7;
using Mint = ModInt<MOD>;

Mint choose2(Mint x) { return x * (x + 1) / 2; }

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> h(N), w(N);
  vector<int> prv(N), nxt(N);
  for (int &x : h)
    cin >> x;
  for (int &x : w)
    cin >> x;
  vector<int> prefW(N + 1);
  for (int i = 0; i < N; ++i)
    prefW[i + 1] = prefW[i] + w[i];
  for (int i = 0; i < N; ++i) {
    prv[i] = i - 1;
    while (prv[i] >= 0 and h[prv[i]] > h[i])
      prv[i] = prv[prv[i]];
  }
  for (int i = N - 1; i >= 0; --i) {
    nxt[i] = i + 1;
    while (nxt[i] < N and h[nxt[i]] >= h[i])
      nxt[i] = nxt[nxt[i]];
  }

  Mint sol = 0;
  for (int i = 0; i < N; ++i) {
    int L = prv[i] + 1;
    int R = nxt[i];
    // [L, R)
    Mint cntL = prefW[i] - prefW[L];
    Mint cntR = prefW[R] - prefW[i + 1];
    sol += choose2(h[i]) * ((cntL + 1) * (cntR + 1) +
                            (w[i] - 1) * (cntL + cntR + 2) + choose2(w[i] - 2));
  }
  cout << sol.x % MOD << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 23 ms 2788 KB Output is correct
4 Correct 49 ms 5504 KB Output is correct
5 Correct 45 ms 5384 KB Output is correct
6 Correct 45 ms 5408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 6 ms 968 KB Output is correct
3 Correct 25 ms 3248 KB Output is correct
4 Correct 51 ms 6176 KB Output is correct
5 Correct 52 ms 6272 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 5 ms 852 KB Output is correct
4 Correct 25 ms 3156 KB Output is correct
5 Correct 52 ms 6192 KB Output is correct
6 Correct 55 ms 6304 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 6 ms 852 KB Output is correct
9 Correct 25 ms 3176 KB Output is correct
10 Correct 48 ms 6020 KB Output is correct
11 Correct 49 ms 6172 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 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 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 23 ms 2768 KB Output is correct
12 Correct 45 ms 5332 KB Output is correct
13 Correct 45 ms 5408 KB Output is correct
14 Correct 46 ms 5368 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 6 ms 852 KB Output is correct
17 Correct 25 ms 3152 KB Output is correct
18 Correct 51 ms 6224 KB Output is correct
19 Correct 53 ms 6288 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 5 ms 832 KB Output is correct
22 Correct 26 ms 3268 KB Output is correct
23 Correct 49 ms 6016 KB Output is correct
24 Correct 49 ms 6144 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 6 ms 852 KB Output is correct
31 Correct 5 ms 872 KB Output is correct
32 Correct 25 ms 3156 KB Output is correct
33 Correct 26 ms 3168 KB Output is correct
34 Correct 49 ms 5888 KB Output is correct
35 Correct 50 ms 6088 KB Output is correct
36 Correct 51 ms 6180 KB Output is correct
37 Correct 51 ms 6192 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 50 ms 6144 KB Output is correct
40 Correct 50 ms 6156 KB Output is correct
41 Correct 50 ms 6196 KB Output is correct
42 Correct 50 ms 6192 KB Output is correct