-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1748.cpp
47 lines (43 loc) · 929 Bytes
/
1748.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, MOD = 1e9 + 7;
int n, x[N], y[N], f[N], fen[N], ans;
void add(int p, int v) {
for (; p <= n; p += p & -p) {
fen[p] += v;
if (fen[p] >= MOD) fen[p] -= MOD;
}
}
int get(int p) {
int v = 0;
for (; p >= 1; p -= p & -p) {
v += fen[p];
if (v >= MOD) v -= MOD;
}
return v;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
y[i] = i;
}
sort(y + 1, y + 1 + n, [&](const int &a, const int &b) {
return x[a] < x[b];
});
for (int i = 1, last = -1, m = 0; i <= n; ++i) {
if (x[y[i]] != last) ++m;
last = x[y[i]];
x[y[i]] = m;
}
for (int i = 1; i <= n; ++i) {
f[i] = 1 + get(x[i] - 1);
if (f[i] >= MOD) f[i] -= MOD;
ans += f[i];
if (ans >= MOD) ans -= MOD;
add(x[i], f[i]);
}
cout << ans;
return 0;
}