HDU 3473 Minimum Sum partition tree

Source: Internet
Author: User

Given a sequence, each time we ask for an interval, We need to select a number. The sum of the distances of all numbers in the interval is the smallest, and the sum of the minimum and

From the absolute value inequality, we can see that when the number we choose is the median, the distance and the minimum are converted to the K smaller range.

However, this question is the minimum sum, so we make a processing, and we maintain an additional sum domain sum [I] representation.[L, I] sum of elements divided into left subtree within the interval

Then, every time we query the K hour, if we enter the right subtree, we divide the elements in the left subtree and accumulate them to left_sum.

Then, use the prefix and calculated Range Sum to calculate the right_sum.

The final result is K * mid_num-left_sum + right_sum-(Y-x + 1-k) * mid_num = right_sum-left_sum + (y-x-1-k-k) * mid_num

Observe the right formula:

Y-x-1 = 2 * K when the Interval Length is an even number

Y-x-1 = 2 * K-1 when the Interval Length is odd

The right formula can be reduced:

The right_sum-left_sum + (Y + x + 1 & 1) * mid_num

In addition, the bad thing is that C ++ and G ++ of HDU are different from those of poj, so they all need to use % i64d for output, because it is really cool to proofread this wa for a long time.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 100100using namespace std;typedef long long ll;int n,m,cnt,a[M],b[M],c[M];int s[20][M];ll pre_sum[M],sum[20][M],left_sum,right_sum,ans;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[l1++]=a[i];s[dpt][i]=(i==l?1:s[dpt][i-1]+1);sum[dpt][i]=(i==l?a[i]:sum[dpt][i-1]+a[i]);left-=(a[i]==c[mid]);}else{b[l2++]=a[i];s[dpt][i]=(i==l?0:s[dpt][i-1]);sum[dpt][i]=(i==l?0:sum[dpt][i-1]);}}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 i,mid=l+r>>1;int l1=(x==l?0:s[dpt][x-1]),l2=s[dpt][y];if(l==r){left_sum+=a[mid];return a[mid];}if(k<=l2-l1)return Get_Ans(l,mid,dpt+1,l+l1,l+l2-1,k);else{left_sum+=sum[dpt][y]-(x==l?0:sum[dpt][x-1]);return Get_Ans(mid+1,r,dpt+1,(mid+1)+(x-l-l1),(mid+1)+(y-l+1-l2)-1,k-l2+l1);}}int main(){//freopen("1.txt","w",stdout);int T,i,x,y,k;for(cin>>T;T;T--){printf("Case #%d:\n",++cnt);cin>>n;for(i=1;i<=n;i++)scanf("%d",&a[i]),c[i]=a[i],pre_sum[i]=pre_sum[i-1]+a[i];sort(c+1,c+n+1);Build_Tree(1,n,0);cin>>m;for(i=1;i<=m;i++){scanf("%d%d",&x,&y);x++;y++;k=y-x+2>>1;left_sum=0;ll mid_num=Get_Ans(1,n,0,x,y,k);right_sum=pre_sum[y]-pre_sum[x-1]-left_sum;ans=right_sum-left_sum+(x+y+1&1)*mid_num;//ans=k*mid_num-left_sum+right_sum-(y-x+1-k)*mid_num;printf("%I64d\n",ans);}putchar('\n');}}


HDU 3473 Minimum Sum partition tree

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.