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 <stdio.h>
#define N 500000
#define N_ (1 << 19) /* N_ = pow2(ceil(log2(N))) */
#define MD 1000000007
unsigned int Z = 12345;
int rand_() {
return (Z *= 3) >> 1;
}
int pp2[N + 1];
void init() {
int i;
pp2[0] = 1;
for (i = 1; i <= N; i++)
pp2[i] = pp2[i - 1] * 2 % MD;
}
int yy[N * 2];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (yy[ii[j]] == yy[i_])
j++;
else if (yy[ii[j]] < yy[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int sz[N_ * 2], prod[N_ * 2], pref[N_ * 2], suff[N_ * 2], n_;
void pul(int i) {
int l = i << 1, r = l | 1;
prod[i] = (long long) prod[l] * prod[r] % MD;
pref[i] = ((long long) pref[l] * pp2[sz[r]] + (long long) prod[l] * pref[r]) % MD;
suff[i] = ((long long) suff[l] * prod[r] + (long long) pp2[sz[l]] * suff[r]) % MD;
}
void build(int n) {
int i;
n_ = 1;
while (n_ < n)
n_ <<= 1;
for (i = 0; i < n_; i++)
if (i < n)
sz[n_ + i] = 1;
else
prod[n_ + i] = 1;
for (i = n_ - 1; i > 0; i--)
sz[i] = sz[i << 1 | 0] + sz[i << 1 | 1], pul(i);
}
void update(int i) {
i += n_;
pref[i] = suff[i] = ++prod[i];
while (i > 1)
pul(i >>= 1);
}
int main() {
static int ii[N * 2];
int n, h, i, x, sum, ans;
init();
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &yy[i << 1 | 0]);
for (i = 0; i < n; i++)
scanf("%d", &yy[i << 1 | 1]);
for (i = 0; i < n * 2; i++)
ii[i] = i;
sort(ii, 0, n * 2);
build(n);
ans = 0;
for (h = 0; h < n * 2; h++) {
x = ((long long) pp2[n] * n - pref[1] - suff[1] + (long long) prod[1] * n + MD * 2) % MD;
ans = (ans + (long long) x * (yy[ii[h]] - (h == 0 ? 0 : yy[ii[h - 1]]))) % MD;
update(ii[h] >> 1);
}
sum = 0;
for (i = 0; i < n * 2; i++)
sum = (sum + yy[i]) % MD;
ans = (ans - (long long) sum * pp2[n - 1] % MD + MD) % MD;
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:81:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:83:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d", &yy[i << 1 | 0]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:85:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d", &yy[i << 1 | 1]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |