|
| 1 | +# segtree |
| 2 | +```cpp |
| 3 | +#pragma GCC optimize("O3, unroll-loops") |
| 4 | +#include <bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +int T, N, a[3000001]{}, mn[8388608]{}, mx[8388608]{}, ans = 1; |
| 8 | + |
| 9 | +void init(int s, int e, int n) { |
| 10 | + if(s == e) { |
| 11 | + mn[n] = mx[n] = a[s]; |
| 12 | + return; |
| 13 | + } |
| 14 | + int m = (s+e)>>1; |
| 15 | + init(s,m,n<<1); init(m+1,e,(n<<1)|1); |
| 16 | + mn[n] = min(mn[n<<1], mn[(n<<1)|1]); |
| 17 | + mx[n] = max(mx[n<<1], mx[(n<<1)|1]); |
| 18 | +} |
| 19 | + |
| 20 | +int f(int s, int e, int l, int r, int n) { |
| 21 | + if(l>r || l>e || r<s) return 2e9; |
| 22 | + if(l<=s && e<=r) return mn[n]; |
| 23 | + int m = (s+e)>>1; |
| 24 | + return min(f(s,m,l,r,n<<1), f(m+1,e,l,r,(n<<1)|1)); |
| 25 | +} |
| 26 | + |
| 27 | +int g(int s, int e, int l, int r, int n) { |
| 28 | + if(l>r || l>e || r<s) return 0; |
| 29 | + if(l<=s && e<=r) return mx[n]; |
| 30 | + int m = (s+e)>>1; |
| 31 | + return max(g(s,m,l,r,n<<1), g(m+1,e,l,r,(n<<1)|1)); |
| 32 | +} |
| 33 | + |
| 34 | +int main(){ |
| 35 | + cin.tie(0)->sync_with_stdio(0); |
| 36 | + |
| 37 | + cin>>T>>N; |
| 38 | + for(int i=1;i<=N;i++) cin>>a[i]; |
| 39 | + init(1,N,1); |
| 40 | + |
| 41 | + for(int i=1,j=1;i<=N;i++) { |
| 42 | + while(j<i && g(1,N,j,i,1) - f(1,N,j,i,1) > T) j++; |
| 43 | + ans = max(ans, i-j+1); |
| 44 | + } |
| 45 | + cout<<ans; |
| 46 | + |
| 47 | +} |
| 48 | +``` |
| 49 | +
|
| 50 | +# deque |
| 51 | +```cpp |
| 52 | +#include <bits/stdc++.h> |
| 53 | +using namespace std; |
| 54 | +
|
| 55 | +int T, N, ans = 1; |
| 56 | +deque<pair<int, int>> mx, mn; |
| 57 | +
|
| 58 | +int main(){ |
| 59 | + cin.tie(0)->sync_with_stdio(0); |
| 60 | + |
| 61 | + cin>>T>>N; |
| 62 | + for(int i=1,j=1,a;i<=N;i++) { |
| 63 | + cin>>a; |
| 64 | + while(!mn.empty() && mn.back().first > a) mn.pop_back(); |
| 65 | + mn.emplace_back(a,i); |
| 66 | + while(!mx.empty() && mx.back().first < a) mx.pop_back(); |
| 67 | + mx.emplace_back(a,i); |
| 68 | + while(j<i && mx.front().first - mn.front().first > T) { |
| 69 | + j++; |
| 70 | + while(mx.front().second < j) mx.pop_front(); |
| 71 | + while(mn.front().second < j) mn.pop_front(); |
| 72 | + } |
| 73 | + ans = max(ans, i-j+1); |
| 74 | + } |
| 75 | + cout<<ans; |
| 76 | +
|
| 77 | +} |
| 78 | +``` |
0 commit comments