/** work as hard as you can and keep the first place **/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define Mp make_pair
const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const int LOG = 20;
ll pw(ll a, ll b, ll M = mod, ll ret = 1) { while(b) { ret = ret * (b & 1? a : 1) % M, a = a * a % M, b >>= 1; } return ret; }
ll inv2, ps[N];
int n, h[N], w[N];
vector < int > cm, Q[N];
set < pair < pii, ll > > st;
ll C2(ll x)
{
x %= mod;
return x * (x - 1) % mod * inv2 % mod;
}
ll baze(int l, int r)
{
return 1ll * (r + l) * (r - l + 1) % mod * inv2 % mod;
}
void add(int l, int r)
{
if(l > r) return;
st.insert(Mp(Mp(l, r), ps[r] - ps[l - 1]));
}
int main()
{
inv2 = pw(2, mod - 2, mod);
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
scanf("%d", &h[i]);
cm.push_back(h[i]);
}
for(int i = 1; i <= n; i ++)
{
scanf("%d", &w[i]);
ps[i] = ps[i - 1] + w[i];
}
st.insert(Mp(Mp(1, n), ps[n]));
cm.push_back(0);
sort(all(cm));
cm.resize(unique(all(cm)) - cm.begin());
for(int i = 1; i <= n; i ++)
{
int cu = lower_bound(all(cm), h[i]) - cm.begin();
Q[cu].push_back(i);
}
ll sum = C2(ps[n] + 1), tot = 0;
for(int i = 1; i < SZ(cm); i ++)
{
for(auto id : Q[i - 1])
{
auto it = st.lower_bound(Mp(Mp(id, 1e9), -1));
it --;
int l = (*it).F.F, r = (*it).F.S;
sum = (sum - C2(ps[r] - ps[l - 1] + 1) + mod) % mod;
st.erase(it);
add(l, id - 1), add(id + 1, r);
sum = (sum + C2(ps[r] - ps[id] + 1) + C2(ps[id - 1] - ps[l - 1] + 1)) % mod;
}
///printf("i = %d cm = %d sum = %lld\n", i, cm[i], sum);
tot = (tot + baze(cm[i - 1] + 1, cm[i]) * sum % mod) % mod;
}
printf("%lld", tot);
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
fancyfence.cpp:57:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d", &h[i]);
| ~~~~~^~~~~~~~~~~~~
fancyfence.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d", &w[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23688 KB |
Output is correct |
2 |
Correct |
15 ms |
23884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23712 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
14 ms |
23772 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23812 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
35 ms |
26328 KB |
Output is correct |
4 |
Correct |
70 ms |
28348 KB |
Output is correct |
5 |
Correct |
64 ms |
28696 KB |
Output is correct |
6 |
Correct |
44 ms |
27788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23796 KB |
Output is correct |
2 |
Correct |
18 ms |
24268 KB |
Output is correct |
3 |
Correct |
30 ms |
26060 KB |
Output is correct |
4 |
Correct |
46 ms |
28092 KB |
Output is correct |
5 |
Correct |
49 ms |
28096 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23696 KB |
Output is correct |
2 |
Correct |
15 ms |
23856 KB |
Output is correct |
3 |
Correct |
18 ms |
24312 KB |
Output is correct |
4 |
Correct |
31 ms |
26052 KB |
Output is correct |
5 |
Correct |
45 ms |
28052 KB |
Output is correct |
6 |
Correct |
45 ms |
28052 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
22 ms |
24400 KB |
Output is correct |
9 |
Correct |
41 ms |
26644 KB |
Output is correct |
10 |
Correct |
72 ms |
30524 KB |
Output is correct |
11 |
Correct |
77 ms |
30396 KB |
Output is correct |
12 |
Correct |
16 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
18 ms |
23796 KB |
Output is correct |
3 |
Correct |
17 ms |
23752 KB |
Output is correct |
4 |
Correct |
18 ms |
23756 KB |
Output is correct |
5 |
Correct |
18 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
16 ms |
23696 KB |
Output is correct |
8 |
Correct |
17 ms |
23840 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23756 KB |
Output is correct |
11 |
Correct |
15 ms |
23836 KB |
Output is correct |
12 |
Correct |
17 ms |
23756 KB |
Output is correct |
13 |
Correct |
18 ms |
23800 KB |
Output is correct |
14 |
Correct |
16 ms |
23844 KB |
Output is correct |
15 |
Correct |
15 ms |
23804 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23688 KB |
Output is correct |
2 |
Correct |
15 ms |
23884 KB |
Output is correct |
3 |
Correct |
16 ms |
23756 KB |
Output is correct |
4 |
Correct |
15 ms |
23884 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23752 KB |
Output is correct |
7 |
Correct |
15 ms |
23688 KB |
Output is correct |
8 |
Correct |
17 ms |
23784 KB |
Output is correct |
9 |
Correct |
16 ms |
23776 KB |
Output is correct |
10 |
Correct |
18 ms |
23756 KB |
Output is correct |
11 |
Correct |
36 ms |
26320 KB |
Output is correct |
12 |
Correct |
71 ms |
28348 KB |
Output is correct |
13 |
Correct |
58 ms |
28812 KB |
Output is correct |
14 |
Correct |
45 ms |
27856 KB |
Output is correct |
15 |
Correct |
16 ms |
23756 KB |
Output is correct |
16 |
Correct |
18 ms |
24268 KB |
Output is correct |
17 |
Correct |
36 ms |
26064 KB |
Output is correct |
18 |
Correct |
45 ms |
28104 KB |
Output is correct |
19 |
Correct |
47 ms |
28060 KB |
Output is correct |
20 |
Correct |
16 ms |
23796 KB |
Output is correct |
21 |
Correct |
20 ms |
24440 KB |
Output is correct |
22 |
Correct |
42 ms |
26540 KB |
Output is correct |
23 |
Correct |
71 ms |
30432 KB |
Output is correct |
24 |
Correct |
74 ms |
30448 KB |
Output is correct |
25 |
Correct |
15 ms |
23724 KB |
Output is correct |
26 |
Correct |
15 ms |
23796 KB |
Output is correct |
27 |
Correct |
16 ms |
23796 KB |
Output is correct |
28 |
Correct |
16 ms |
23812 KB |
Output is correct |
29 |
Correct |
18 ms |
23792 KB |
Output is correct |
30 |
Correct |
25 ms |
24572 KB |
Output is correct |
31 |
Correct |
24 ms |
24656 KB |
Output is correct |
32 |
Correct |
50 ms |
26708 KB |
Output is correct |
33 |
Correct |
67 ms |
28132 KB |
Output is correct |
34 |
Correct |
149 ms |
31816 KB |
Output is correct |
35 |
Correct |
89 ms |
29268 KB |
Output is correct |
36 |
Correct |
136 ms |
32124 KB |
Output is correct |
37 |
Correct |
132 ms |
30800 KB |
Output is correct |
38 |
Correct |
14 ms |
23756 KB |
Output is correct |
39 |
Correct |
119 ms |
29112 KB |
Output is correct |
40 |
Correct |
109 ms |
30328 KB |
Output is correct |
41 |
Correct |
78 ms |
30388 KB |
Output is correct |
42 |
Correct |
64 ms |
30404 KB |
Output is correct |