#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 5e5 + 23;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define kill(x) cout<<x<<endl, exit(0);
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lc (v << 1)
#define rc ((v<<1) |1)
#define int ll
void ok(int &x) {
while(x <0) x += mod;
x %=mod;
}
int pw(int a,int b) {
int ans=1;
while(b) {
if(b&1) ok(ans *= a);
b >>=1;
ok(a *= a);
}
return ans;
}
int n;
int h[N],w[N];
int l[N],r[N];
stack<int> st;
int ps[N];
int tedad =0;
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
for(int i = 1; i<= n ; i++) cin>>h[i];
for(int i = 1; i<= n ; i++) cin>>w[i];
for(int i = 1; i<= n ; i++) {
if(w[i] == 0) continue;
tedad ++;
for(int j = i+1; j <=n ; j ++) {
if(h[j] == h[i]) {
ok(w[i] += w[j]);
w[j] = 0;
h[j] = inf;
} else break;
}
}
int tinv = pw(2,mod-2);
for(int i = 1; i<= n+1 ; i++) {
ok(ps[i] = w[i] + ps[i-1]);
}
int ans=0;
h[0] = 0,h[n+1] = 0;
st.push(0);
for(int i = 1; i<= n; i++) {
if(w[i] == 0) continue;
while(h[st.top()] >= h[i]) st.pop();
l[i] = st.top();
st.push(i);
} while(sz(st)) st.pop();
st.push(n+1);
for(int i = n; i >= 1; i--) {
if(w[i] == 0) continue;
while(h[st.top()] > h[i]) st.pop();
r[i] = st.top();
st.push(i);
}
for(int i = 1; i<= n ; i++) {
if(w[i] == 0) continue;
int x = (h[i] * (h[i]+1)%mod*tinv%mod) %mod;
//ok(ans += (w[i] * (w[i]+1)%mod*tinv%mod) %mod * x %mod);
int one = (ps[i-1]- ps[l[i]] + 2 * mod) %mod;
int two = (ps[r[i]-1] - ps[i] + 2 *mod) %mod;
debug(one,two,x);
int f = (ps[r[i]-1] - ps[l[i]] + 2 * mod) %mod;
int three = f * (f+1)%mod*tinv%mod;
ok(three -= one * (one+1)%mod * tinv%mod);
ok(three -= two*(two+1) %mod * tinv%mod);
ok(ans += three *x );
//ok(ans -= x);
}
cout<<ans%mod << '\n';
return 0;
}
Compilation message
fancyfence.cpp: In function 'int32_t main()':
fancyfence.cpp:5:20: warning: statement has no effect [-Wunused-value]
5 | #define debug(...) 69
| ^~
fancyfence.cpp:92:3: note: in expansion of macro 'debug'
92 | debug(one,two,x);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
9 ms |
13404 KB |
Output is correct |
4 |
Correct |
23 ms |
18244 KB |
Output is correct |
5 |
Correct |
17 ms |
18260 KB |
Output is correct |
6 |
Correct |
21 ms |
18256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
18 ms |
10716 KB |
Output is correct |
4 |
Correct |
18 ms |
12892 KB |
Output is correct |
5 |
Correct |
22 ms |
12880 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8792 KB |
Output is correct |
4 |
Correct |
9 ms |
10844 KB |
Output is correct |
5 |
Correct |
21 ms |
12764 KB |
Output is correct |
6 |
Correct |
17 ms |
12764 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
4 ms |
8796 KB |
Output is correct |
9 |
Correct |
11 ms |
10972 KB |
Output is correct |
10 |
Correct |
23 ms |
17620 KB |
Output is correct |
11 |
Correct |
24 ms |
17864 KB |
Output is correct |
12 |
Correct |
1 ms |
8548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8548 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8628 KB |
Output is correct |
6 |
Correct |
1 ms |
8544 KB |
Output is correct |
7 |
Correct |
1 ms |
8664 KB |
Output is correct |
8 |
Correct |
2 ms |
8548 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
2 ms |
8656 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
8688 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
1 ms |
8684 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8656 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8656 KB |
Output is correct |
10 |
Correct |
2 ms |
8660 KB |
Output is correct |
11 |
Correct |
9 ms |
13404 KB |
Output is correct |
12 |
Correct |
19 ms |
18284 KB |
Output is correct |
13 |
Correct |
19 ms |
18268 KB |
Output is correct |
14 |
Correct |
17 ms |
18248 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
8860 KB |
Output is correct |
17 |
Correct |
13 ms |
11848 KB |
Output is correct |
18 |
Correct |
18 ms |
14796 KB |
Output is correct |
19 |
Correct |
24 ms |
14924 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
3 ms |
8928 KB |
Output is correct |
22 |
Correct |
10 ms |
11868 KB |
Output is correct |
23 |
Correct |
21 ms |
19656 KB |
Output is correct |
24 |
Correct |
21 ms |
19652 KB |
Output is correct |
25 |
Correct |
1 ms |
8536 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8668 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
3 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
11 ms |
13660 KB |
Output is correct |
33 |
Correct |
12 ms |
13916 KB |
Output is correct |
34 |
Correct |
21 ms |
18792 KB |
Output is correct |
35 |
Correct |
30 ms |
18804 KB |
Output is correct |
36 |
Correct |
23 ms |
18784 KB |
Output is correct |
37 |
Correct |
28 ms |
18752 KB |
Output is correct |
38 |
Correct |
1 ms |
8552 KB |
Output is correct |
39 |
Correct |
20 ms |
18764 KB |
Output is correct |
40 |
Correct |
22 ms |
18768 KB |
Output is correct |
41 |
Correct |
20 ms |
19032 KB |
Output is correct |
42 |
Correct |
21 ms |
19284 KB |
Output is correct |