#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
const int mod=1e9+7,mxn=1e5+5,lg=30,inf=1e18,minf=-1e18;
pii pos[mxn+10];
int ans=0,pa[mxn+10],add=0,sz[mxn+10],fac[mxn+10],inv=500000004;
int find(int u){return (pa[u]==u)?u:pa[u]=find(pa[u]);}
int cal(int w){
return (((w*(w+1)%mod)*inv)%mod);
}
void merg(int a,int b){
int c=b-a;
a=find(a),b=find(b);
pa[a]=b;
add-=(cal(sz[b])+cal(sz[a]));
add=(add+mod)%mod;
sz[b]+=sz[a];
sz[b]+=c;
add+=cal(sz[b]);
add%=mod;
}
int32_t main(){
int n;cin>>n;
vector<pii>v(n);
for(int i=0;i<n;i++)cin>>v[i].f,v[i].s=i,pa[i]=i;
for(int i=0;i<n;i++){
int a;cin>>a;
if(i)pos[i]={pos[i-1].s,pos[i-1].s+a};
else pos[i]={1,a+1};
}
sort(rall(v));
int cur=0,cnt=0;
for(int i=0;i<n;i++){
while(cur<n&&v[cur].f==v[i].f){
merg(pos[v[cur].s].f,pos[v[cur].s].s);
cur++;
}
int ch=v[i].f,nh=((cur>=n)?0:v[cur].f);
int tmp=(((((((ch*(ch+1))%mod)*inv)%mod)-((((nh*(nh+1))%mod)*inv)%mod))+mod)%mod);
ans=(ans+(add*tmp)%mod)%mod;
i=cur-1;
}
cout<<ans;
}
Compilation message
fancyfence.cpp: In function 'int32_t main()':
fancyfence.cpp:63:15: warning: unused variable 'cnt' [-Wunused-variable]
63 | int cur=0,cnt=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Runtime error |
4 ms |
4700 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Runtime error |
3 ms |
4700 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |