"Enumeration" "Weights chunked" bzoj1112 [POI2008] Bricks Klo

Source: Internet
Author: User

Enumerates all segments with a length of M, and attempts to update the answer with the median.

So need data structure, support query K large, and greater than/less than k large value of the number and.

Balance tree, Weight line segment tree, weight value of the block or whatever.

1#include <cstdio>2#include <algorithm>3#include <cmath>4 using namespacestd;5typedefLong Longll;6 structpoint{intV,p;} t[100001];7 BOOL operator< (ConstPoint &a,ConstPoint &b) {returna.v<B.V;}8 intsumv[ -],ma[100001],en,a[100001],b[100001],tot[ -],l[ -],r[ -],num[100001],sum=1, k,n,m;9ll ans=999999999999999999, TAns;Ten voidMakeblock () One { A     intSz=sqrt (en);if(!SZ) sz=1; -      for(; sum*sz<en;++sum) -       { thel[sum]=r[sum-1]+1; r[sum]=sum*sz; -            for(intI=l[sum];i<=r[sum];++i) num[i]=sum; -       } -l[sum]=r[sum-1]+1; r[sum]=en; +      for(intI=l[sum];i<=r[sum];++i) num[i]=sum; - } + voidInsert (Const int&AMP;X) {++b[x]; ++tot[num[x]]; sumv[num[x]]+=(LL) ma[x];} A voidDelete (Const int&AMP;X) {--b[x];--tot[num[x]]; sumv[num[x]]-=(LL) ma[x];} at intQuery (Const int&x) - { -     intCnt=0; ll now=0; -      for(intI=1;; i++) -       { -Cnt+=tot[i]; now+=Sumv[i]; in         if(cnt>=x) -           { toCnt-=tot[i]; now-=Sumv[i]; +              for(intJ=l[i];; J + +) -               { theCNT+=B[J]; now+= (LL) ma[j]*(LL) b[j]; *                 if(cnt>=x) $                   {Panax NotoginsengCNT-=B[J]; now-= (LL) ma[j]*(LL) b[j]; -tans= (LL) ma[j]* (LL) cnt-Now ); the                       returnJ; +                   } A               } the           } +       } - } $ voidNext_sum (Const int&x) $ { -     intCnt=0; ll now=0; -      for(inti=x+1; i<=r[num[x]];++i) {cnt+=b[i]; now+= (LL) ma[i]*(LL) b[i];} the      for(inti=num[x]+1; i<=sum;++i) {cnt+=tot[i]; now+=sumv[i];} -tans+= (now-(LL) ma[x]*(LL) CNT);Wuyi } the intMain () - { Wuscanf"%d%d", &n,&m); k= (m>>1)+1; -      for(intI=1; i<=n;++i) About       { $scanf"%d",&t[i].v); -t[i].p=i; -} sort (t+1, t+n+1); -ma[a[t[1].p]=++en]=t[1].v; A      for(intI=2; i<=n;++i) +       { the           if(t[i].v!=t[i-1].V) + +en; -ma[a[t[i].p]=en]=t[i].v; $ } makeblock (); the      for(intI=1; i<=m;++i) Insert (A[i]); the     intT=query (K); Next_sum (t); ans=min (ans,tans); the      for(inti=m+1; i<=n;++i) the       { -Delete (a[i-m]); Insert (A[i]); in           intt=Query (K); the next_sum (t); theans=min (ans,tans); About} printf ("%lld\n", ans); the     return 0; the}

"Enumeration" "Weights chunked" bzoj1112 [POI2008] Bricks Klo

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.