Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- const ll mod = 1e9 + 7;
- const int N = 1e5 + 3;
- ll tree[4 * N];
- int a[N], n;
- void init(int n) {
- for (int i = 0; i <= 4 * n; ++i) {
- tree[i] = 0;
- }
- }
- void update(int v, int tl, int tr, int pos, ll x) {
- if (tl == tr) {
- tree[v] = (tree[v] + x) % mod;
- } else {
- int tm = (tl + tr) / 2;
- if (pos <= tm) {
- update(v * 2, tl, tm, pos, x);
- } else {
- update(v * 2 + 1, tm + 1, tr, pos, x);
- }
- tree[v] = (tree[v * 2] + tree[v * 2 + 1]) % mod;
- }
- }
- ll sum(int v, int tl, int tr, int l, int r) {
- if (l > r) {
- return 0;
- }
- if (tl == l && r == tr) {
- return tree[v];
- }
- int tm = (tl + tr) / 2;
- return (sum(v * 2, tl, tm, l, min(tm, r)) + sum(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r)) % mod;
- }
- void solve(int tc) {
- cout << "Case " << tc << ": ";
- cin >> n;
- pair<int, int> b[n];
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- b[i] = {a[i], i};
- }
- sort(b + 0, b + n);
- int k = 1;
- a[b[0].second] = k;
- for (int i = 1; i < n; ++i) {
- if (b[i - 1].first != b[i].first) {
- k++;
- }
- a[b[i].second] = k;
- }
- ll ans = 0;
- init(k);
- for (int i = 0; i < n; ++i) {
- ll cur = sum(1, 1, k, 1, a[i] - 1) + 1;
- ans = (ans + cur) % mod;
- update(1, 1, k, a[i], cur);
- }
- cout << ans << endl;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- int test_cnt;
- cin >> test_cnt;
- for (int tc = 1; tc <= test_cnt; ++tc) {
- solve(tc);
- }
- return 0;
- }
- /*
- 1 2 3 3 4
- 23
- 3 20
- 1 2 8 12
- 1 2 3 4
- {1}
- {1, 2}, {2}
- {1, 3}, {1, 2, 3}, {2, 3}, {3}
- {1, 3}, {1, 2, 3}, {2, 3}, {3}
- {1,4}
- {1, 2, 4}, {2, 4}
- {1, 3, 4}, {1, 2, 3, 4}, {2, 3, 4}, {3, 4}
- {1, 3, 4}, {1, 2, 3, 4}, {2, 3, 4}, {3, 4}. {4}
- ans += get(1, 1-1)=> +get(1,0)=0 + 1 = 1
- ans += get(1, 2-1)=> +get(1,1)=1 + 1 = 2
- ans += get(1, 3-1)=> +get(1,2)=3 + 1 = 4
- ans += get(1, 3-1)=> +get(1,2)=3 + 1 = 4
- ans += get(1, 4-1)=> +get(1,3)=11 + 1 = 12
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement