-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIncreasing Subsequence II.cpp
80 lines (65 loc) · 1.4 KB
/
Increasing Subsequence II.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "bits/stdc++.h"
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define M 1000000007
struct FenwickTree {
vector<ll> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
ll sum(int r) {
ll ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1){
ret += bit[r];
ret%=M;
}
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
int main(){
// ifstream cin("test_input.txt");
int n;
cin>>n;
int ara[n];
set<int> ss;
for(int i=0;i<n;i++) {
cin>>ara[i];
ss.insert(ara[i]);
}
map<int,int> compress;
int cnt = 0;
for(auto s:ss){
compress[s] = ++cnt;
}
for(int i=0;i<n;i++)
ara[i] = compress[ ara[i] ];
FenwickTree ft(cnt+5);
ll res = 0;
for(int i=0;i<n;i++){
ll temp = ft.sum(0, ara[i]-1 );
temp++;
temp%=M;
ft.add(ara[i],temp);
res+=temp;
res%=M;
}
cout<<res;
}