#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define Bit(x, i) ((x >> (i)) & 1)
#define Mask(i) (1 << (i))
#define Cnt(x) __builtin_popcount(x)
#define Cntll(x) __builtin_popcountll(x)
#define Ctz(x) __builtin_ctz(x) // so luong so 0 tinh tu ben phai
#define Ctzll(x) __builtin_ctzll(x)
#define Clz(x) __builtin_clz(x) // so luong so 0 tinh tu ben trai
#define Clzll(x) __builtin_clzll(x)
inline bool maximize(int &u, int v){
return v > u ? u = v, true : false;
}
inline bool minimize(int &u, int v){
return v < u ? u = v, true : false;
}
inline bool maximizell(long long &u, long long v){
return v > u ? u = v, true : false;
}
inline bool minimizell(long long &u, long long v){
return v < u ? u = v, true : false;
}
const int mod = 1e9 + 7;
inline int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
inline void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 2e5 + 5;
const int inf = 1e9;
const long long infll = 1e18;
struct Node{
int pref, suff, sum, answer;
Node operator + (const Node &rhs) const{
Node res;
res.answer = answer + rhs.answer;
if(res.answer >= mod) res.answer -= mod;
add(res.answer, 1ll * suff * rhs.pref % mod);
res.pref = 0;
if(sum == pref) res.pref = sum + rhs.pref;
else res.pref = pref;
add(res.pref, 0);
res.suff = 0;
if(rhs.sum == rhs.suff) res.suff = rhs.sum + suff;
else res.suff = rhs.suff;
add(res.suff, 0);
res.sum = sum + rhs.sum;
add(res.sum, 0);
return res;
}
}it[maxN << 2];
int w[maxN], h[maxN];
void build(int id, int l, int r){
if(l == r){
it[id] = {w[l], w[l], w[l], 1ll * w[l] * (w[l] + 1) / 2 % mod};
return;
}
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
it[id] = it[id << 1] + it[id << 1 | 1];
}
void update(int id, int l, int r, int p){
if(l == r){
it[id] = {0, 0, w[l], 0};
return;
}
int mid = l + r >> 1;
if(p <= mid) update(id << 1, l, mid, p);
else update(id << 1 | 1, mid + 1, r, p);
it[id] = it[id << 1] + it[id << 1 | 1];
}
Node get(int id, int l, int r, int u, int v){
if(l >= u && r <= v) return it[id];
int mid = l + r >> 1;
if(v <= mid) return get(id << 1, l, mid, u, v);
if(v > mid) return get(id << 1 | 1, mid + 1, r, u, v);
Node L = get(id << 1, l, mid, u, v);
Node R = get(id << 1 | 1, mid + 1, r, u, v);
return L + R;
}
int n;
vector<int> compress;
vector<int> query[maxN];
void process(){
cin >> n;
FOR(i, 1, n){
cin >> h[i];
compress.emplace_back(h[i]);
}
FOR(i, 1, n){
cin >> w[i];
}
build(1, 1, n);
sort(all(compress));
uni(compress);
int pre = 0;
int answer = 0;
FOR(i, 1, n){
h[i] = lower_bound(all(compress), h[i]) - compress.begin();
query[h[i]].emplace_back(i);
}
FOR(i, 0, compress.size() - 1){
int valh = compress[i] - pre;
valh = (1ll * valh * pre + 1ll * valh * (valh + 1) / 2) % mod;
add(answer, 1ll * valh * it[1].answer % mod);
for(int p : query[i])update(1, 1, n, p);
pre = compress[i];
}
cout << answer;
}
#define LOVE "fancyfence"
int main(){
if(fopen(LOVE".inp", "r")){
freopen(LOVE".inp", "r", stdin);
freopen(LOVE".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
// cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
Compilation message (stderr)
fancyfence.cpp: In function 'void build(int, int, int)':
fancyfence.cpp:72:65: warning: narrowing conversion of '((((1 * ((long long int)w[l])) * ((long long int)(w[l] + 1))) / 2) % ((long long int)((int)mod)))' from 'long long int' to 'int' [-Wnarrowing]
72 | it[id] = {w[l], w[l], w[l], 1ll * w[l] * (w[l] + 1) / 2 % mod};
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(LOVE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(LOVE".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |