제출 #650260

#제출 시각아이디문제언어결과실행 시간메모리
650260alvinpiterFancy Fence (CEOI20_fancyfence)C++14
100 / 100
94 ms4880 KiB
#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define MOD 1000000007
#define MAXN 100000

int n;
LL h[MAXN + 3], w[MAXN + 3], sumWidth[MAXN + 3], sumFhw = 0, ans = 0;
stack<int> st;

LL f(LL k) {
  LL k1 = k + 1;
  return k%2 == 0 ? ((k/2)%MOD * (k1)%MOD)%MOD : (k%MOD * (k1/2)%MOD)%MOD;
}

int solveBruteForce() {
  int psum[55][55];
  memset(psum, 0, sizeof(psum));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= h[i]; j++) {
      psum[i][j] = 1;
    }
    cout << endl;
  }

  for (int i = 1; i <= 50; i++) {
    for (int j = 1; j <= 50; j++) {
      psum[i][j] += psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1];
    }
  }

  int ans = 0;
  for (int i1 = 1; i1 <= 50; i1++) {
    for (int j1 = 1; j1 <= 50; j1++) {
      for (int i2 = i1; i2 <= 50; i2++) {
        for (int j2 = j1; j2 <= 50; j2++) {
          int sum = psum[i2][j2] - psum[i2][j1 - 1] - psum[i1 - 1][j2] + psum[i1 - 1][j1 - 1];
          if (sum == (i2 - i1 + 1) * (j2 - j1 + 1)) {
            ans += 1;
          }
        }
      }
    }
  }

  return ans;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }

  sumWidth[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
    sumWidth[i] = sumWidth[i - 1] + w[i];
  }

  // cout << solveBruteForce() << endl;
  // return 0;

  for (int i = 1; i <= n; i++) {
    LL rectanglesOnTheCurrentFence = (f(h[i]) * f(w[i]))%MOD;
    ans = (ans + rectanglesOnTheCurrentFence)%MOD;

    while (!st.empty() && h[st.top()] >= h[i]) {
      int top = st.top(); st.pop();

      if (st.empty()) {
        sumFhw = 0;
      } else {
        LL totalWidth = sumWidth[top] - sumWidth[st.top()];
        LL fhw = (f(h[top]) * (totalWidth%MOD))%MOD;
        sumFhw = (sumFhw - fhw + MOD)%MOD;
      }
    }

    int firstLowerIdx = (st.empty() ? 0 : st.top());

    LL rectanglesOnTheCurrentFenceAndTallerNeighbors = 0;
    if (firstLowerIdx != i - 1) {
      LL waysToPickWidth = ((sumWidth[i - 1] - sumWidth[firstLowerIdx])%MOD * w[i])%MOD;
      rectanglesOnTheCurrentFenceAndTallerNeighbors = (waysToPickWidth * f(h[i]))%MOD;
      ans = (ans + rectanglesOnTheCurrentFenceAndTallerNeighbors)%MOD;
    }

    LL rectanglesOnTheCurrentFenceAndLowerNeighbors = (sumFhw * w[i])%MOD;
    ans = (ans + rectanglesOnTheCurrentFenceAndLowerNeighbors)%MOD;

    LL totalWidth = sumWidth[i] - (st.empty() ? 0 : sumWidth[st.top()]);
    sumFhw = (sumFhw + (f(h[i]) * (totalWidth%MOD))%MOD)%MOD;

    st.push(i);

    // cout << "\ni: " << i << endl;
    // cout << "rectanglesOnTheCurrentFence: " << rectanglesOnTheCurrentFence << endl;
    // cout << "rectanglesOnTheCurrentFenceAndTallerNeighbors: " << rectanglesOnTheCurrentFenceAndTallerNeighbors << endl;
    // cout << "rectanglesOnTheCurrentFenceAndLowerNeighbors: " << rectanglesOnTheCurrentFenceAndLowerNeighbors << endl;
    // cout << "sumFhw: " << sumFhw << endl;
  }

  cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...