#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
const ll mod2=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=101000;
int n,h[N],w[N],l[N],r[N];
ll pf[N];
int main() {
scanf("%d",&n);
rep(i,1,n+1) scanf("%d",h+i);
rep(i,1,n+1) scanf("%d",w+i);
rep(i,1,n+1) pf[i]=pf[i-1]+w[i];
stack<int> stk;
rep(i,1,n+1) {
while (SZ(stk)&&h[stk.top()]>h[i]) stk.pop();
l[i]=(SZ(stk)?stk.top()+1:1);
stk.push(i);
}
while (SZ(stk)) stk.pop();
per(i,1,n+1) {
while (SZ(stk)&&h[stk.top()]>=h[i]) stk.pop();
r[i]=(SZ(stk)?stk.top()-1:n);
stk.push(i);
}
//rep(i,1,n+1) printf("%d ",l[i]); puts("");
//rep(i,1,n+1) printf("%d ",r[i]); puts("");
ll ans=0;
rep(i,1,n+1) {
int L=l[i],R=r[i];
ll sl=(pf[i-1]-pf[L-1])%mod;
ll sr=(pf[R]-pf[i])%mod;
ll x=((ll)h[i]*(h[i]+1)/2)%mod;
ll y=((ll)w[i]*(w[i]+1)/2)%mod;
ans=(ans+((x*sl)%mod)*(sr+w[i])%mod)%mod;
ans=(ans+((x*w[i])%mod)*sr)%mod;
ans=(ans+x*y)%mod;
}
printf("%lld",ans);
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
fancyfence.cpp:29:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | rep(i,1,n+1) scanf("%d",h+i);
| ~~~~~^~~~~~~~~~
fancyfence.cpp:30:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | rep(i,1,n+1) scanf("%d",w+i);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
10 ms |
2160 KB |
Output is correct |
4 |
Correct |
23 ms |
4036 KB |
Output is correct |
5 |
Correct |
20 ms |
3940 KB |
Output is correct |
6 |
Correct |
21 ms |
3812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
3 ms |
720 KB |
Output is correct |
3 |
Correct |
13 ms |
2648 KB |
Output is correct |
4 |
Correct |
23 ms |
4980 KB |
Output is correct |
5 |
Correct |
24 ms |
5072 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
3 ms |
720 KB |
Output is correct |
4 |
Correct |
12 ms |
2640 KB |
Output is correct |
5 |
Correct |
26 ms |
4976 KB |
Output is correct |
6 |
Correct |
24 ms |
5028 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
3 ms |
720 KB |
Output is correct |
9 |
Correct |
12 ms |
2640 KB |
Output is correct |
10 |
Correct |
24 ms |
4880 KB |
Output is correct |
11 |
Correct |
23 ms |
4984 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
11 ms |
2128 KB |
Output is correct |
12 |
Correct |
21 ms |
4040 KB |
Output is correct |
13 |
Correct |
22 ms |
3928 KB |
Output is correct |
14 |
Correct |
20 ms |
3776 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
3 ms |
720 KB |
Output is correct |
17 |
Correct |
13 ms |
2644 KB |
Output is correct |
18 |
Correct |
24 ms |
4916 KB |
Output is correct |
19 |
Correct |
25 ms |
5104 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
720 KB |
Output is correct |
22 |
Correct |
12 ms |
2640 KB |
Output is correct |
23 |
Correct |
22 ms |
4804 KB |
Output is correct |
24 |
Correct |
24 ms |
4940 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
0 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
3 ms |
720 KB |
Output is correct |
31 |
Correct |
4 ms |
720 KB |
Output is correct |
32 |
Correct |
12 ms |
2384 KB |
Output is correct |
33 |
Correct |
14 ms |
2436 KB |
Output is correct |
34 |
Correct |
25 ms |
4400 KB |
Output is correct |
35 |
Correct |
25 ms |
4312 KB |
Output is correct |
36 |
Correct |
25 ms |
4544 KB |
Output is correct |
37 |
Correct |
29 ms |
4572 KB |
Output is correct |
38 |
Correct |
0 ms |
336 KB |
Output is correct |
39 |
Correct |
24 ms |
4560 KB |
Output is correct |
40 |
Correct |
23 ms |
4508 KB |
Output is correct |
41 |
Correct |
23 ms |
4560 KB |
Output is correct |
42 |
Correct |
25 ms |
4716 KB |
Output is correct |