题目链接:
Super Mario
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
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
题意:
给定一个数列.给区间[l,r]问此区间内<=k的数目是多少;
思路:
可以用树状数组离线处理,把原序列按从小到大排序,询问也按k的大小排序,然后一边询问一边update;最近学了划分树,也可以做这题,看明天有时间想一想怎么做吧;
AC代码:
/* 树状数组的代码: 4417 202MS 3744K 1873 B G++ 2014300227 */ #include <bits/stdc++.h> using namespace std; const int N=1e5+4; int a[N],n,m,sum[N],ans[N]; struct node { friend bool operator< (node x,node y) { if(x.num == y.num)return x.pos < y.pos; return x.num < y.num; } int l,r,pos,num; }; node po[N]; struct nod { friend bool operator< (nod x,nod y) { if(x.a==y.a)return x.pos<y.pos; return x.a<y.a; } int a,pos; }; nod p[N]; int lowbit(int x) { return x&(-x); } void update(int x) { while(x<=n) { sum[x]++; x+=lowbit(x); } } int query(int x) { int s=0; while(x>0) { s+=sum[x]; x-=lowbit(x); } return s; } int main() { int t; scanf("%d",&t); int cnt=1; while(t--) { memset(sum,0,sizeof(sum)); printf("Case %d: ",cnt++); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&p[i].a); p[i].pos=i; } sort(p+1,p+n+1); for(int i=0;i<m;i++) { scanf("%d%d%d",&po[i].l,&po[i].r,&po[i].num); po[i].l++; po[i].r++; po[i].pos=i; } sort(po,po+m); int fp=1; for(int i=0;i<m;i++) { if(p[fp].a<=po[i].num) { while(p[fp].a<=po[i].num&&fp<=n) { update(p[fp].pos); fp++; } } if(po[i].l==1)ans[po[i].pos]=query(po[i].r); else { ans[po[i].pos]=query(po[i].r)-query(po[i].l-1); } } for(int i=0;i<m;i++) { printf("%d ",ans[i]); } } return 0; }