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