http://acm.hdu.edu.cn/showproblem.php?pid=3530
There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and no larger than k.
There are multiple test cases.
For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range $[1, 100000]$. m and k are in the range $[0, 1000000]$. The second line has n integers, which are all in the range $[0, 1000000]$.
Proceed to the end of file.
For each test case, print the length of the subsequence on a single line.
5 0 0
1 1 1 1 1
5 0 3
1 2 3 4 5
5
4
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 #include<set> 9 using std::set; 10 using std::map; 11 using std::min; 12 using std::max; 13 using std::cin; 14 using std::cout; 15 using std::endl; 16 using std::find; 17 using std::sort; 18 using std::pair; 19 using std::vector; 20 #define sz(c) (int)(c).size() 21 #define all(c) (c).begin(), (c).end() 22 #define iter(c) decltype((c).begin()) 23 #define cls(arr,val) memset(arr,val,sizeof(arr)) 24 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 25 #define rep(i, n) for (int i = 1; i <= (int)(n); i++) 26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 27 #define pb(e) push_back(e) 28 #define mp(a, b) make_pair(a, b) 29 #define mid ((l+r)>>1) 30 #define lc (root<<1) 31 #define rc (root<<1|1) 32 const int Max_N = 100010; 33 const int INF = 0x3f3f3f3f; 34 typedef unsigned long long ull; 35 int n, m, k, tmin, tmax; 36 struct SegTree { 37 struct Node { int max, min; }seg[Max_N << 2]; 38 inline void built(int root, int l, int r) { 39 if (l == r) { 40 scanf("%d", &seg[root].max), seg[root].min = seg[root].max; 41 return; 42 } 43 built(lc, l, mid); 44 built(rc, mid + 1, r); 45 seg[root].max = max(seg[lc].max, seg[rc].max); 46 seg[root].min = min(seg[lc].min, seg[rc].min); 47 } 48 inline void query(int root, int l, int r, int x, int y) { 49 if (x > r || y < l) return; 50 if (x <= l && y >= r) { 51 tmin = min(tmin, seg[root].min); 52 tmax = max(tmax, seg[root].max); 53 return; 54 } 55 query(lc, l, mid, x, y); 56 query(rc, mid + 1, r, x, y); 57 } 58 inline int query(int l, int r) { 59 tmin = INF, tmax = -INF; 60 query(1, 1, n, l, r); 61 return tmax - tmin; 62 } 63 }seg; 64 int main() { 65 #ifdef LOCAL 66 freopen("in.txt", "r", stdin); 67 freopen("out.txt", "w+", stdout); 68 #endif 69 int l, r, ans; 70 while (~scanf("%d %d %d", &n, &m, &k)) { 71 l = 1, ans = 0; 72 seg.built(1, 1, n); 73 rep(i, n) { 74 r = i; 75 if (l > r) continue; 76 while (seg.query(l, r) > k) l++; 77 if (seg.query(l, r) >= m && seg.query(l, r) <= k) ans = max(ans, r - l + 1); 78 } 79 printf("%d\n", ans); 80 } 81 return 0; 82 }