This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int mxn = 1e5 + 9;
const int mod = 1e9 + 7;
int a[mxn], b[mxn], p[mxn], n;
vector<pii> q;
set<int> s;
int mult(int a, int b) {
a %= mod;
b %= mod;
if(a < b) swap(a, b);
if(b == 0) return 0;
if(b == 1) return a;
int t = mult(a, b / 2);
if(b % 2 == 0) return (t + t) % mod;
return ((t + t) % mod + a) % mod;
}
int C2(int a) {
if(a & 1) return mult(a, (a + 1) / 2);
return mult(a / 2, a + 1);
}
signed main() {
cin >> n;
for(int i = 2; i <= n + 1; i++) {
cin >> a[i];
q.push_back(MP(a[i], i));
}
for(int i = 2; i <= n + 1; i++) {
cin >> b[i];
p[i] = p[i - 1] + b[i];
}
int ans = 0;
for(int i = 2; i <= n + 1; i++) {
int d = mult(C2(a[i]), C2(b[i]));
ans = (ans + d) % mod;
}
q.push_back(MP(0, 1));
q.push_back(MP(0, n + 2));
sort(q.begin(), q.end());
int t = 0;
for(pii p : q) {
int H = p.ff;
int r = p.ss;
while(t < q.size() && q[t].ff < H) {
s.insert(-q[t].ss);
t++;
}
auto it = s.upper_bound(-r);
if(it == s.end()) continue;
int l = -(*it);
int W = ::p[r - 1] - ::p[l];
int d = mult(mult(W, b[r]), C2(H));
// cout << l << ' ' << r << ": " << d << endl;
ans = (ans + d) % mod;
}
s.clear();
t = 0;
for(pii p : q) {
int H = p.ff;
int l = p.ss;
while(t < q.size() && q[t].ff <= H) {
s.insert(q[t].ss);
t++;
}
auto it = s.upper_bound(l);
if(it == s.end()) continue;
int r = (*it);
int W = ::p[r - 1] - ::p[l];
// cout << l << ' ' << r << endl;
int d = mult(mult(W, b[l]), C2(H));
ans = (ans + d) % mod;
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:57:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while(t < q.size() && q[t].ff < H) {
| ~~^~~~~~~~~~
fancyfence.cpp:75:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while(t < q.size() && q[t].ff <= H) {
| ~~^~~~~~~~~~
# | 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... |