#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxN = 1e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
const int MOD = 1e9 + 7;
int pref[mxN], nex[mxN], prv[mxN], seg[4 * mxN], lazy[4 * mxN];
pair<int,int> arr[mxN];
void push(int ind, int l, int r) {
if (lazy[ind]) {
seg[ind] = (r - l + 1) * lazy[ind] % MOD;
if (l != r) {
lazy[2*ind] = lazy[ind];
lazy[2*ind+1] = lazy[ind];
}
lazy[ind] = 0;
}
}
void update(int lo, int hi, int val, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (lo > r || l > hi) {
return;
}
if (lo <= l && r <= hi) {
lazy[ind] = val;
push(ind, l, r);
return;
}
int mid = (l + r) / 2;
update(lo, hi, val, 2*ind, l, mid);
update(lo, hi, val, 2*ind+1, mid+1, r);
}
int query(int pos, int ind = 1, int l = 0, int r = SZ - 1) {
push(ind, l, r);
if (l == r) {
return seg[ind];
}
int mid = (l + r) / 2;
if (pos <= mid) {
return query(pos, 2*ind, l, mid);
}
else {
return query(pos, 2*ind+1, mid+1, r);
}
}
int nc2(int n) {
return n * (n - 1) / 2;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<pair<int,int>> pos;
for (int i = 1; i <= n; ++i) {
cin >> arr[i].first;
pos.push_back({arr[i].first, i});
}
sort(pos.begin(), pos.end());
for (int i = 1; i <= n; ++i) {
cin >> arr[i].second;
pref[i] = pref[i - 1] + arr[i].second;
}
vector<int> st = {n + 1};
for (int i = n; i >= 1; --i) {
while (st.size() && arr[i].first <= arr[st.back()].first) {
st.pop_back();
}
assert(st.size());
nex[i] = st.back();
st.push_back(i);
}
st = {0};
for (int i = 1; i <= n; ++i) {
while (st.size() && arr[i].first <= arr[st.back()].first) {
st.pop_back();
}
assert(st.size());
prv[i] = st.back();
st.push_back(i);
}
int ans = 0;
for (auto [h, i] : pos) {
int w = (pref[nex[i] - 1] - pref[prv[i]]) % MOD;
ans += (nc2(h + 1) - query(i)) % MOD * (nc2(w + 1) % MOD) % MOD;
ans %= MOD;
update(prv[i] + 1, nex[i] - 1, nc2(h + 1) % MOD);
}
cout << ans << "\n";
}
Compilation message
fancyfence.cpp: In function 'int32_t main()':
fancyfence.cpp:99:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
99 | for (auto [h, i] : pos) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
26 ms |
7640 KB |
Output is correct |
4 |
Correct |
53 ms |
10596 KB |
Output is correct |
5 |
Correct |
49 ms |
10560 KB |
Output is correct |
6 |
Correct |
45 ms |
10440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
6 ms |
3388 KB |
Output is correct |
3 |
Correct |
28 ms |
7636 KB |
Output is correct |
4 |
Correct |
55 ms |
10436 KB |
Output is correct |
5 |
Correct |
55 ms |
10432 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
6 ms |
3296 KB |
Output is correct |
4 |
Correct |
28 ms |
7668 KB |
Output is correct |
5 |
Correct |
54 ms |
10440 KB |
Output is correct |
6 |
Correct |
56 ms |
10436 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
6 ms |
3552 KB |
Output is correct |
9 |
Correct |
27 ms |
7896 KB |
Output is correct |
10 |
Correct |
55 ms |
12228 KB |
Output is correct |
11 |
Correct |
56 ms |
12228 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2588 KB |
Output is correct |
11 |
Correct |
26 ms |
7640 KB |
Output is correct |
12 |
Correct |
60 ms |
10440 KB |
Output is correct |
13 |
Correct |
51 ms |
10436 KB |
Output is correct |
14 |
Correct |
46 ms |
10452 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
6 ms |
3296 KB |
Output is correct |
17 |
Correct |
28 ms |
7476 KB |
Output is correct |
18 |
Correct |
54 ms |
10588 KB |
Output is correct |
19 |
Correct |
56 ms |
10440 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
6 ms |
3552 KB |
Output is correct |
22 |
Correct |
29 ms |
7896 KB |
Output is correct |
23 |
Correct |
56 ms |
12232 KB |
Output is correct |
24 |
Correct |
55 ms |
12192 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2664 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
7 ms |
3292 KB |
Output is correct |
31 |
Correct |
7 ms |
3296 KB |
Output is correct |
32 |
Correct |
30 ms |
7636 KB |
Output is correct |
33 |
Correct |
31 ms |
7640 KB |
Output is correct |
34 |
Correct |
62 ms |
11220 KB |
Output is correct |
35 |
Correct |
61 ms |
10452 KB |
Output is correct |
36 |
Correct |
64 ms |
10452 KB |
Output is correct |
37 |
Correct |
65 ms |
10496 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
63 ms |
10532 KB |
Output is correct |
40 |
Correct |
66 ms |
11204 KB |
Output is correct |
41 |
Correct |
59 ms |
10496 KB |
Output is correct |
42 |
Correct |
60 ms |
11976 KB |
Output is correct |