1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #pragma GCC optimize(3,"Ofast","inline") #pragma comment(linker, "/STACK:1024000000,1024000000") #include<bits/stdc++.h> using namespace std; #define endl '\n' typedef long long ll; #define int long long #define ld long double const int N=100010; struct node{ int l,r,sum; }; node tr[N*4]; int n,m,k,a[100010]; int pre1[N]; int pre2[N]; void build(int u,int l,int r){ tr[u]={l,r,0}; if(l==r) { tr[u].sum=0;return; } int mid=(l+r)/2; build(u*2,l,mid); build(u*2+1,mid+1,r); } int query(int u,int l,int r){ if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum; int mid=(tr[u].l+tr[u].r)/2; int ti=0; if(mid>=l) ti=query(u*2,l,r); if(mid+1<=r) ti=max(ti,query(u*2+1,l,r)); return ti; } void change(int u,int id,int c){ if(tr[u].l==id&&tr[u].r==id) { tr[u].sum=c; return; } int mid=(tr[u].l+tr[u].r)/2; if(mid>=id) change(u*2,id,c); else if(mid+1<=id) change(u*2+1,id,c); tr[u].sum=max(tr[u*2].sum,tr[u*2+1].sum); } void solve() { vector<int>q; cin>>n>>m>>k; for(int i=1;i<=n;i++){ cin>>a[i]; q.push_back(a[i]); } q.push_back(1e17); sort(q.begin(),q.end()); q.erase(unique(q.begin(),q.end()),q.end()); int len=q.size(); build(1,1,len); for(int i=1;i<=n;i++){ int L=a[i]-k,R=a[i]+k; int pos1=lower_bound(q.begin(),q.end(),L)-q.begin(); int pos2=upper_bound(q.begin(),q.end(),R)-q.begin(); int id=lower_bound(q.begin(),q.end(),a[i])-q.begin(); id++,pos1++; pos1=min(pos1,len); pos2=min(pos2,len); int t1=query(1,pos1,id); int t2=query(1,id,pos2); pre1[i]=t1; pre2[i]=t2; change(1,id,i); } int x;cin>>x; vector<int>dp(n+10); while(x--){ int l,r;cin>>l>>r; for(int i=l;i<=r;i++) dp[i]=0; int v=0; for(int i=l;i<=r;i++){ dp[i]=1; if(pre1[i]>=l) { dp[i]=max(dp[i],dp[pre1[i]]+1); } if(pre2[i]>=l) { dp[i]=max(dp[i],dp[pre2[i]]+1); } v=max(v,dp[i]); } v=(r-l+1)-v; cout<<v<<endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int t; cin>>t; while(t--) { solve(); } return 0; }
|