#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;
sz[b]%=mod;
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:60:15: warning: unused variable 'cnt' [-Wunused-variable]
60 | int cur=0,cnt=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
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 |
2648 KB |
Output is correct |
4 |
Correct |
0 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 |
23 ms |
5996 KB |
Output is correct |
4 |
Correct |
44 ms |
7280 KB |
Output is correct |
5 |
Correct |
45 ms |
7220 KB |
Output is correct |
6 |
Correct |
53 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
6 ms |
2908 KB |
Output is correct |
3 |
Correct |
29 ms |
6384 KB |
Output is correct |
4 |
Correct |
57 ms |
8020 KB |
Output is correct |
5 |
Correct |
59 ms |
8276 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
6 ms |
2712 KB |
Output is correct |
4 |
Correct |
29 ms |
6344 KB |
Output is correct |
5 |
Correct |
61 ms |
8000 KB |
Output is correct |
6 |
Correct |
59 ms |
8104 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
2904 KB |
Output is correct |
9 |
Correct |
29 ms |
6492 KB |
Output is correct |
10 |
Correct |
55 ms |
8016 KB |
Output is correct |
11 |
Correct |
58 ms |
8016 KB |
Output is correct |
12 |
Correct |
0 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 |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 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 |
2396 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 |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2500 KB |
Output is correct |
16 |
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 |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 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 |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
23 ms |
5976 KB |
Output is correct |
12 |
Correct |
46 ms |
7260 KB |
Output is correct |
13 |
Correct |
45 ms |
7248 KB |
Output is correct |
14 |
Correct |
43 ms |
7252 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
29 ms |
6236 KB |
Output is correct |
18 |
Correct |
58 ms |
8000 KB |
Output is correct |
19 |
Correct |
59 ms |
8272 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
3156 KB |
Output is correct |
22 |
Correct |
30 ms |
6452 KB |
Output is correct |
23 |
Correct |
56 ms |
8020 KB |
Output is correct |
24 |
Correct |
56 ms |
8012 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 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 |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
7 ms |
2856 KB |
Output is correct |
31 |
Correct |
7 ms |
2936 KB |
Output is correct |
32 |
Correct |
31 ms |
6236 KB |
Output is correct |
33 |
Correct |
33 ms |
6236 KB |
Output is correct |
34 |
Correct |
62 ms |
8020 KB |
Output is correct |
35 |
Correct |
61 ms |
8016 KB |
Output is correct |
36 |
Correct |
68 ms |
8012 KB |
Output is correct |
37 |
Correct |
75 ms |
8320 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
66 ms |
8172 KB |
Output is correct |
40 |
Correct |
66 ms |
8304 KB |
Output is correct |
41 |
Correct |
68 ms |
8212 KB |
Output is correct |
42 |
Correct |
61 ms |
8528 KB |
Output is correct |