HDU 4417 Super Mario tree/tree Array

Source: Internet
Author: User

Given a sequence, evaluate the number of elements in the interval smaller than or equal to a certain number.

First, the ranking of the intervals is that the decision tree is not the same as the K. We need to do some processing.

The first method is to divide the second answer and convert it to the k-th. I like it, but this is not the method mentioned here.

First, we will discuss the relationship between K and A [Mid] for each query.

If K <A [Mid], the right subtree must not be smaller than or equal to K. We will go to the left subtree to find

If K> = A [Mid], then all the numbers in the left subtree are smaller than or equal to K. Therefore, the number of elements entering the left subtree in the query interval is recorded in ans, then find the right range

The recursive exit condition is that the query interval is null or the node is a leaf node.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 100100using namespace std;int n,m,cnt,a[M],b[M],c[M];int s[20][M];void Build_Tree(int l,int r,int dpt){int i,mid=l+r>>1;int l1=l,l2=mid+1;int left=mid-l+1;if(l==r)return ;for(i=l;i<=r;i++)left-=(a[i]<c[mid]);for(i=l;i<=r;i++){if(a[i]>c[mid]||a[i]==c[mid]&&!left)b[l2++]=a[i],s[dpt][i]=(i==l?0:s[dpt][i-1]  );elseb[l1++]=a[i],s[dpt][i]=(i==l?1:s[dpt][i-1]+1),left-=(a[i]==c[mid]);}memcpy( a+l , b+l , sizeof(a[0])*(r-l+1) );Build_Tree(l,mid,dpt+1);Build_Tree(mid+1,r,dpt+1);}int Get_Ans(int l,int r,int dpt,int x,int y,int k){int mid=l+r>>1;int l1=(x==l?0:s[dpt][x-1]),l2=s[dpt][y];if(x>y)return 0;if(l==r)return a[mid]<=k;if(k<c[mid])return Get_Ans(l,mid,dpt+1,l+l1,l+l2-1,k);elsereturn l2-l1+Get_Ans(mid+1,r,dpt+1,(mid+1)+(x-l-l1),(mid+1)+(y-l+1-l2)-1,k);}int main(){int T,i,x,y,k;for(cin>>T;T;T--){printf("Case %d:\n",++cnt);cin>>n>>m;for(i=1;i<=n;i++)scanf("%d",&a[i]),c[i]=a[i];sort(c+1,c+n+1);Build_Tree(1,n,0);for(i=1;i<=m;i++)scanf("%d%d%d",&x,&y,&k),printf("%d\n", Get_Ans(1,n,0,x+1,y+1,k) );}}

Later, I looked at the problem and found that this tree array can also be used... Offline thinking: otz first

We sort the query by the value of the query, for each value, we insert the element location less than or equal to this value into the tree array, then query (x-1, y] The number of elements in the interval.

After writing, the number of decision trees in 140 ms is 109 Ms. The constant for sorting two times is a little larger than 0.0.

If you change memset to a time mark repeatedly, it will be faster than 125 Ms.

And don't forget to output case 1... because this p event also contributed a wa

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 100100using namespace std;struct abcd{int x,y,num,pos;bool operator < (const abcd &x)const{return num < x.num;}}q[M];pair<int,int>a[M];int n,m,cnt,ans[M];int c[M],tim[M];void update(int x){for(;x<=n;x+=x&-x){if(tim[x]!=cnt)c[x]=0;tim[x]=cnt;c[x]++;}}int getans(int x){int re=0;for(;x;x-=x&-x)if(tim[x]==cnt)re+=c[x];return re;}int main(){int T,i,j,x,y,k;for(cin>>T;T;T--){printf("Case %d:\n",++cnt);cin>>n>>m;for(i=1;i<=n;i++)scanf("%d",&a[i].first),a[i].second=i;sort(a+1,a+n+1);for(i=1;i<=m;i++)scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].num),q[i].pos=i;sort(q+1,q+m+1);j=1;for(i=1;i<=m;i++){for(;j<=n&&a[j].first<=q[i].num;j++)update(a[j].second);ans[q[i].pos]=getans(q[i].y+1)-getans(q[i].x);}for(i=1;i<=m;i++)printf("%d\n",ans[i]);}}


HDU 4417 Super Mario tree/tree Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.