#include <cmath>
#include <cassert>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
#include <stack>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimization ("Ofast")
using namespace std;
int M = 1e9 + 7;
struct modint {
modint() : n(0) {}
template<class T>
modint(T n) {
n %= M;
if (n < 0) n += M;
this->n = n;
}
modint &operator+=(const modint &r) {
n += r.n;
if (n >= M) n -= M;
return *this;
}
modint &operator-=(const modint &r) {
n -= r.n;
if (n < 0) n += M;
return *this;
}
modint &operator*=(const modint &r) {
n = (int) ((long long) n * r.n % M);
return *this;
}
modint &operator--() {
if (--n == -1) n = M - 1;
return *this;
}
modint &operator++() {
if (++n == M) n = 0;
return *this;
}
modint operator--(int) {
modint t = *this;
--*this;
return t;
}
modint operator++(int) {
modint t = *this;
++*this;
return t;
}
modint operator-() const { return 0 - *this; }
modint operator+() const { return *this; }
modint pow(long long k = M - 2) const {
modint f = *this, p = 1;
while (k > 0) {
if (k % 2 == 1) f *= p;
p *= p, k /= 2;
}
return f;
}
int mod() const { return M; }
friend modint operator+(modint l, const modint &r) { return l += r; }
friend modint operator-(modint l, const modint &r) { return l -= r; }
friend modint operator*(modint l, const modint &r) { return l *= r; }
friend bool operator==(const modint &l, const modint &r) { return l.n == r.n; }
friend bool operator!=(const modint &l, const modint &r) { return l.n != r.n; }
friend ostream &operator<<(ostream &out, const modint &r) { return out << r.n; }
int n;
};
modint c2 (modint x) {
int64_t n = x.n;
return (n * (n + 1)/2) % M;
}
const int MOD = 1e9 + 7;
struct SparseTable {
vector<modint> val;
vector<vector<int>> dp;
int query (int l, int r) {
if (l > r) {
return INT_MAX;
}
int sz = log2(r - l + 1);
return min(dp[l][sz], dp[min(r - (1 << sz) + 1, (int)dp.size() - 1)][sz]);
}
void init () {
dp.resize(val.size());
for (int i = 0; i < dp.size(); i++) {
dp[i].resize(32);
dp[i][0] = val[i].n;
}
for (int j = 1; j < 32; j++) {
for (int i = 0; i < dp.size(); i++) {
dp[i][j] = min(dp[i][j - 1], dp[min(i + (1 << (j - 1)), (int)dp.size() - 1)][j - 1]);
}
}
}
SparseTable (vector<modint> val) {
this->val = val;
init();
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<modint> h(N), w(N);
vector<pair<modint, modint>> grid(N);
map<int,vector<int>> myMap;
for (int i = 0; i < N; i++) {
cin >> h[i].n;
myMap[h[i].n].push_back(i);
}
vector<modint> pref = {0};
for (int i = 0; i < N; i++) {
cin >> w[i].n;
pref.push_back(pref.back() + w[i]);
}
for (int i = 0; i < N; i++) {
grid[i] = make_pair(w[i], h[i]);
}
SparseTable st(h);
modint ans = 0;
for (auto& p: myMap) {
int x = p.first;
for (int i: p.second) {
if (h[i] == x) {
int L = 0;
int R = i;
while (L != R) {
int MID = (L + R)/2;
if (st.query(MID, i - 1) >= x) {
R = MID;
} else {
L = MID + 1;
}
}
int l = L;
L = i;
R = N - 1;
while (L != R) {
int MID = (L + R + 1)/2;
if (st.query(i + 1, MID) > x) {
L = MID;
} else {
R = MID - 1;
}
}
int r = R;
modint sum = (pref[i] - pref[l]) * (pref[r + 1] - pref[i + 1]);
sum += (pref[r + 1] - pref[l] - w[i]) * w[i];
ans += sum* c2(x);
}
}
}
for (int i = 0; i < N; i++) {
ans += c2(grid[i].first) * c2(grid[i].second);
}
cout << ans << '\n';
return 0;
}
Compilation message
fancyfence.cpp:16: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
16 | #pragma GCC optimization ("O3")
|
fancyfence.cpp:17: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
17 | #pragma GCC optimization ("unroll-loops")
|
fancyfence.cpp:18: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
18 | #pragma GCC optimization ("Ofast")
|
fancyfence.cpp: In member function 'void SparseTable::init()':
fancyfence.cpp:117:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int i = 0; i < dp.size(); i++) {
| ~~^~~~~~~~~~~
fancyfence.cpp:122:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for (int i = 0; i < dp.size(); i++) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
58 ms |
10108 KB |
Output is correct |
4 |
Correct |
124 ms |
19912 KB |
Output is correct |
5 |
Correct |
135 ms |
20988 KB |
Output is correct |
6 |
Correct |
133 ms |
21012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
9 ms |
2252 KB |
Output is correct |
3 |
Correct |
50 ms |
10016 KB |
Output is correct |
4 |
Correct |
121 ms |
19792 KB |
Output is correct |
5 |
Correct |
125 ms |
21824 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
9 ms |
2252 KB |
Output is correct |
4 |
Correct |
51 ms |
10096 KB |
Output is correct |
5 |
Correct |
122 ms |
19792 KB |
Output is correct |
6 |
Correct |
122 ms |
21920 KB |
Output is correct |
7 |
Correct |
2 ms |
580 KB |
Output is correct |
8 |
Correct |
11 ms |
3532 KB |
Output is correct |
9 |
Correct |
59 ms |
13092 KB |
Output is correct |
10 |
Correct |
149 ms |
32268 KB |
Output is correct |
11 |
Correct |
168 ms |
32372 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
528 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
52 ms |
10172 KB |
Output is correct |
12 |
Correct |
125 ms |
19904 KB |
Output is correct |
13 |
Correct |
138 ms |
21044 KB |
Output is correct |
14 |
Correct |
138 ms |
21140 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
9 ms |
2376 KB |
Output is correct |
17 |
Correct |
50 ms |
10948 KB |
Output is correct |
18 |
Correct |
120 ms |
21832 KB |
Output is correct |
19 |
Correct |
129 ms |
21812 KB |
Output is correct |
20 |
Correct |
2 ms |
580 KB |
Output is correct |
21 |
Correct |
11 ms |
3500 KB |
Output is correct |
22 |
Correct |
62 ms |
13132 KB |
Output is correct |
23 |
Correct |
152 ms |
32168 KB |
Output is correct |
24 |
Correct |
174 ms |
32372 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
2 ms |
580 KB |
Output is correct |
28 |
Correct |
1 ms |
572 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
14 ms |
3384 KB |
Output is correct |
31 |
Correct |
14 ms |
3424 KB |
Output is correct |
32 |
Correct |
83 ms |
10976 KB |
Output is correct |
33 |
Correct |
112 ms |
16332 KB |
Output is correct |
34 |
Correct |
326 ms |
31668 KB |
Output is correct |
35 |
Correct |
257 ms |
21736 KB |
Output is correct |
36 |
Correct |
321 ms |
32324 KB |
Output is correct |
37 |
Correct |
375 ms |
27984 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
296 ms |
26524 KB |
Output is correct |
40 |
Correct |
290 ms |
32096 KB |
Output is correct |
41 |
Correct |
210 ms |
32320 KB |
Output is correct |
42 |
Correct |
150 ms |
32296 KB |
Output is correct |