HDU4417 Super Mario【主席樹】
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9501 Accepted Submission(s): 4017
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
2012 ACM/ICPC Asia Regional Hangzhou Online
題目連線:HDU4417 Super Mario
題解:求靜態區間[l,r]中小於等於k的數的數量,主席樹模板題。注意,題目中區間從0開始編號
AC的C++程式碼:
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int a[N],b[N],rt[N*20],ls[N*20],rs[N*20],sum[N*20];
int id;
void build(int &o,int l,int r)
{
o=++id;
sum[o]=0;
if(l==r) return;
int m=(l+r)>>1;
build(ls[o],l,m);
build(rs[o],m+1,r);
}
void update(int &o,int l,int r,int last,int p)
{
o=++id;
ls[o]=ls[last];
rs[o]=rs[last];
sum[o]=sum[last]+1;
if(l==r) return;
int m=(l+r)>>1;
if(p<=m)
update(ls[o],l,m,ls[last],p);
else
update(rs[o],m+1,r,rs[last],p);
}
//查詢區間[L,R]內小於等於k的個數
int query(int s,int e,int l,int r,int k)
{
if(b[r]<=k)
return sum[e]-sum[s];
if(l==r)
return 0;
int m=(l+r)>>1;
int sum=0;
if(b[m+1]<=k)
sum+=query(rs[s],rs[e],m+1,r,k);
sum+=query(ls[s],ls[e],l,m,k);
return sum;
}
int main()
{
int T,n,q,l,r,k;
scanf("%d",&T);
for(int t=1;t<=T;t++){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int size=unique(b+1,b+1+n)-(b+1);
id=0;
build(rt[0],1,size);
for(int i=1;i<=n;i++){
a[i]=lower_bound(b+1,b+1+size,a[i])-b;
update(rt[i],1,size,rt[i-1],a[i]);
}
printf("Case %d:\n",t);
while(q--){
scanf("%d%d%d",&l,&r,&k);
l++,r++; //題中是從0開始編號的,因此使它加一從1開始編號
int num=query(rt[l-1],rt[r],1,size,k);
printf("%d\n",num);
}
}
return 0;
}
相關文章
- hdu--4417Super Mario+劃分樹
- 主席樹
- 主席樹模板
- 靜態主席樹模板
- 動態主席樹模板
- kali滲透綜合靶機(七)--Super-Mario-Host靶機
- HDU2665 Kth number【主席樹】
- BZOJ4299: Codechef FRBSUM(主席樹)
- D-query SPOJ - DQUERY (主席樹)
- 【資料結構】淺談主席樹資料結構
- 洛谷P4197 Peaks(Kruskal重構樹 主席樹)
- bzoj3524: [Poi2014]Couriers(主席樹)
- HDU-4348 - To the moon (主席樹+區間修改)
- 【主席數】可持續化線段樹
- SDOI2018 原題識別(主席樹)
- 演算法隨筆——主席樹(可持久化線段樹)演算法持久化
- 【主席樹】P3919 【模板】可持久化線段樹 1持久化
- bzoj3545: [ONTAK2010]Peaks(主席樹+最小生成樹)
- bzoj4477: [Jsoi2015]字串樹(主席樹+Hash+Lca)JS字串
- bzoj3439: Kpm的MC密碼(主席樹+DFS序+字典樹)密碼
- bzoj2809: [Apio2012]dispatching(DFS序+主席樹)API
- 求區間不同數的個數【主席樹求解】
- bzoj5178: [Jsoi2011]棒棒糖(主席樹)JS
- bzoj1112: [POI2008]磚塊Klo(主席樹)
- bzoj2588: Spoj 10628. Count on a tree(主席樹+LCA)
- 2024年3月21日 懸繩法 + 珂朵莉樹(ODT) + 主席樹
- 可持久化線段————主席樹(洛谷p3834)持久化
- bzoj3110: [Zjoi2013]K大數查詢(主席樹+樹狀陣列)陣列
- [8] UE C++ MarioC++
- 洛谷 P3919 可持久化線段樹 1 之主席樹模板(初級)持久化
- bzoj1146: [CTSC2008]網路管理Network(dfs序+主席樹+樹狀陣列)陣列
- bzoj1803: Spoj1487 Query on a tree III(DFS序+主席樹)
- bzoj4448: [Scoi2015]情報傳遞(主席樹+Lca)
- bzoj5177: [Jsoi2013]貪心的導遊(主席樹)JS
- bzoj3207: 花神的嘲諷計劃Ⅰ(hash+主席樹)
- bzoj2733: [HNOI2012]永無鄉(並查集+主席樹)並查集
- bzoj3932: [CQOI2015]任務查詢系統(主席樹)
- bzoj3123: [Sdoi2013]森林(主席樹+Lca+啟發式合併)