#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
typedef long long ll;
const int lim = 1e5 + 5;
const int mod = 1e9 + 7;
void add(int& a, int b){
if((a += b) >= mod){
a -= mod;
}
}
int f(ll N){
return ((N & 1LL) ? N % mod * (((N + 1) >> 1LL) % mod) : (N >> 1LL) % mod * ((N + 1) % mod)) % mod;
}
int f(ll l, ll r){
return (f(r) - f(l - 1) + mod) % mod;
}
int n, h[lim], w[lim];
namespace sub3{
void solve(){
int ans = f(accumulate(w + 1, w + n + 1, 0LL));
ll N = 0;
for(int i = 1; i <= n; i++){
if(h[i] == 2){
N += w[i];
}
else{
add(ans, (f(N) << 1) % mod);
N = 0;
}
}
cout << (ans + ((f(N) << 1) % mod)) % mod;
}
}
namespace sub4{
void solve(){
cout << 1LL * f(accumulate(w + 1, w + n + 1, 0LL)) * f(h[1]) % mod;
}
}
namespace sub2567{
ll len[lim];
int par[lim];
int find_set(int N){
return N == par[N] ? N : par[N] = find_set(par[N]);
}
void merge(int a, int b){
if(len[a = find_set(a)] != 0 && len[b = find_set(b)] != 0){
len[par[b] = a] += len[b];
}
}
void solve(){
memset(len, 0, sizeof(len));
vector<int>p(n);
iota(p.begin(), p.end(), 1);
sort(p.begin(), p.end(), [&] (int i, int j){
return h[i] > h[j];
});
int ans = 0;
iota(par, par + n + 2, 0);
for(int& i : p){
int left = len[find_set(i - 1)] % mod, right = len[find_set(i + 1)] % mod;
add(ans, 1LL * left * (len[i] = w[i]) % mod * f(h[i]) % mod);
add(ans, 1LL * right * w[i] % mod * f(h[i]) % mod);
add(ans, 1LL * left * right % mod * f(h[i]) % mod);
add(ans, 1LL * f(w[i]) * f(h[i]) % mod);
merge(i, i + 1);
merge(i, i - 1);
}
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
for(int i = 1; i <= n; i++){
cin >> h[i];
}
for(int i = 1; i <= n; i++){
cin >> w[i];
}
if(*max_element(h + 1, h + n + 1) < 3){
sub3::solve();
}
else if(*min_element(h + 1, h + n + 1) == *max_element(h + 1, h + n + 1)){
sub4::solve();
}
else{
sub2567::solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |