题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=3530
Subsequence
Description
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.
Input
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.
Output
For each test case, print the length of the subsequence on a single line.
Sample Input
5 0 0
1 1 1 1 1
5 0 3
1 2 3 4 5
Sample Output
5
4
RMQ线段树。。
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::min; 10 using std::max; 11 #define sz(c) (int)(c).size() 12 #define all(c) (c).begin(), (c).end() 13 #define iter(c) decltype((c).begin()) 14 #define cls(arr,val) memset(arr,val,sizeof(arr)) 15 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 16 #define rep(i, n) for (int i = 1; i <= (int)(n); i++) 17 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 18 #define pb(e) push_back(e) 19 #define mp(a, b) make_pair(a, b) 20 #define mid ((l+r)>>1) 21 #define lc (root<<1) 22 #define rc (root<<1|1) 23 const int Max_N = 100010; 24 const int INF = 0x3f3f3f3f; 25 typedef unsigned long long ull; 26 int n, m, k, tmin, tmax; 27 struct SegTree { 28 struct Node { int max, min; }seg[Max_N << 2]; 29 inline void built(int root, int l, int r) { 30 if (l == r) { 31 scanf("%d", &seg[root].max), seg[root].min = seg[root].max; 32 return; 33 } 34 built(lc, l, mid); 35 built(rc, mid + 1, r); 36 seg[root].max = max(seg[lc].max, seg[rc].max); 37 seg[root].min = min(seg[lc].min, seg[rc].min); 38 } 39 inline void query(int root, int l, int r, int x, int y) { 40 if (x > r || y < l) return; 41 if (x <= l && y >= r) { 42 tmin = min(tmin, seg[root].min); 43 tmax = max(tmax, seg[root].max); 44 return; 45 } 46 query(lc, l, mid, x, y); 47 query(rc, mid + 1, r, x, y); 48 } 49 inline int query(int l, int r) { 50 tmin = INF, tmax = -INF; 51 query(1, 1, n, l, r); 52 return tmax - tmin; 53 } 54 }seg; 55 int main() { 56 #ifdef LOCAL 57 freopen("in.txt", "r", stdin); 58 freopen("out.txt", "w+", stdout); 59 #endif 60 int l, r, ans; 61 while (~scanf("%d %d %d", &n, &m, &k)) { 62 l = 1, ans = 0; 63 seg.built(1, 1, n); 64 rep(i, n) { 65 r = i; 66 if (l > r) continue; 67 while (seg.query(l, r) > k) l++; 68 if (seg.query(l, r) >= m && seg.query(l, r) <= k) ans = max(ans, r - l + 1); 69 } 70 printf("%d\n", ans); 71 } 72 return 0; 73 }
时间: 2024-10-11 21:44:10