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;
typedef long long ll;
const int N = 5e5 + 12, MOD = (int)1e9 + 7;
#define int ll
int n, a[N], b[N], m, ai[N], bi[N];
ll st[N];
void add(ll &x, ll y) {
y %= MOD;
x += y;
x %= MOD;
}
vector<ll> c;
void prec() {
for(int i = 1; i <= n; i++) {
c.push_back(a[i]);
c.push_back(b[i]);
}
sort(c.begin(), c.end());
c.resize(unique(c.begin(), c.end()) - c.begin());
m = (int)c.size();
for(int i = 1; i <= n; i++) {
ai[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin() + 1;
bi[i] = lower_bound(c.begin(), c.end(), b[i]) - c.begin() + 1;
}
st[0] = 1;
for(int i = 1; i <= n; i++) {
st[i] = st[i - 1] ;
add(st[i], st[i]);
}
}
void rev() {
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
reverse(ai + 1, ai + n + 1);
reverse(bi + 1, bi + n + 1);
}
ll res = 0;
void solve(bool fi = true) {
vector<ll> d(m + 1, 0);
d[0] = 1;
for(int i = 1; i <= n; i++) {
ll s = 0, _s = 0;
for(int j = 0; j < bi[i]; j++) {
add(_s, d[j]);
if(j < ai[i]) {
add(s, d[j]);
}
}
for(int j = 0; j < ai[i]; j++) {
d[j] = 0;
}
for(int j = bi[i]; j <= m; j++) {
add(d[j], d[j]);
}
add(d[ai[i]], s);
add(d[bi[i]], _s);
for(int j = 1; j <= m; j++) {
add(res, d[j] * c[j - 1] % MOD * st[n - i] % MOD);
}
}
if(fi) {
ll K = 0;
for(int i = 1; i <= m; i++) {
ll v = d[i] * c[i -1] % MOD * 1ll * n % MOD;
res -= v;
if(res < 0) {
res += MOD;
}
}
}
}
void test() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= n; i++) {
cin >> b[i];
if(a[i] > b[i]) {
swap(a[i], b[i]);
}
}
prec();
solve(1);
rev();
solve(0);
ll v = 1, sum= 0;
for(int i = 1; i <= n; i++) {
add(sum, a[i]);
add(sum, b[i]);
if(i < n) {
add(v, v);
}
}
v = v * sum % MOD;
res = (res - v + MOD) % MOD;
cout << res << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void solve(bool)':
Main.cpp:69:12: warning: unused variable 'K' [-Wunused-variable]
69 | ll K = 0;
| ^
# | 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... |