#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 in[mxn+10];
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){
a=find(a),b=find(b);
if(a==b)return;
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;
pa[n]=n;
for(int i=0;i<n;i++)cin>>in[i];
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(v[cur].s,v[cur].s+1,in[v[cur].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:61:15: warning: unused variable 'cnt' [-Wunused-variable]
61 | int cur=0,cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |