#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using std::mt19937_64;
using std::random_device;
using std::uniform_int_distribution;
#include <numeric>
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("art2.in", "r", stdin);
// freopen("art2.out", "w", stdout);
// #endif
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int exp(int x, int n, int m) {
if(x == 0 and n == 0) return 1;
x %= m;
int res = 1;
while (n > 0) {
if (n % 2 == 1) { res = res * x % m; }
x = x * x % m;
n /= 2;
}
return res;
}
int modinv(int n) {
int r = 1;
int p = mod - 2;
while (p > 0) {
if (p % 2 == 1) {
r = (r * n) % mod;
}
n = (n * n) % mod;
p /= 2;
}
return r;
}
signed main()
{
int t=1;
c_p_c();
// cin >> t;
while(t--){
int n;
cin >> n;
vector<int> a(n);
vector<int> wei(n+1,0);
for(int i = 0 ; i < n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
cin >> wei[i];
wei[i] += wei[i-1];
}
stack<pair<int,int>> s;
s.push({0,0});
vector<int> v(n);
// cout<<1<<endl;
for(int i = 1; i <= n; i++){
while(!s.empty() and s.top().ff >= a[i-1]){
s.pop();
}
v[i-1] = s.top().ss;
s.push({a[i-1],i});
}
// cout<<1<<endl;
while(!s.empty()){
s.pop();
}
s.push({0,0});
reverse(a.begin(),a.end());
vector<int> b(n);
for(int i = 1; i <= n; i++){
while(!s.empty() and s.top().ff >= a[i-1]){
s.pop();
}
b[i-1] = n-s.top().ss;
s.push({a[i-1],i});
}
reverse(b.begin(),b.end());
reverse(a.begin(),a.end());
int ans = 0;
set<pair<pair<int,int>,int>> st;
for(int i = 0 ; i < n; i++){
st.insert({{v[i],b[i]},a[i]});
// cout<<v[i]<<" "<<b[i]<<endl;
}
vector<int> pref(n+2);
for(auto &x:st){
pref[x.ff.ff] += x.ss;
pref[x.ff.ss] -= x.ss;
}
for(int i = 1 ; i <= n; i++){
pref[i] += pref[i-1];
}
// for(int i = 0 ; i <= n; i++){
// cout<<pref[i]<<" ";
// }
// cout<<endl;
set<pair<int,int>> se;
for(int i = 0 ; i < n; i++){
// int cur =
if(se.empty() or se.find({v[i],b[i]}) == se.end()){
int cur_h = a[i];
int cur_w = wei[b[i]]-wei[v[i]];
int cur_rec = (((cur_h%mod)*(cur_h+1)%mod)%mod*modinv(2))%mod;
int cur_rec2 = (((cur_w%mod)*(cur_w+1)%mod)%mod*modinv(2))%mod;
int pref_h = pref[i] - a[i];
int x = ((cur_rec*cur_rec2)%mod-(pref_h*cur_rec2)%mod+mod)%mod;
// cout<<cur_h<<" "<<cur_w<<" "<<cur_rec<<" "<<cur_rec2<<" "<<x<<endl;
ans = (ans+x)%mod;
se.insert({v[i],b[i]});
}
}
cout<<ans<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
500 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
500 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |