日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 4417 Super Mario 树状数组||主席树

發(fā)布時間:2024/10/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 4417 Super Mario 树状数组||主席树 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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.)

?

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、 題意:n個數(shù),m個詢問 求區(qū)間l,r小于等于h的數(shù)目; 思路:主席樹求法,單點更新,區(qū)間查詢; #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; struct Chairmantree {int rt[N*20],ls[N*20],rs[N*20],sum[N*20];int tot;void init(){tot=0;}void build(int l,int r,int &pos){pos=++tot;sum[pos]=0;if(l==r)return;int mid=(l+r)>>1;build(l,mid,ls[pos]);build(mid+1,r,rs[pos]);}void update(int p,int c,int pre,int l,int r,int &pos){pos=++tot;ls[pos]=ls[pre];rs[pos]=rs[pre];sum[pos]=sum[pre]+c;if(l==r)return;int mid=(l+r)>>1;if(p<=mid)update(p,c,ls[pre],l,mid,ls[pos]);elseupdate(p,c,rs[pre],mid+1,r,rs[pos]);}int query(int s,int t,int L,int R,int l,int r){if(L<=l&&r<=R)return sum[t]-sum[s];int mid=(l+r)>>1;int ans=0;if(L<=mid)ans+=query(ls[s],ls[t],L,R,l,mid);if(R>mid)ans+=query(rs[s],rs[t],L,R,mid+1,r);return ans;} }; Chairmantree tree; int a[N],b[N<<2]; int l[N],r[N],x[N]; int getpos(int x,int cnt) {int pos=lower_bound(b+1,b+cnt,x)-b;return pos; } int main() {int T,cas=1;scanf("%d",&T);while(T--){int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}//for(int i=1;i<num;i++)// printf("%d ",tree.rt[i]);//printf("\n");for(int i=1;i<=m;i++){scanf("%d%d%d",&l[i],&r[i],&x[i]);b[i+n]=x[i];}sort(b+1,b+1+n+m);int num=unique(b+1,b+1+n+m)-b;tree.init();tree.build(1,num-1,tree.rt[0]);for(int i=1;i<=n;i++){int p=getpos(a[i],num);tree.update(p,1,tree.rt[i-1],1,num-1,tree.rt[i]);}printf("Case %d:\n",cas++);for(int i=1;i<=m;i++){int p=getpos(x[i],num);printf("%d\n",tree.query(tree.rt[l[i]],tree.rt[r[i]+1],1,p,1,num-1));}}return 0; }

樹狀數(shù)組求法:

  按照值的大小排序;

  標記區(qū)間的下標,詳見代碼;

#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<" "<<x<<endl; const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; int n,m; struct treearray {int tree[N];void init(){memset(tree,0,sizeof(tree));}int lowbit(int x){return x&(-x);}void update(int x,int c){while(x<N){tree[x]+=c;x+=lowbit(x);}}int query(int x){int sum=0;while(x){sum+=tree[x];x-=lowbit(x);}return sum;} }treearray; struct a {int x,pos;bool operator <(const a b)const{return x<b.x;} }a[N]; struct q {int l,r,pos,h;bool operator <(const q b)const{return h<b.h;} }q[N]; int ans[N]; int main() {int T,cas=1;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d",&a[i].x),a[i].pos=i;printf("Case %d:\n",cas++);for(int i=1;i<=m;i++)scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h),q[i].pos=i;sort(q+1,q+1+m);sort(a+1,a+1+n);treearray.init();int st=1;for(int i=1;i<=m;i++){//bug(q[i].pos);while(st<=n&&a[st].x<=q[i].h){//cout<<a[st].pos<<endl;treearray.update(a[st].pos,1);st++;}ans[q[i].pos]=treearray.query(q[i].r+1)-treearray.query(q[i].l);}for(int i=1;i<=m;i++)printf("%d\n",ans[i]);}return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/jhz033/p/6479825.html

總結(jié)

以上是生活随笔為你收集整理的hdu 4417 Super Mario 树状数组||主席树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。