Hdu 5126 stars CDQ divided treatment

Source: Internet
Author: User

stars Time limit:20000/10000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 282 Accepted Submission (s): 68


Problem Description John loves to see the sky. A Day has A Q times. Each time John would find a new star in the sky, or he wants to know how many stars between (X1,Y1,Z1) and (X2,Y2,Z2).
Input The first line contains a single integer T (1≤t≤10) (the data for q>100 less than 6 cases), indicating the number of test cases.
The first line contains an integer Q (1≤q≤50000), indicating how many times in a day.
Next Q lines contain some integers, first input an integer A (1≤a≤2). If a=1 then input 3 integers x, y and Z, indicating A coordinate of one star. If a=2 then input 6 integers x1,y1,z1,x2,y2,z2 (1≤x,y,z,x1,y1,z1,x2,y2,z2≤109,x1≤x2,y1≤y2,z1≤z2).
Output for each "a=2", the output an integer means how many stars in the such A section.
Sample Input

2 11 1 1 1 1 2 1 1 1 1 1 1 1 2 2 2 1 1 1 2 2 1 1 1 2 2 2 1 3 3 3 1 4 4 4 1 5 5 5 1 6 6 6 2 1 1 1 6 6 6 6 6 11 1 1 1 1 2 1 1 1 1 1 1 1 2 2 2 1 1 1 2 2 1 1 1 2 2 2 1 3 3 3 1 4 4 4 1 5 5 5 1 6 6 6 2 1 1 1 3 3 6) 6 6
Sample Output
1 3 7 4 1 3 7 4 topic meaning: first three-dimensional space no point, to M operation, a=1 Insert a point, a=2 asked a subspace in a few points

It was timed out with a kd tree. On the internet to see is CDQ divided treatment, looked at the next ppt.

Http://wenku.baidu.com/link?url=VwuZ4ij8GABr5FOVcuU2yyMelwPSa_ Sxaha9lgvu0bqtrf2j3guzgghxe7lini0-eulsxjjxz3omfl4dlivozddlriudcptcpawthnamlj3

And I saw someone else's solution.

In the end, my approach is roughly the same, but it's relatively slow.

Split a query into 8 queries (tolerance), using a tree-like array to maintain coordinates Z from 0 to the number of points that need to be queried, of course, Z should be discretized first.

Then is to use CDQ division, make every query is effective point.

The idea is to divide a set, so that the insertion point in each set is in the dimension that has been processed, and the coordinates are less than the coordinate value of the inquiry.

1. Because the points of insertion and query are sequential, they should be considered as four-dimensional. The first time should be sorted by the Order of operations (in fact, no sort) to do CDQ division

2. For the first time in the CDQ Division, the division of the rule makes the point by the x-axis sort, because according to the left and right two parts, you can know the order < The order of operation of the side, then the left side of the insertion and the right side of the query put together, then the new set in the order of this dimension, the query And because the division is sorted by X, the new set can be linearly sorted by the x-axis size of the first keyword and the order of the second keyword.

3. In the new collection, sort the y-axis, because the left insert operation Point x< to the right of X, then the left insertion point y is less than the right of the current query point y, then in the update tree array to the left of the insertion point z value, and then update the query, since the tree array to empty each time, So the points that are inserted need to be deleted

There are two ways to clear a tree array:

1. Timestamp mark, with T[],tag, if T[]=tag indicates that the value of this position is new, otherwise set to 0, let T[]=tag, each time to empty tag++ can

2. Delete the inserted point

