Three-dimensional partial sequence tree array of bzoj3262/Rokua P3810 on the flower-open CDQ

Source: Internet
Author: User

Original link http://www.cnblogs.com/zhouzhendong/p/8672131.html

Topic Portal-BZOJ3262Topic Portal-Fall Valley P3810Test Instructions

There are $n$ elements, the $i$ element has $a_i$, $b _i$, $c _i$ three attributes, set $f (i) $ to meet $a_j\leq a_i$ and $b_j\leq b_i$ and $c_j\leq c_i$ the number of $j$. For $d\in [0,n] $, ask for the number of $f (i) =d$.

$n \leq 100000,max\{a_i,b_i,c_i|i\in[1,n]\}<=200000$

Solving

Three-dimensional partial-order template problem.

CDQ a wave of treatment. A tree set of trees is also possible.

CDQ idea of divided treatment:

One-dimensional partial order: direct ordering.

Two-dimensional partial order: a tree-like array.

Three-dimensional partial order: the 3rd dimensional set of CDQ.

We still deal with the first and second dimensions as they are.

For the third dimension, we consider divide and conquer.

First obviously to sort.

For an interval $[l,r]$, $mid=\left\lfloor\frac{l+r}{2}\right\rfloor$ is first obtained.

Then consider the first division of $[l,mid]$ and $[mid+1,r]$.

Then, using the tree-like array, the left interval is used to update the right interval. Notice the restoration of the tree array.

Overall, for the 3rd dimension, rank was saved at the beginning. For the 2nd dimension, it is in the recursive process of merging to get an orderly arrangement. For the 1th dimension, the third dimension is ordered because the second dimension is the same as the 3rd keyword, and the merge sort is stable.

Code
#include <bits/stdc++.h>using namespace Std;const int n=100005;struct node{int x,y,z,rank,res;void get () {scanf (" %d%d%d ", &x,&y,&z), Res=0;}} A[n],b[n];int N,k,tree[n*2],tot[n];bool cmp (Node A,node b) {if (a.x!=b.x) return a.x<b.x;if (A.Y!=B.Y) return a.y< B.y;return a.z<b.z;} BOOL Issame (Node A,node b) {return a.x==b.x&&a.y==b.y&&a.z==b.z;} void Sameadd () {int tot=1,last=n;for (int i=n-1;i>=1;i--) if (Issame (A[i],a[last])) a[i].res+=tot++;elselast=i,tot= 1;} int lowbit (int x) {return x&-x;} void Add (int x,int y) {for (; X<=k;x+=lowbit (x)) tree[x]+=y;} int sum (int x) {int ans=0;for (; x>0;x-=lowbit (x)) Ans+=tree[x];return ans;} void CDQ (int l,int R) {if (l==r) Return;int mid= (l+r) >>1,cnt=L; CDQ (L,mid), CDQ (Mid+1,r); for (int i=l,l=l,r=mid+1;i<=r;i++) if (r>r| | (L&LT;=MID&AMP;&AMP;A[L].Y&LT;=A[R].Y)) b[cnt++]=a[l++];elseb[cnt++]=a[r++];for (int i=l;i<=r;i++) {a[i]=b[i];if (A[i].rank<=mid) Add (a[i].z,1); Elsea [I].res+=sum (A[I].Z);} for (int i=l;i<=r; i++) if (a[i].rank<=mid) Add (a[i].z,-1);} int main () {scanf ("%d%d", &n,&k), for (int i=1;i<=n;i++) a[i].get (); sort (a+1,a+n+1,cmp); Sameadd (); for (int i =1;i<=n;i++) A[i].rank=i;memset (tree,0,sizeof tree); CDQ (1,n), memset (tot,0,sizeof tot), for (int i=1;i<=n;i++) tot[a[i].res]++;for (int i=0;i<n;i++) printf ("%d\n", Tot[i]); return 0;}

  

Three-dimensional partial sequence tree array of bzoj3262/Rokua P3810 on the flower-open CDQ

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.