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
题解:
主席树模板题...
1 //Never forget why you start 2 #include<iostream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<cmath> 7 #include<algorithm> 8 #define ll(x) seg[x].l 9 #define rr(x) seg[x].r 10 using namespace std; 11 int n,m,t,b[100005],mmax; 12 struct node{ 13 int x,pos,cnt; 14 friend bool operator < (const node a,const node b){ 15 return a.x<b.x; 16 } 17 }a[100005]; 18 bool cmp(const node a,const node b){ 19 return a.pos<b.pos; 20 } 21 int root[100005],cnt=0; 22 struct Seg{ 23 int l,r,sum; 24 }seg[10000005]; 25 int newnode(int root){ 26 cnt++; 27 seg[cnt]=seg[root]; 28 return cnt; 29 } 30 void push_up(int root){ 31 seg[root].sum=seg[ll(root)].sum+seg[rr(root)].sum; 32 } 33 void insert(int &root,int l,int r,int x){ 34 root=newnode(root); 35 if(l==r){seg[root].sum++;return;} 36 int mid=(l+r)>>1; 37 if(x<=mid)insert(ll(root),l,mid,x); 38 if(mid<x)insert(rr(root),mid+1,r,x); 39 push_up(root); 40 } 41 int query(int lroot,int rroot,int left,int right,int l,int r){ 42 if(l<=b[left]&&b[right]<=r) 43 return seg[rroot].sum-seg[lroot].sum; 44 if(l>b[right]||r<b[left])return 0; 45 int mid=(left+right)>>1,ans=0; 46 if(l<=b[mid])ans+=query(ll(lroot),ll(rroot),left,mid,l,r); 47 if(b[mid]<r)ans+=query(rr(lroot),rr(rroot),mid+1,right,l,r); 48 return ans; 49 } 50 int main(){ 51 int i,j; 52 scanf("%d",&t); 53 for(j=1;j<=t;j++){ 54 scanf("%d%d",&n,&m); 55 for(i=1;i<=n;i++){ 56 scanf("%d",&a[i].x); 57 a[i].pos=i; 58 } 59 sort(a+1,a+n+1); 60 a[1].cnt=1; 61 b[a[1].cnt]=a[1].x; 62 for(i=2;i<=n;i++){ 63 a[i].cnt=a[i-1].cnt+(a[i].x!=a[i-1].x); 64 b[a[i].cnt]=a[i].x; 65 } 66 mmax=a[n].cnt;; 67 sort(a+1,a+n+1,cmp); 68 root[0]=0;cnt=0; 69 for(i=1;i<=n;i++){ 70 root[i]=root[i-1]; 71 insert(root[i],1,mmax,a[i].cnt); 72 } 73 printf("Case %d:\n",j); 74 for(i=1;i<=m;i++){ 75 int u,v,l; 76 scanf("%d%d%d",&u,&v,&l); 77 u++;v++; 78 printf("%d\n",query(root[u-1],root[v],1,mmax,0,l)); 79 } 80 } 81 return 0; 82 }