#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
A not too simple solution:
Sort sections in decreasing order by height
Use DSU (why not?, it's a good practice) when merging intervals
Note that you don't need so many long long, i just wanted to be sure
Note that this is an O(NlogN) solution because of the sorting
Made for you by Mate Busa
*/
const long long mod = 1000000007;
long long N, ans;
long long h[100000];
long long w[100000];
vector<int> sections;
int parent[100000];
bool done[100000];
//calculate binom(x,2)
long long bin(long long x){
x %= mod;
return ((x*(x+1))/2)%mod;
}
//count fancy rectangles in a big rectangle of size x*y
long long rec(long long x, long long y){
return (bin(x)*bin(y))%mod;
}
//DSU
int find_set(int x){
if(parent[x]<0) return x;
return (parent[x] = find_set(parent[x]));
}
void union_sets(int x, int y, long long high){
x = find_set(x); y = find_set(y);
long long h = (rec(w[x],high)+rec(w[y],high))%mod;
h = (rec(w[x]+w[y],high)-h+mod)%mod; ans = (ans+h)%mod;
if(parent[x]<parent[y]){
parent[y] = x;
w[x] = (w[x]+w[y])%mod;
} else {
if(parent[x]==parent[y]) parent[y]--;
parent[x] = y;
w[y] = (w[x]+w[y])%mod;
}
}
bool smaller(int x, int y){
return h[x]>h[y];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N;
for(int i=0; i<N; i++){
cin >> h[i];
sections.push_back(i); parent[i] = -1;
}
for(int i=0; i<N; i++) cin >> w[i];
//sort sections by height
sort(sections.begin(),sections.end(),smaller);
for(int i=0; i<N; i++) ans = (ans+rec(w[i],h[i]))%mod;
for(int i:sections){
//merge intervals
if(i!=0 && done[i-1]) union_sets(i-1,i,h[i]);
if(i!=N-1 && done[i+1]) union_sets(i,i+1,h[i]);
done[i] = true;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
14 ms |
1664 KB |
Output is correct |
4 |
Correct |
29 ms |
2940 KB |
Output is correct |
5 |
Correct |
34 ms |
2928 KB |
Output is correct |
6 |
Correct |
29 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
17 ms |
1664 KB |
Output is correct |
4 |
Correct |
33 ms |
2940 KB |
Output is correct |
5 |
Correct |
35 ms |
2932 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
16 ms |
1664 KB |
Output is correct |
5 |
Correct |
37 ms |
2932 KB |
Output is correct |
6 |
Correct |
36 ms |
2928 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
640 KB |
Output is correct |
9 |
Correct |
17 ms |
1664 KB |
Output is correct |
10 |
Correct |
35 ms |
2940 KB |
Output is correct |
11 |
Correct |
33 ms |
2940 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
18 ms |
1664 KB |
Output is correct |
12 |
Correct |
29 ms |
2932 KB |
Output is correct |
13 |
Correct |
31 ms |
2940 KB |
Output is correct |
14 |
Correct |
28 ms |
2940 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
708 KB |
Output is correct |
17 |
Correct |
17 ms |
1664 KB |
Output is correct |
18 |
Correct |
33 ms |
2932 KB |
Output is correct |
19 |
Correct |
36 ms |
2916 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
17 ms |
1792 KB |
Output is correct |
23 |
Correct |
33 ms |
2940 KB |
Output is correct |
24 |
Correct |
34 ms |
2972 KB |
Output is correct |
25 |
Correct |
0 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
640 KB |
Output is correct |
32 |
Correct |
20 ms |
1664 KB |
Output is correct |
33 |
Correct |
22 ms |
1664 KB |
Output is correct |
34 |
Correct |
43 ms |
2940 KB |
Output is correct |
35 |
Correct |
40 ms |
2932 KB |
Output is correct |
36 |
Correct |
44 ms |
2932 KB |
Output is correct |
37 |
Correct |
44 ms |
2932 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
43 ms |
2932 KB |
Output is correct |
40 |
Correct |
40 ms |
2940 KB |
Output is correct |
41 |
Correct |
37 ms |
2928 KB |
Output is correct |
42 |
Correct |
35 ms |
2932 KB |
Output is correct |