Question: There are several numbers in the range of the function-based line segment tree that are less than or equal to K. in offline practice, we first separate all the queries and sequences, and then use the function-based line segment tree for processing.
#include <map> #include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=200005;const int MAXN=3000005;int n,m,tot,data[MAXN],son[MAXN][2],head[MAXN];map<int,int>M;int b[N],a[N],l[N],r[N],k[N];int make(int l,int r){ int t=++tot,mid=(l+r)>>1; if(l==r)return t; son[t][0]=make(l,mid);son[t][1]=make(mid+1,r); return t;}int insert(int x,int pos,int l,int r){ int t=++tot,mid=(l+r)>>1; data[t]=data[pos]+1; if(l==r)return t; if(x<=mid)son[t][1]=son[pos][1],son[t][0]=insert(x,son[pos][0],l,mid); else{son[t][0]=son[pos][0];son[t][1]=insert(x,son[pos][1],mid+1,r);} return t;}int query(int x,int y,int k,int l,int r){ int mid=(l+r)>>1; if(l==r) return 0; if(k>=mid)return(query(son[x][1],son[y][1],k,mid+1,r)+data[son[y][0]]-data[son[x][0]]); else return query(son[x][0],son[y][0],k,l,mid);}int main(){ int T,cnt=1; scanf("%d",&T); while(T--){ int s=0,t=0; M.clear(); tot=0; printf("Case %d:\n",cnt++); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){scanf("%d",&a[i]);b[++s]=a[i];} for(int i=1;i<=m;i++){scanf("%d%d%d",&l[i],&r[i],&k[i]);l[i]++,r[i]++;b[++s]=k[i];} sort(b+1,b+s+1); for(int i=1;i<=s;i++)if(!M[b[i]])M[b[i]]=++t; for(int i=1;i<=n;i++)a[i]=M[a[i]]; for(int i=1;i<=m;i++)k[i]=M[k[i]]; head[0]=make(1,t); for(int i=1;i<=n;i++)head[i]=insert(a[i],head[i-1],1,t); for(int i=1;i<=m;i++)printf("%d\n",query(head[l[i]-1],head[r[i]],k[i],1,t)); }return 0;}
HDU 4417 Super Mario