Topic Link: Click to open the link
Test instructions: give you a sequence of n number, a number h, to find the difference of the number of neighbors not exceeding h number of subsequence and% 9901.
Idea: The classical water problem, apparently using d[i] to indicate the number of sub-sequences satisfying the condition ending with a[i]. So for J < I, | A[J]-A[i] | <= h, equivalent to A[j] <= A[i] + H && a[j] >= a[i]-H. For this restriction with the line tree subscript maintenance, the segment tree is used to maintain the summation of the d[i].
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue> # Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) (
A):(B)) using namespace Std;
typedef long Long LL;
typedef long double LD;
Const LD EPS = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 9901;
const int INF = 0X3F3F3F3F;
& 0x7FFFFFFF const int seed = 131;
CONST LL INF64 = LL (1e18);
const int MAXN = 100000 + 10;
int t,n,m,sum[maxn<<2],h,d[maxn],a[maxn],b[maxn];
inline void Add (int& a, int b) {A + = B;
if (a >= mod) a%= mod;
} void pushup (int o) {Sum[o] = sum[o<<1] + sum[o<<1|1];
if (Sum[o] >= MoD) sum[o]%= mod; } void Build (int l, int r, int o) {int m = (L + R);> 1;
Sum[o] = 0;
if (L = = r) return;
Build (L, M, o<<1);
Build (M+1, R, O<<1|1);
Pushup (o);
} void Update (int l, int r, ll v, int l, int r, int o) {int m = (L + R) >> 1;
if (l <= l && R <= R) {Add (Sum[o], v); return;
} if (l <= m) update (L, R, V, L, M, o<<1);
if (M < R) Update (L, R, V, M+1, R, O<<1|1);
Pushup (o);
} int query (int l, int r, int l, int r, int o) {int m = (L + R) >> 1;
if (l <= l && R <= R) return Sum[o];
int ans = 0;
if (l <= m) Add (ans, query (L, R, L, M, o<<1));
if (M < R) Add (ans, query (L, R, M+1, R, O<<1|1));
Pushup (o);
return ans; } int main () {while (~scanf ("%d%d", &n,&h)) {for (int i=1;i<=n;i++) {scanf ("%d", &a[
I]);
B[I-1] = A[i];
} sort (b, b+n);
int len = unique (b, b+n)-B;
Build (1, Len, 1);
int ans = 0; for (int i=1;i<=n;i++) {int L = Lower_bound (b, B+len, a[i]-h)-B + 1;
int r = Upper_bound (b, B+len, a[i]+h)-B;
int pos = Lower_bound (b, B+len, a[i])-B + 1;
D[i] = 1;
Add (D[i], query (L, R, 1, Len, 1));
Add (ans, d[i]-1);
Update (POS, POS, D[i], 1, Len, 1);
} printf ("%d\n", ans);
} return 0;
}