forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncreasing Subsequence II.cpp
More file actions
41 lines (35 loc) · 856 Bytes
/
Increasing Subsequence II.cpp
File metadata and controls
41 lines (35 loc) · 856 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+5;
const ll MOD = 1e9+7;
int N;
ll ans, ds[maxN];
struct Operation { int x, idx; } ops[maxN];
void update(int idx, ll val){
for(int i = idx; i < maxN; i += -i&i)
ds[i] = (ds[i] + val) % MOD;
}
ll query(int idx){
ll sum = 0;
for(int i = idx; i > 0; i -= -i&i)
sum = (sum + ds[i]) % MOD;
return sum;
}
int main(){
scanf("%d", &N);
for(int i = 0, x; i < N; i++){
scanf("%d", &x);
ops[i] = {x, i+1};
}
sort(ops, ops+N, [](Operation A, Operation B){
return A.x == B.x ? B.idx < A.idx : A.x < B.x;
});
for(int i = 0; i < N; i++){
int idx = ops[i].idx;
ll amnt = query(idx)+1;
ans = (ans + amnt) % MOD;
update(idx, amnt);
}
printf("%lld\n", ans);
}