#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
struct node{
int left=0;
int right=0;
int ans=0;
int sum=0;
};
node zero={0, 0, 0};
struct segtree{
vector<node> tree;
segtree(int n){
tree.resize(n*4);
};
node merge(node a, node b){
node ret;
ret.sum=(a.sum+b.sum)%mod;
ret.ans=(a.ans+b.ans)%mod;
int middle=(a.right+b.left)%mod;
if(a.left==a.sum){
middle=0;
ret.left=(a.sum+b.left)%mod;
}
if(b.right==b.sum){
middle=0;
ret.right=(a.right+b.sum)%mod;
}
ret.ans+=(middle*(middle+1)/2)%mod;
ret.ans%=mod;
return ret;
}
node update(int l, int r, int v, int x, int val){
if(x<l || r<=x) return tree[v];
if(l+1==r){
tree[v].left=val;
tree[v].right=val;
tree[v].sum=val;
tree[v].ans=0;
return tree[v];
}
int m=l+(r-l)/2;
return tree[v]=merge(update(l, m, v*2+1, x, val), update(m, r, v*2+2, x, val));
}
node query(int l, int r, int v, int ql, int qr){
if(qr<=l || r<=ql) return zero;
if(ql<=l && r<=qr) return tree[v];
int m=l+(r-l)/2;
return merge(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> h(n);
map<int,int> ind; int x=1;
vector<pair<int, vector<int> > >sorted;
sorted.push_back({0, {}});
for(int i=0; i<n; i++){
cin >> h[i];
if(ind[h[i]]==0){
sorted.push_back({h[i], {}});
ind[h[i]]=x;
x++;
}
sorted[ind[h[i]]].second.push_back(i);
}
sort(sorted.begin(), sorted.end());
reverse(sorted.begin(), sorted.end());
vector<int> w(n);
for(int i=0; i<n; i++){
cin >> w[i];
}
int ans=0;
segtree seg(n);
for(int i=0; i<(int)sorted.size()-1; i++){
int h=sorted[i].first;
int next=sorted[i+1].first;
vector<int> ele=sorted[i].second;
for(auto e: ele){
seg.update(0, n, 0, e, w[e]);
}
node res=seg.query(0, n, 0, 0, n);
//cout << res.ans << " " << res.left << " " << res.right << "\n";
int s=res.ans;
s+= (res.left*(res.left+1)/2)%mod;
s%=mod;
if(res.right!=res.sum){
s+=(res.right*(res.right+1)/2)%mod;
s%=mod;
}
int factor=((h*(h+1))/2 - (next*(next+1))/2)%mod;
//cout << factor << " " << s << "\n";
ans+=(s*factor)%mod;
ans%=mod;
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
1900 KB |
Output is correct |
3 |
Correct |
41 ms |
8168 KB |
Output is correct |
4 |
Correct |
85 ms |
16120 KB |
Output is correct |
5 |
Correct |
81 ms |
16028 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
8 ms |
1796 KB |
Output is correct |
4 |
Correct |
39 ms |
8276 KB |
Output is correct |
5 |
Correct |
80 ms |
16080 KB |
Output is correct |
6 |
Correct |
78 ms |
16104 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
11 ms |
2896 KB |
Output is correct |
9 |
Correct |
50 ms |
10304 KB |
Output is correct |
10 |
Correct |
126 ms |
28764 KB |
Output is correct |
11 |
Correct |
129 ms |
28980 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |