#include <cstdio>
#include <stack>
#include <vector>
#define X first
#define Y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
inline int add(int a, int b) {
return ((ll) a + b + MOD) % MOD;
}
inline int mult(int a, int b) {
return (ll) a * b % MOD;
}
inline int frac(int a) {
return ((ll) a * (a + 1) / 2) % MOD;
}
int n, H[N], W[N];
int sol, sum;
stack<pii> stog;
int product(int h, int w) {
return mult(w, frac(h));
}
int calc(int h, int w) {
return mult(frac(w), frac(h));
}
int _calc(int h, int w1, int w2) {
return mult(mult(w1, w2), frac(h));
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", H + i);
for(int i = 0; i < n; i++) scanf("%d", W + i);
for(int i = 0; i < n; i++) {
sol = add(sol, calc(H[i], W[i])); //printf("+= %d\n", calc(H[i], W[i]));
int _w = 0;
while(!stog.empty() && stog.top().X >= H[i]) {
int h = stog.top().X;
int w = stog.top().Y;
sum = add(sum, -product(h, w));
_w = add(_w, w);
stog.pop();
}
sol = add(sol, mult(sum, W[i]));
sol = add(sol, _calc(H[i], _w, W[i]));
_w = add(W[i], _w);
stog.push({H[i], _w});
sum = add(sum, product(H[i], _w));
}
printf("%d\n", sol);
return 0;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
fancyfence.cpp:47:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | for(int i = 0; i < n; i++) scanf("%d", H + i);
| ~~~~~^~~~~~~~~~~~~
fancyfence.cpp:48:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | for(int i = 0; i < n; i++) scanf("%d", W + i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
1052 KB |
Output is correct |
3 |
Correct |
10 ms |
1368 KB |
Output is correct |
4 |
Correct |
18 ms |
2128 KB |
Output is correct |
5 |
Correct |
18 ms |
2368 KB |
Output is correct |
6 |
Correct |
18 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
3 |
Correct |
10 ms |
1628 KB |
Output is correct |
4 |
Correct |
23 ms |
3420 KB |
Output is correct |
5 |
Correct |
21 ms |
2984 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
600 KB |
Output is correct |
4 |
Correct |
11 ms |
1624 KB |
Output is correct |
5 |
Correct |
21 ms |
3132 KB |
Output is correct |
6 |
Correct |
25 ms |
3408 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
11 ms |
2080 KB |
Output is correct |
10 |
Correct |
22 ms |
3664 KB |
Output is correct |
11 |
Correct |
20 ms |
3916 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
796 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
1368 KB |
Output is correct |
12 |
Correct |
18 ms |
2136 KB |
Output is correct |
13 |
Correct |
18 ms |
2128 KB |
Output is correct |
14 |
Correct |
18 ms |
2136 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
11 ms |
1624 KB |
Output is correct |
18 |
Correct |
21 ms |
2904 KB |
Output is correct |
19 |
Correct |
21 ms |
3156 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
11 ms |
1884 KB |
Output is correct |
23 |
Correct |
19 ms |
3620 KB |
Output is correct |
24 |
Correct |
20 ms |
3828 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
600 KB |
Output is correct |
31 |
Correct |
2 ms |
600 KB |
Output is correct |
32 |
Correct |
10 ms |
1624 KB |
Output is correct |
33 |
Correct |
11 ms |
1756 KB |
Output is correct |
34 |
Correct |
27 ms |
3160 KB |
Output is correct |
35 |
Correct |
20 ms |
2908 KB |
Output is correct |
36 |
Correct |
23 ms |
2904 KB |
Output is correct |
37 |
Correct |
22 ms |
2896 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
21 ms |
3152 KB |
Output is correct |
40 |
Correct |
21 ms |
2896 KB |
Output is correct |
41 |
Correct |
20 ms |
3156 KB |
Output is correct |
42 |
Correct |
21 ms |
3288 KB |
Output is correct |