#include <cstdio> #include <algorithm> using namespace std;
    struct node{int x,y,z,p,t;
Node () {} node (int x,int y,int z,int p,int T): X (x), Y (y), Z (z), p (p), T (t) {}};
#define MAXN 450007 Node STAR[MAXN];
Node STAR2[MAXN];
Node STAR3[MAXN];
int TREE[MAXN];
        int lowbit (int i) {return i& (-i),} void Add (int i,int x) {while (I&LT;MAXN) {tree[i]+=x;
    I+=lowbit (i);
    }} int get (int i) {int ans = 0;
        while (i>0) {ans + = tree[i];
    I-= lowbit (i);
} return ans;
} int RES[MAXN];
    void CDQ2 (int l,int r) {if (L = = r) return;
    int mid = (l+r)/2;
    CDQ2 (L,mid);
    CDQ2 (MID+1,R);
    int L1 =l, r1 = mid+1; while (R1 <= R) {while (L1 <= mid && star2[l1].y <= star2[r1].y) {if (star2[l1].t = = 0)
            Add (star2[l1].z,1);
        l1++;
        } if (star2[r1].t! = 0) {RES[STAR2[R1].P] + = Get (star2[r1].z) *star2[r1].t;
    } r1++; } while (L1 >L) {--l1;
    if (star2[l1].t = = 0) Add (star2[l1].z,-1);
    } L1 = l, r1 = mid+1;
            for (int i = L;i <= r; i++) {if (L1 <= mid && star2[l1].y <= star2[r1].y) | | R1 > R)
        Star3[i] = star2[l1++];
    else star3[i] = star2[r1++];
} for (int i = l; I <= R; i++) Star2[i] = Star3[i];
    } void CDQ1 (int l,int r) {if (L = = r) return;
    int mid = (l+r)/2;
    CDQ1 (L,mid);
    CDQ1 (MID+1,R);
    int L1 = l, r1 = mid+1,n = 0;
        while (R1 <= R) {while (star[l1].t! = 0 && L1 <= mid) l1++;
        while (star[r1].t = = 0 && R1 <= r) r1++;
        if (R1 > R) break;
        if ((star[l1].x <= star[r1].x && L1 <= mid) | | R1 > R) star2[n++] = star[l1++];
    else star2[n++] = star[r1++];
    } if (n > 0) CDQ2 (0,n-1);
    L1 = l, r1 = mid+1;
for (int i = L;i <= r; i++) {if (star[l1].x <= star[r1].x && L1 <= mid) | | R1 > R)            Star3[i] = star[l1++];
    else star3[i] = star[r1++];
} for (int i = L;i <= r; i++) Star[i] = Star3[i];
    } int COMPN (node A,node b) {return A.P < B.P;} int Compz (node A,node b) {return a.z < B.Z;} int main () {
    int t,q,a,x1,y1,z1,x2,y2,z2;
    scanf ("%d", &t);
        while (t--) {int n = 0;
        scanf ("%d", &q);
            while (q--) {scanf ("%d", &a);
                if (a = = 1) {scanf ("%d%d%d", &AMP;X1,&AMP;Y1,&AMP;Z1);
            star[n++] = Node (x1,y1,z1,n,0);
                } else {scanf ("%d%d%d%d%d%d", &AMP;X1,&AMP;Y1,&AMP;Z1,&AMP;X2,&AMP;Y2,&AMP;Z2);
                star[n++] = Node (x2,y2,z2,n,1);
                star[n++] = Node (x2,y1-1,z2,n,-1);
                star[n++] = Node (x1-1,y2,z2,n,-1);
                star[n++] = Node (x2,y2,z1-1,n,-1);
                star[n++] = Node (x1-1,y1-1,z2,n,1);
               star[n++] = Node (x1-1,y2,z1-1,n,1); star[n++] = Node (x2,y1-1,z1-1,n,1);
            star[n++] = Node (x1-1,y1-1,z1-1,n,-1);
        }} for (int i = 0;i < n; i++) res[i] = 0;
        Sort (STAR,STAR+N,COMPZ);
        A = 1;
        Star[n].z =-1;
            for (int i = 0;i < n; i++) {if (star[i].z! = star[i+1].z) star[i].z = a++;
        else star[i].z = A;
        } sort (STAR,STAR+N,COMPN);
        CDQ1 (0,n-1);
        Sort (STAR,STAR+N,COMPN);
                for (int i = 0;i < n; i++) {if (star[i].t! = 0) {int ans = 0;
                for (int j = 0;j < 8; j + +) ans + = res[i+j];
                printf ("%d\n", ans);
            i + = 7;
}}} return 0; }


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.