"Tree-like array" "Weight-value chunking" bzoj2352 Stars

Source: Internet
Author: User

Classic problem: Two-dimensional partial ordering. The number of points at the bottom left of each point, given the n points in a plane.

Because all points already have Y as the first keyword, x is ordered for the second keyword, so we do it in read order, just need to calculate how many points the X coordinate is less than <= a point.

This is what is said: n-dimensional partial order, one-dimensional ordering, two-dimensional tree-like array, three-dimensional partition Or tree-shaped array set of balance tree ...

< law one > tree-like array.

1#include <cstdio>2#include <algorithm>3#include <iostream>4 using namespacestd;5 struct Point6 {7     intx, y;8 };9 intn,d[3200001],ji[1500001],m;TenPoint star[1500001]; One BOOLcmpConstPoint &a,ConstPoint &b) A { -     if(a.x<b.x) -       return true; the     Else if(a.x>b.x) -       return false; -     Else if(a.y<b.y) -       return true; +     return false; - } + intLowbit (intx) A { at     returnx& (-x); - } - voidUpdateintXintDelta) - { -      for(; x<=m;x+=lowbit (x)) -d[x]+=Delta; in } - intGetsum (intx) to { +     intres=0; -      for(;x>0; x-=lowbit (x)) theres+=D[x]; *     returnRes; $ }Panax Notoginseng intMain () - { thescanf"%d",&n); +      for(intI=1; i<=n;i++) A       { thescanf"%d%d",&star[i].x,&star[i].y); +star[i].y++; -m=Max (M,STAR[I].Y); $       } $      for(intI=1; i<=n;i++) -       { -Update (STAR[I].Y,1); theJi[getsum (STAR[I].Y)-1]++; -       }Wuyi      for(intI=0; i<n;i++) theprintf"%d\n", Ji[i]); -     return 0; Wu}

< method two > Weight sub-block. I'd say it's nearly a fold faster than a tree-shaped array?

1#include <cstdio>2#include <cmath>3#include <algorithm>4 using namespacestd;5 intn,x[15001],y[15001],limit,r[ $],l[ $],num[33000],sumv[ $],sz,sum,rank[33000],b[33000];6 voidMakeblock ()7 {8Sz= (int) sqrt (Double) LIMIT);if(!SZ) sz=1; r[0]=-1;9      for(sum=1; sum*sz<limit;sum++)Ten       { Onel[sum]=r[sum-1]+1; Ar[sum]=sum*sz; -            for(inti=l[sum];i<=r[sum];i++) num[i]=sum; -       } thel[sum]=r[sum-1]+1; -r[sum]=LIMIT; -      for(inti=l[sum];i<=r[sum];i++) num[i]=sum; - } + intQueryConst int&V) - { +     intCnt=0; A      for(intI=1; i<num[v];i++) cnt+=Sumv[i]; at      for(inti=l[num[v]];i<=v;i++) cnt+=B[i]; -++B[V]; ++Sumv[num[v]]; -     returnCNT; - } - intMain () - { inscanf"%d",&n); -      for(intI=1; i<=n;i++) to       { +scanf"%d%d",&x[i],&y[i]); -limit=Max (limit,x[i]); the } makeblock (); *      for(intI=1; i<=n;i++) + +rank[query (X[i]); $      for(intI=0; i<n;i++) printf ("%d\n", Rank[i]);Panax Notoginseng     return 0; -}

"Tree-like array" "Weight-value chunking" bzoj2352 Stars

Related Article

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.