#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+37;
int p[N], vis[N], length[N];
int sum, ans=0, mod=1e9+7;
int s(int x, int y){
return ((x+y)%mod+mod)%mod;
}
int mul(int x, int y){
return (x*y)%mod;
}
int poww(int x, int y){
if(y==0) return 1;
int tmp=poww(x, y/2);
if(y&1) return mul(tmp, mul(tmp, x));
return mul(tmp, tmp);
}
int val(int x){
return mul(mul(x, (x+1)), poww(2, mod-2));
}
int find(int x){
return (p[x]!=x)?p[x]=find(p[x]):x;
}
void dsu(int x, int y){
x=find(x);
y=find(y);
if(x==y) return;
sum=s(sum, -val(length[x]));
sum=s(sum, -val(length[y]));
p[y]=x;
length[x]=s(length[x], length[y]);
sum=s(sum, val(length[x]));
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
vector<array<int, 3>> a(n+1);
for(int i=1; i<=n; i++) p[i]=i;
for(int i=0; i<n; i++){
cin>>a[i][0];
a[i][2]=i+1;
}
for(int i=0; i<n; i++){
cin>>a[i][1];
length[i+1]=a[i][1];
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
for(int i=0; i<a.size()-1; i++)
{
int p=a[i][2], len=a[i][1], h=a[i][0];
sum=s(sum, val(length[p])); vis[p]=1;
if(vis[p-1]&&vis[p+1]){
dsu(p, p-1);
dsu(p, p+1);
}
else if(vis[p-1]){
dsu(p, p-1);
}
else if(vis[p+1]){
dsu(p, p+1);
}
ans=s(ans, mul(sum, s(val(h), -val(a[i+1][0]))));
}
cout<<ans;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:71:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i=0; i<a.size()-1; i++)
| ~^~~~~~~~~~~
fancyfence.cpp:73:24: warning: unused variable 'len' [-Wunused-variable]
73 | int p=a[i][2], len=a[i][1], h=a[i][0];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
124 ms |
3152 KB |
Output is correct |
4 |
Correct |
243 ms |
6132 KB |
Output is correct |
5 |
Correct |
250 ms |
6048 KB |
Output is correct |
6 |
Correct |
254 ms |
6160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
27 ms |
980 KB |
Output is correct |
3 |
Correct |
129 ms |
3564 KB |
Output is correct |
4 |
Correct |
272 ms |
6840 KB |
Output is correct |
5 |
Correct |
245 ms |
7052 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
26 ms |
904 KB |
Output is correct |
4 |
Correct |
127 ms |
3668 KB |
Output is correct |
5 |
Correct |
278 ms |
6912 KB |
Output is correct |
6 |
Correct |
268 ms |
7048 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
33 ms |
972 KB |
Output is correct |
9 |
Correct |
148 ms |
3672 KB |
Output is correct |
10 |
Correct |
271 ms |
6768 KB |
Output is correct |
11 |
Correct |
265 ms |
6884 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
388 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
136 ms |
3136 KB |
Output is correct |
12 |
Correct |
264 ms |
6180 KB |
Output is correct |
13 |
Correct |
277 ms |
6092 KB |
Output is correct |
14 |
Correct |
261 ms |
6096 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
24 ms |
980 KB |
Output is correct |
17 |
Correct |
128 ms |
3528 KB |
Output is correct |
18 |
Correct |
240 ms |
6956 KB |
Output is correct |
19 |
Correct |
245 ms |
7036 KB |
Output is correct |
20 |
Correct |
3 ms |
344 KB |
Output is correct |
21 |
Correct |
25 ms |
972 KB |
Output is correct |
22 |
Correct |
124 ms |
3580 KB |
Output is correct |
23 |
Correct |
242 ms |
6828 KB |
Output is correct |
24 |
Correct |
243 ms |
6892 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
412 KB |
Output is correct |
28 |
Correct |
3 ms |
412 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
24 ms |
988 KB |
Output is correct |
31 |
Correct |
25 ms |
976 KB |
Output is correct |
32 |
Correct |
119 ms |
3568 KB |
Output is correct |
33 |
Correct |
121 ms |
3668 KB |
Output is correct |
34 |
Correct |
244 ms |
6756 KB |
Output is correct |
35 |
Correct |
242 ms |
6732 KB |
Output is correct |
36 |
Correct |
241 ms |
6908 KB |
Output is correct |
37 |
Correct |
245 ms |
6912 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
241 ms |
6904 KB |
Output is correct |
40 |
Correct |
251 ms |
7040 KB |
Output is correct |
41 |
Correct |
236 ms |
6948 KB |
Output is correct |
42 |
Correct |
239 ms |
7508 KB |
Output is correct |