답안 #468382

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468382 2021-08-27T22:41:03 Z thomas_li Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
107 ms 4952 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
template <class T, class U> T pow2(T base, U pow) {
  T x = 1; while (true) {
    if (pow % 2 == 1) x = x * base;
    if ((pow /= 2) == 0) break;
    base = base * base;
  }
  return x;
}
template <class T> constexpr bool isPrime(T x) {
  if (x < 2) return false;
  for (T i = 2; i * i <= x; i++) if (x % i == 0) return false;
  return true;
}
template <class T, const T MOD,
#if __cplusplus < 201402L
          const bool PRIME_MOD,
#else
          const bool PRIME_MOD = isPrime(MOD),
#endif
          const bool MUL_OVERFLOW = (numeric_limits<T>::max() / MOD < MOD)>
    struct IntMod {
  static_assert(is_integral<T>::value, "T must be an integral type");
  static_assert(is_signed<T>::value, "T must be a signed type");
  static_assert(0 < MOD, "MOD must be a positive integer");
  using IM = IntMod<T, MOD, PRIME_MOD, MUL_OVERFLOW>;
  T v; IntMod() : v(0) {}
  IntMod(const T &x) {
    v = -MOD < x && x < MOD ? x : x % MOD; if (v < 0) v += MOD;
  }
  bool operator < (const IM &i) const { return v < i.v; }
  bool operator <= (const IM &i) const { return v <= i.v; }
  bool operator > (const IM &i) const { return v > i.v; }
  bool operator >= (const IM &i) const { return v >= i.v; }
  bool operator == (const IM &i) const { return v == i.v; }
  bool operator != (const IM &i) const { return v != i.v; }
  IM operator ++ () {
    if (++v == MOD) v = 0;
    return *this;
  }
  IM operator ++ (int) {
    IM ret = *this; if (++v == MOD) v = 0;
    return ret;
  }
  IM operator -- () {
    if (v-- == 0) v = MOD - 1;
    return *this;
  }
  IM operator -- (int) {
    IM ret = *this; if (v-- == 0) v = MOD - 1; return ret;
  }
  IM operator + (const IM &i) const { return IM(*this) += i; }
  IM &operator += (const IM &i) {
    if ((v += i.v) >= MOD) v -= MOD;
    return *this;
  }
  IM operator - (const IM &i) const { return IM(*this) -= i; }
  IM &operator -= (const IM &i) {
    if ((v -= i.v) < 0) v += MOD;
    return *this;
  }
  IM operator + () const { return *this; }
  IM operator - () const { return IM(-v); }
  IM operator * (const IM &i) const { return IM(*this) *= i; }
  IM &operator *= (const IM &i) {
    if (MUL_OVERFLOW) {
      IM x = 0, y = *this; T z = i.v;
      for (; z > 0; z /= 2, y += y) if (z % 2 == 1) x += y;
      *this = x;
    } else v = v * i.v % MOD;
    return *this;
  }
  bool hasMulInv() const {
    if (v == 0) return false;
    if (!PRIME_MOD || MUL_OVERFLOW) {
      T g = MOD, r = v; while (r != 0) { g %= r; swap(g, r); }
      return g == 1;
    } else return true;
  }
  IM mulInv() const {
    if (!PRIME_MOD || MUL_OVERFLOW) {
      T g = MOD, r = v, x = 0, y = 1; while (r != 0) {
        T q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y);
      }
      assert(g == 1); return IM(x);
    } else return pow2(*this, MOD - 2);
  }
  IM operator / (const IM &i) const { return IM(*this) /= i; }
  IM &operator /= (const IM &i) { return *this *= i.mulInv(); }
  friend istream &operator >> (istream &stream, IM &i) {
    T v; stream >> v; i = IM(v); return stream;
  }
  friend ostream &operator << (ostream &stream, const IM &i) {
    return stream << i.v;
  }
};
const int MM = 1e5+5,MOD = 1e9+7,inv2 = 500000004;
typedef IntMod<ll,MOD> IM;

