Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6018 Accepted Submission(s): 2620
Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping
ability reminded in our memory. Now the poor princess is in trouble again
and Mario needs to save his lover. We regard the road to the boss’s castle
as a line (the length is n), on every integer point i there is a brick on
height hi. Now the question is how many bricks in [L, R] Mario can hit if
the maximal height he can jump is H.
Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
Output
For each case, output "Case X: " (X is the case number starting from 1)
followed by m lines, each line contains an integer. The ith integer is the
number of bricks Mario can hit for the ith query.
Sample Input
1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
Sample Output
Case 1: 4 0 0 3 1 2 0 1 5 1
Source
题意:给一些数,数中有重复的。还有一些询问,问的是[L,R] 区间内有多少个数小于h,有多次询问。
思路:普通方法的话肯定会超时,题目问[L,R]区间内小于h的数有多少个,则可以算出cal(R) 和 cal(L - 1), 两者相减就是答案。这类问题和求逆序数的问题非常类似,即求一个数前面有几个数比它小。求逆序数的问题可以用树状数组解决。
然而我不想用树状数组写。据说是主席树的裸题,
然后就这样了
#include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; const int N=1e5+10; const int M=N*20; int n,m,T,sum[M],ls[M],rs[M]; int root[N],a[N],b[N],sz; map<int,int>ys; void build(int &k,int l,int r){ k=++sz; sum[k]=0; if(l==r) return ; int mid=l+r>>1; build(ls[k],l,mid); build(rs[k],mid+1,r); } void insert(int &k,int last,int l,int r,int pos,int val){ k=++sz; ls[k]=ls[last]; rs[k]=rs[last]; sum[k]=sum[last]+val; if(l==r) return ; int mid=l+r>>1; if(pos<=mid) insert(ls[k],ls[last],l,mid,pos,val); else insert(rs[k],rs[last],mid+1,r,pos,val); } int query(int x,int y,int l,int r,int pos){ int mid=l+r>>1; if(l==r) return sum[y]-sum[x]; else if(pos<=mid) return query(ls[x],ls[y],l,mid,pos); else return sum[ls[y]]-sum[ls[x]]+query(rs[x],rs[y],mid+1,r,pos); } int main(){ int T,cas=1; for(scanf("%d",&T);cas<=T;cas++){ printf("Case %d: ",cas); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+n+1); int cnt=unique(b+1,b+n+1)-(b+1); for(int i=1;i<=cnt;i++) ys[b[i]]=i; sz=0;build(root[0],1,cnt); for(int i=1;i<=n;i++) insert(root[i],root[i-1],1,cnt,ys[a[i]],1); for(int i=1,l,r,h;i<=m;i++){ scanf("%d%d%d",&l,&r,&h);l++;r++; int k=upper_bound(b+1,b+cnt+1,h)-(b+1); if(k) printf("%d ",query(root[l-1],root[r],1,cnt,k)); else puts("0"); } } return 0; }
/*分块TLE(因为有T组数据) 不完整的块枚举统计即可; 而要在每个整块内寻找小于一个值的元素数,于是我们不得不要求块内元素是有序的,这样就能使用二分法对块内查询,需要预处理时每块做一遍排序,复杂度O(nlogn), 每次查询在√n个块内二分,以及暴力2√n个元素, 总复杂度O(nlogn + n√nlog√n)。 可以通过均值不等式计算出更优的分块大小,QAQ不会~~ */ #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int F=501; int n,q,cas,T,cnt,C[F],block[F][F]; int main(){ memset(block,0x3f3f3f3f,sizeof block); for(scanf("%d",&T);T--;){ scanf("%d%d",&n,&q); int m=sqrt(n)+1;printf("Case %d: ",++cas); for(int i=0;i<n;i++) scanf("%d",&block[i/m][i%m]); for(int opt,x,y,z,b1,b2,p1,p2;q--;){ scanf("%d%d%d",&x,&y,&z);cnt=0; b1=x/m;b2=y/m; p1=x%m;p2=y%m; if(b1==b2){ for(int j=p1;j<=p2;j++) if(block[b1][j]<=z) cnt++; } else{ for(int j=p1;j<m;j++) if(block[b1][j]<=z) cnt++; for(int j=b1+1;j<b2;j++){ for(int k=0;k<m;k++) C[k]=block[j][k]; sort(C,C+m); cnt+=upper_bound(C,C+m,z)-C; } for(int j=0;j<=p2;j++) if(block[b2][j]<=z) cnt++; } printf("%d ",cnt); } } return 0; }