Submission #1240260

#TimeUsernameProblemLanguageResultExecution timeMemory
1240260thewizardmanFancy Fence (CEOI20_fancyfence)C++20
100 / 100
25 ms4032 KiB
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define plpll pair<ll, pll>
#define pipii pair<int, pii>
#define plpii pair<ll, pii>
#define pipll pair<int, pll>
#define lll tuple<ll, ll, ll>
#define iii tuple<int, int, int>
#define lii tuple<ll, int, int>
#define lli tuple<ll, ll, int>
#define md 1000000007LL
#define linf 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define lninf (ll)0xc0c0c0c0c0c0c0c0
#define ninf (int)0xc0c0c0c0
#define bruh ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ss << ' ' <<
#ifdef wizard
  #define usaco(x)
#else
  #define usaco(x) ifstream cin(#x ".in"); ofstream cout(#x ".out");
#endif

template<class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
  return in >> p.first >> p.second;
}

template<class T1, class T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
  return out << p.first << ' ' << p.second;
}

template<size_t I = 0, typename... Ts>
typename enable_if<I == sizeof...(Ts), istream&>::type
read_tuple(istream& in, tuple<Ts...>& t) { return in; }

template<size_t I = 0, typename... Ts>
typename enable_if<I < sizeof...(Ts), istream&>::type
read_tuple(istream& in, tuple<Ts...>& t) {
  in >> get<I>(t);
  return read_tuple<I + 1>(in, t);
}

template<typename... Ts>
istream& operator>>(istream& in, tuple<Ts...>& t) {
  return read_tuple(in, t);
}

template<size_t I = 0, typename... Ts>
typename enable_if<I == sizeof...(Ts), void>::type
write_tuple(ostream& out, const tuple<Ts...>&) {}

template<size_t I = 0, typename... Ts>
typename enable_if<I < sizeof...(Ts), void>::type
write_tuple(ostream& out, const tuple<Ts...>& t) {
  if (I) out << ' ';
  out << get<I>(t);
  write_tuple<I + 1>(out, t);
}

template<typename... Ts>
ostream& operator<<(ostream& out, const tuple<Ts...>& t) {
  write_tuple(out, t);
  return out;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& v) {
  for (auto& x : v) in >> x;
  return in;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  for (size_t i = 0; i < v.size(); ++i)
    out << (i ? " " : "") << v[i];
  return out;
}

int n;
ll h[100000], w[100000], ans;
vector<pll> v;
ll sum;

inline ll calc1(ll h, ll w1, ll w2) {
  return (((h*(h+1)/2)%md) * ((w1*w2)%md)) % md;
}

inline ll calc2(ll h, ll w) {
  return (((h*(h+1)/2)%md) * ((w*(w+1)/2)%md)) % md;
}

inline ll calc3(ll h, ll w) {
  return ((h*(h+1)/2)%md * w)%md;
}

int main() {
  bruh;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> h[i];
  for (int i = 0; i < n; i++) cin >> w[i];
  for (int i = 0; i < n; i++) {
    ll ww = 0;
    while (v.size() && v.back().first >= h[i]) {
      ww = (ww + v.back().second) % md;
      sum = (sum - calc3(v.back().first, v.back().second) + md) % md;
      v.pop_back();
    }
    ans = (ans + calc1(h[i], ww, w[i]) + calc2(h[i], w[i]) + sum * w[i]) % md;
    // for (auto [h1, w1]: v) ans = (ans + ((w[i])*(w1)%md)*(h1*(h1+1)/2)%md) % md;
    v.push_back({h[i], w[i] + ww});
    sum = (sum + calc3(h[i], w[i] + ww)) % md;
    // cout << sum << '\n';
  }
  cout << ans;
}
#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...