4 2 1 3 7 5
4HintExplanation There are 4 possibilities: 1 3 1 3 5 3 5 7 5
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 100010; __int64 dp[N]; int xis[N]; int arr[N]; int cnt; struct node { int l, r; __int64 sum; }tree[N << 2]; int BinSearch(int val) { int l = 1, r = cnt, mid, ans; while (l <= r) { mid = (l + r) >> 1; if (xis[mid] == val) { ans = mid; break; } else if (xis[mid] > val) { r = mid - 1; } else { l = mid + 1; } } return ans; } int BinSearch_left(int val) { int l = 1; int r = cnt, mid, ans; while (l <= r) { mid = (l + r) >> 1; if (xis[mid] >= val) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; } int BinSearch_right(int val) { int l = 1; int r = cnt, mid, ans; while (l <= r) { mid = (l + r) >> 1; if (xis[mid] <= val) { ans = mid; l = mid + 1; } else { r = mid - 1; } } return ans; } void build(int p, int l, int r) { tree[p].l = l; tree[p].r = r; tree[p].sum = 0; if (l == r) { return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); } void update(int p, int pos, __int64 val) { if (tree[p].l == tree[p].r) { tree[p].sum += val; return; } int mid = (tree[p].l + tree[p].r) >> 1; if (pos <= mid) { update(p << 1, pos, val); } else { update(p << 1 | 1, pos, val); } tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum; tree[p].sum %= 9901; } __int64 query(int p, int l, int r) { if (tree[p].l >= l && tree[p].r <= r) { return tree[p].sum; } int mid = (tree[p].l + tree[p].r) >> 1; if (r <= mid) { return query(p << 1, l, r); } else if (l > mid) { return query(p << 1 | 1, l, r); } else { return query(p << 1, l, mid) + query(p << 1 | 1, mid + 1, r); } } int main() { int n, d; while(~scanf("%d%d", &n, &d)) { memset (dp, 0, sizeof(dp)); __int64 ans = 0; for (int i = 1; i <= n; ++i) { scanf("%d", &arr[i]); xis[i] = arr[i]; } sort(xis + 1, xis + n + 1); cnt = unique(xis + 1, xis + n + 1) - xis - 1; build(1, 1, cnt); dp[1] = 0; update(1, BinSearch(arr[1]), dp[1] + 1); int l, r; for (int i = 2; i <= n; i++) { l = BinSearch_left(arr[i] - d); r = BinSearch_right(arr[i] + d); dp[i] = query(1, l, r); dp[i] %= 9901; int x = BinSearch(arr[i]); update(1, x, dp[i] + 1); ans += dp[i]; ans %= 9901; } printf("%I64d\n", ans); } return 0; }
原文地址:http://blog.csdn.net/guard_mine/article/details/41749007