【HDU 4417】Super Mario 【主席樹】
阿新 • • 發佈:2019-01-25
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cctype> using namespace std; char inc; inline void get(int& x) { x = 0;inc =getchar(); while(!isdigit(inc))inc=getchar(); while(isdigit(inc)) { x=x*10+inc-'0'; inc=getchar(); } } const int maxn = 100010; int T,casecnt,n,m; int a[maxn],hashrank[maxn],tot; int getrank(int x){ return lower_bound(hashrank+1,hashrank+1+tot,x)-hashrank;} int x,y,h,root[maxn],cnt; struct Node{ int ls,rs; int sum; }tr[maxn*40]; void update(int& u,int v,int l,int r,int p) { ++cnt;tr[cnt]=tr[v];tr[cnt].sum++;u=cnt; if(l==r)return; int mid = (l+r)>>1; if(p<=mid)update(tr[u].ls,tr[v].ls,l,mid,p); else update(tr[u].rs,tr[v].rs,mid+1,r,p); } int query(int x,int y,int l,int r) { if(l==r)return tr[y].sum-tr[x].sum; int mid = (l+r)>>1; if(h<=mid)return query(tr[x].ls,tr[y].ls,l,mid); else return query(tr[x].rs,tr[y].rs,mid+1,r)+tr[tr[y].ls].sum-tr[tr[x].ls].sum; } int main() { get(T); while(T--) { cnt = 0; get(n);get(m); for(int i=1;i<=n;i++)get(a[i]),hashrank[i]=a[i]; sort(hashrank+1,hashrank+1+n); tot = unique(hashrank+1,hashrank+1+n)-hashrank-1; for(int i=1;i<=n;i++)a[i]=getrank(a[i]); for(int i=1;i<=n;i++)update(root[i],root[i-1],1,tot,a[i]); printf("Case %d:\n",++casecnt); for(int i=1;i<=m;i++) { get(x);get(y);get(h); x++,y++; h = upper_bound(hashrank+1,hashrank+1+tot,h)-hashrank-1; if(h==0)puts("0"); else printf("%d\n",query(root[x-1],root[y],1,tot)); } } return 0; }