AC日记——Super Mario hdu 4417
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6122 Accepted Submission(s):
2660
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
Recommend
思路:
主席树+二分查找;
来,上代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define maxn 100005 using namespace std; struct TreeNodeType { int lc,rc,dis; }; struct TreeNodeType tree[maxn*30]; int if_z,t,n,m,num[maxn],Hash[maxn],size,cnt,root[maxn]; char Cget; inline void in(int &now) { now=0,if_z=1,Cget=getchar(); while(Cget>'9'||Cget<'0') { if(Cget=='-') if_z=-1; Cget=getchar(); } while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } now*=if_z; } void tree_build(int &now,int l,int r) { now=++cnt,tree[now].dis=0; if(l==r) return ; int mid=(l+r)>>1; tree_build(tree[now].lc,l,mid); tree_build(tree[now].rc,mid+1,r); } void tree_add(int pre,int &now,int to,int l,int r) { now=++cnt; tree[now].dis=tree[pre].dis+1; if(l==r) return ; int mid=(l+r)>>1; if(to<=mid) { tree_add(tree[pre].lc,tree[now].lc,to,l,mid); tree[now].rc=tree[pre].rc; } else { tree_add(tree[pre].rc,tree[now].rc,to,mid+1,r); tree[now].lc=tree[pre].lc; } } int find(int x) { int l=1,r=size,mid,ans; while(l<=r) { mid=(l+r)>>1; if(x<=Hash[mid]) ans=mid,r=mid-1; else l=mid+1; } return ans; } int tree_query(int pre,int now,int to,int l,int r) { if(l==r) return tree[now].dis-tree[pre].dis; int pos=tree[tree[now].lc].dis-tree[tree[pre].lc].dis; int mid=(l+r)>>1; if(to<=mid) return tree_query(tree[pre].lc,tree[now].lc,to,l,mid); else return pos+tree_query(tree[pre].rc,tree[now].rc,to,mid+1,r); } int main() { in(t); for(int Case=1;Case<=t;Case++) { in(n),in(m);cnt=0; for(int i=1;i<=n;i++) in(num[i]),Hash[i]=num[i]; sort(Hash+1,Hash+n+1); size=unique(Hash+1,Hash+n+1)-Hash-1; tree_build(root[0],1,size); for(int i=1;i<=n;i++) { num[i]=lower_bound(Hash+1,Hash+size+1,num[i])-Hash; tree_add(root[i-1],root[i],num[i],1,size); } printf("Case %d:\n",Case); int u,v,w; while(m--) { in(u),in(v),in(w); if(w<Hash[1]) { printf("0\n"); continue; } int pos=find(w); if(Hash[pos]>w) pos--; printf("%d\n",tree_query(root[u],root[v+1],pos,1,size)); } } return 0; }