#include <bits/stdc++.h>
using namespace std;
const long long MAXN=1e5, MOD=1e9+7;
long long h[MAXN], w[MAXN];
bool compare(long long x, long long y)
{
if (h[x]==h[y])
return x<y;
return h[x]<h[y];
}
long long outer(long long i, long long leftsum, long long rightsum)
{
long long rectangles=0;
rectangles=(rectangles+h[i]*w[i]%MOD*leftsum%MOD)%MOD;
rectangles=(rectangles+h[i]*(h[i]-1)/2%MOD*w[i]%MOD*leftsum%MOD)%MOD;
rectangles=(rectangles+h[i]*w[i]%MOD*rightsum%MOD)%MOD;
rectangles=(rectangles+h[i]*(h[i]-1)/2%MOD*w[i]%MOD*rightsum%MOD)%MOD;
rectangles=(rectangles+h[i]*leftsum%MOD*rightsum%MOD)%MOD;
rectangles=(rectangles+h[i]*(h[i]-1)/2%MOD*leftsum%MOD*rightsum%MOD)%MOD;
return rectangles;
}
long long inner(long long i)
{
long long rectangles=0;
rectangles=(rectangles+h[i]*w[i]%MOD)%MOD;
rectangles=(rectangles+w[i]*(w[i]-1)/2%MOD*h[i]%MOD)%MOD;
rectangles=(rectangles+h[i]*(h[i]-1)/2%MOD*w[i]%MOD)%MOD;
rectangles=(rectangles+h[i]*(h[i]-1)/2%MOD*(w[i]*(w[i]-1)/2%MOD)%MOD)%MOD;
return rectangles;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, x, left, right, i;
long long leftsum, rightsum, total=0;
set<long long> processed;
cin >> n;
long long index[n];
long long prefixsum[n];
for (i=0; i<n; i++)
cin >> h[i];
for (i=0; i<n; i++)
cin >> w[i];
for (i=0; i<n; i++)
index[i]=i;
sort(index, index+n, compare);
/*cout << "sorted by height ";
for (i=0; i<n; i++)
cout << index[i] << " ";
cout << '\n';*/
prefixsum[0]=w[0];
for (i=1; i<n; i++)
prefixsum[i]=prefixsum[i-1]+w[i];
/*cout << "prefixsum of widths ";
for (i=0; i<n; i++)
cout << prefixsum[i] << " ";
cout << '\n';*/
processed.insert(-1);
processed.insert(n);
/*cout << "processed " << '\n';
for (auto item : processed)
cout << item << " ";
cout << '\n';*/
for (i=0; i<n; i++)
{
x=index[i];
//for (int j=0; j<n; j++)
// cout << index[j] << " ";
//cout << '\n';
//cout << "i x " << i << " " << x << '\n';
auto it=processed.lower_bound(x);
right=*it;
it--;
left=*it;
//cout << "left right " << left << " " << right << '\n';
rightsum=(prefixsum[right-1]-prefixsum[x])%MOD;
leftsum=(prefixsum[x]-w[x]-(prefixsum[left+1]-w[left+1]))%MOD;
//cout << "lsum rsum " << leftsum << " " << rightsum << '\n';
//cout << "inner outer " << inner(x) << " " << outer(x, leftsum, rightsum) << '\n';
total=(total+outer(x, leftsum, rightsum)+inner(x))%MOD;
processed.insert(x);
}
//cout << "ANSWER ";
cout << total;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
26 ms |
5060 KB |
Output is correct |
4 |
Correct |
45 ms |
9364 KB |
Output is correct |
5 |
Correct |
45 ms |
9300 KB |
Output is correct |
6 |
Correct |
41 ms |
9384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
1372 KB |
Output is correct |
3 |
Correct |
23 ms |
5152 KB |
Output is correct |
4 |
Correct |
46 ms |
10068 KB |
Output is correct |
5 |
Correct |
53 ms |
10136 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
484 KB |
Output is correct |
3 |
Correct |
5 ms |
1372 KB |
Output is correct |
4 |
Correct |
23 ms |
5204 KB |
Output is correct |
5 |
Correct |
46 ms |
10064 KB |
Output is correct |
6 |
Correct |
47 ms |
10068 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
22 ms |
5208 KB |
Output is correct |
10 |
Correct |
55 ms |
10048 KB |
Output is correct |
11 |
Correct |
47 ms |
10140 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
476 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
22 ms |
4820 KB |
Output is correct |
12 |
Correct |
45 ms |
9404 KB |
Output is correct |
13 |
Correct |
42 ms |
9296 KB |
Output is correct |
14 |
Correct |
42 ms |
9296 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
1372 KB |
Output is correct |
17 |
Correct |
23 ms |
5112 KB |
Output is correct |
18 |
Correct |
48 ms |
10072 KB |
Output is correct |
19 |
Correct |
48 ms |
10072 KB |
Output is correct |
20 |
Correct |
1 ms |
480 KB |
Output is correct |
21 |
Correct |
4 ms |
1372 KB |
Output is correct |
22 |
Correct |
23 ms |
5136 KB |
Output is correct |
23 |
Correct |
46 ms |
10072 KB |
Output is correct |
24 |
Correct |
46 ms |
9960 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
1372 KB |
Output is correct |
31 |
Correct |
6 ms |
1372 KB |
Output is correct |
32 |
Correct |
26 ms |
5196 KB |
Output is correct |
33 |
Correct |
28 ms |
5296 KB |
Output is correct |
34 |
Correct |
58 ms |
9912 KB |
Output is correct |
35 |
Correct |
54 ms |
10004 KB |
Output is correct |
36 |
Correct |
61 ms |
10068 KB |
Output is correct |
37 |
Correct |
60 ms |
10004 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
58 ms |
10172 KB |
Output is correct |
40 |
Correct |
54 ms |
10064 KB |
Output is correct |
41 |
Correct |
56 ms |
10028 KB |
Output is correct |
42 |
Correct |
44 ms |
10192 KB |
Output is correct |