int n,h[MM],w[MM]; IM psa[MM],dp[MM];
IM c2(int x){
    IM res(x);
    return res*(res+1)/2;
}
signed main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n; 
    for(int i = 1; i <= n; i++) cin >> h[i];
    for(int i = 1; i <= n; i++) cin >> w[i],psa[i] = psa[i-1] + w[i];
    stack<int> st; st.push(0);
    IM ans(0);
    for(int i = 1; i <= n; i++){
        while(st.size() && h[st.top()] >= h[i]) st.pop(); 
        int j = st.top();
        dp[i] = dp[j];     
        dp[i] += (psa[i] - psa[j])*c2(h[i]);
        ans += dp[j]*w[i] + c2(w[i])*c2(h[i]);
        ans += (psa[i-1] - psa[j])*w[i]*c2(h[i]);
        st.push(i);
    }
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1868 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 44 ms 2844 KB Output is correct
4 Correct 91 ms 3808 KB Output is correct
5 Correct 89 ms 3804 KB Output is correct
6 Correct 86 ms 3788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 10 ms 2152 KB Output is correct
3 Correct 46 ms 3232 KB Output is correct
4 Correct 93 ms 4492 KB Output is correct
5 Correct 107 ms 4676 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 10 ms 2156 KB Output is correct
4 Correct 46 ms 3144 KB Output is correct
5 Correct 91 ms 4716 KB Output is correct
6 Correct 99 ms 4684 KB Output is correct
7 Correct 2 ms 1868 KB Output is correct
8 Correct 10 ms 2184 KB Output is correct
9 Correct 48 ms 3324 KB Output is correct
10 Correct 90 ms 4860 KB Output is correct
11 Correct 90 ms 4932 KB Output is correct
12 Correct 1 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 1 ms 1868 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 2 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 2 ms 1868 KB Output is correct
9 Correct 2 ms 1868 KB Output is correct
10 Correct 2 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 2 ms 1868 KB Output is correct
13 Correct 2 ms 1868 KB Output is correct
14 Correct 2 ms 1868 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 1 ms 1868 KB Output is correct
4 Correct 2 ms 1792 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 1 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 2 ms 1868 KB Output is correct
11 Correct 44 ms 2844 KB Output is correct
12 Correct 86 ms 3788 KB Output is correct
13 Correct 86 ms 3740 KB Output is correct
14 Correct 88 ms 3812 KB Output is correct
15 Correct 3 ms 1868 KB Output is correct
16 Correct 12 ms 2152 KB Output is correct
17 Correct 46 ms 3236 KB Output is correct
18 Correct 90 ms 4544 KB Output is correct
19 Correct 95 ms 4568 KB Output is correct
20 Correct 2 ms 1912 KB Output is correct
21 Correct 10 ms 2124 KB Output is correct
22 Correct 47 ms 3284 KB Output is correct
23 Correct 89 ms 4804 KB Output is correct
24 Correct 100 ms 4952 KB Output is correct
25 Correct 1 ms 1888 KB Output is correct
26 Correct 2 ms 1868 KB Output is correct
27 Correct 2 ms 1868 KB Output is correct
28 Correct 2 ms 1896 KB Output is correct
29 Correct 2 ms 1868 KB Output is correct
30 Correct 10 ms 2152 KB Output is correct
31 Correct 11 ms 2124 KB Output is correct
32 Correct 46 ms 3148 KB Output is correct
33 Correct 47 ms 3180 KB Output is correct
34 Correct 90 ms 4300 KB Output is correct
35 Correct 102 ms 4368 KB Output is correct
36 Correct 94 ms 4528 KB Output is correct
37 Correct 107 ms 4544 KB Output is correct
38 Correct 1 ms 1868 KB Output is correct
39 Correct 100 ms 4480 KB Output is correct
40 Correct 100 ms 4532 KB Output is correct
41 Correct 92 ms 4552 KB Output is correct
42 Correct 92 ms 4848 KB Output is correct