hdu-4417-主席树+离线

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9118    Accepted Submission(s): 3840


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.)
 
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的数的个数,用第R棵树的query()减去第L-1棵树的query()就是答案了。
WA了几发是因为离散化的问题,我只是离散化了ai却忘记了hi,离线把询问的数值和ai一起离散化了,
还要记住这样的话根节点的区间就是[1,N+Q]了。
  
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long 
 4 #define inf 0x3f3f3f3f
 5 #define pb push_back
 6 #define mid ((L+R)>>1)
 7 const int maxn=100010;
 8 int a[maxn],root[maxn],N,tot;
 9 int l[maxn],r[maxn],h[maxn];
10 vector<int>vi;
11 struct node{int lc,rc,sum;}T[maxn*40];
12 int getid(int x){return lower_bound(vi.begin(),vi.end(),x)-vi.begin()+1;}
13 void update(int &x,int y,int L,int R,int d){
14     T[++tot]=T[y],T[tot].sum++,x=tot;
15     if(L==R)return;
16     if(d<=mid) update(T[x].lc,T[y].lc,L,mid,d);
17     else update(T[x].rc,T[y].rc,mid+1,R,d);
18 }
19 int query(int x,int L,int R,int d){
20      if(L==R) return T[x].sum;
21      if(d<=mid) return query(T[x].lc,L,mid,d);
22      else return T[T[x].lc].sum+query(T[x].rc,mid+1,R,d);
23 }
24 int main(){
25     int t,q,cas=0;
26     cin>>t;
27     while(t--){
28         tot=0;
29         vi.clear();
30         scanf("%d%d",&N,&q);
31         for(int i=1;i<=N;++i) scanf("%d",a+i),vi.pb(a[i]);
32         printf("Case %d:\n",++cas);
33         for(int i=1;i<=q;++i){
34             scanf("%d%d%d",l+i,r+i,h+i);
35             vi.pb(h[i]);
36         }
37         sort(vi.begin(),vi.end()),vi.erase(unique(vi.begin(), vi.end()),vi.end());
38         for(int i=1;i<=N;++i) update(root[i],root[i-1],1,N+q,getid(a[i]));
39         for(int i=1;i<=q;++i) h[i]=getid(h[i]),printf("%d\n",query(root[r[i]+1],1,N+q,h[i])-query(root[l[i]],1,N+q,h[i]));
40     }
41     return 0;
42 }
 

猜你喜欢

转载自www.cnblogs.com/zzqc/p/9381408.html
今日推荐