#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 100007
#define MOD 1000000007
tuple<int, int, int> hw[MAX];
int p[MAX];
int sumsz[MAX];
int storeSqrSum = 0;
int compute(int x){
return ( (x * (x+1))/2 ) %MOD;
}
void activate(int a){
sumsz[a] = get<1>(hw[a]);
storeSqrSum += compute(get<1>(hw[a]));
storeSqrSum %= MOD;
}
int root(int a){
if (a == p[a]) return a;
return p[a] = root(p[a]);
}
void merge(int a, int b){
a = root(a); b = root(b);
if (a == b) return;
p[a] = b;
storeSqrSum -= compute(sumsz[a]);
storeSqrSum += MOD; storeSqrSum %= MOD;
storeSqrSum -= compute(sumsz[b]);
storeSqrSum += MOD; storeSqrSum %= MOD;
sumsz[b] += sumsz[a]; sumsz[b] %= MOD; //????????????????????
storeSqrSum += compute(sumsz[b]);
storeSqrSum %= MOD;
}
int rainbow(int start, int end){
if ( (end-start)%2 ){
//nice, no mid element
int res = start+end;
return (res * ((end-start+1)/2))%MOD;
}
else{
int res = start+end;
return ((res * ((end-start+1)/2))%MOD + (start+end)/2)%MOD;
}
}
main(){
int n; cin >> n;
set<int> heights;
for (int x = 0; x < n; x++) cin >> get<0>(hw[x]), heights.insert(get<0>(hw[x]));
for (int x = 0; x < n; x++) cin >> get<1>(hw[x]), get<2>(hw[x]) = x;
tuple<int, int, int> ohgod[n];
for (int x = 0; x < n; x++) ohgod[x] = hw[x];
sort(ohgod, ohgod+n);
reverse(ohgod, ohgod+n);
for (int x = 0; x < n; x++) p[x] = x, sumsz[x] = 0;
int ptr = 0;
int ans = 0;
heights.insert(0);
for (auto it = heights.rbegin(); it != heights.rend(); it++){
if (*it == 0) break;
while (ptr != n && get<0>(ohgod[ptr]) == *it){
int idx = get<2>(ohgod[ptr]);
activate( idx );
if (idx != 0 && sumsz[idx-1] != 0) merge(idx, idx-1);
if (idx != n-1 && sumsz[idx+1] != 0) merge(idx, idx+1);
ptr++;
}
int curh = *it, nexth = (*next(it));
ans += (storeSqrSum * rainbow(nexth+1, curh))%MOD;
ans %= MOD;
//cerr << curh << ' ' << nexth << ' ' << storeSqrSum << ' ' << (storeSqrSum * rainbow(nexth+1, curh))%MOD << '\n';
}
cout << ans;
}
Compilation message
fancyfence.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
27 ms |
4860 KB |
Output is correct |
4 |
Correct |
54 ms |
7740 KB |
Output is correct |
5 |
Correct |
58 ms |
7840 KB |
Output is correct |
6 |
Correct |
52 ms |
7824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
2908 KB |
Output is correct |
3 |
Correct |
34 ms |
5276 KB |
Output is correct |
4 |
Correct |
71 ms |
8532 KB |
Output is correct |
5 |
Correct |
74 ms |
8496 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
7 ms |
2908 KB |
Output is correct |
4 |
Correct |
33 ms |
5232 KB |
Output is correct |
5 |
Correct |
68 ms |
8556 KB |
Output is correct |
6 |
Correct |
79 ms |
8492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
8 ms |
3408 KB |
Output is correct |
9 |
Correct |
39 ms |
6324 KB |
Output is correct |
10 |
Correct |
76 ms |
13140 KB |
Output is correct |
11 |
Correct |
76 ms |
13064 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2460 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2496 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
26 ms |
4692 KB |
Output is correct |
12 |
Correct |
52 ms |
7764 KB |
Output is correct |
13 |
Correct |
52 ms |
7608 KB |
Output is correct |
14 |
Correct |
51 ms |
7608 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
7 ms |
2764 KB |
Output is correct |
17 |
Correct |
40 ms |
5212 KB |
Output is correct |
18 |
Correct |
67 ms |
8452 KB |
Output is correct |
19 |
Correct |
69 ms |
8620 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
8 ms |
3352 KB |
Output is correct |
22 |
Correct |
38 ms |
6232 KB |
Output is correct |
23 |
Correct |
87 ms |
13132 KB |
Output is correct |
24 |
Correct |
77 ms |
13196 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
9 ms |
3208 KB |
Output is correct |
31 |
Correct |
13 ms |
3420 KB |
Output is correct |
32 |
Correct |
35 ms |
5204 KB |
Output is correct |
33 |
Correct |
45 ms |
7640 KB |
Output is correct |
34 |
Correct |
89 ms |
12888 KB |
Output is correct |
35 |
Correct |
67 ms |
8232 KB |
Output is correct |
36 |
Correct |
103 ms |
13304 KB |
Output is correct |
37 |
Correct |
89 ms |
11328 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
95 ms |
10556 KB |
Output is correct |
40 |
Correct |
91 ms |
13056 KB |
Output is correct |
41 |
Correct |
76 ms |
13188 KB |
Output is correct |
42 |
Correct |
80 ms |
13200 KB |
Output is correct |