BZOJ 3262: Flowering CDQ 3-D Partial Order on the Mouse, Tree Nest Tree, Two Solutions

Keywords: less Attribute Programming

Description
There are n flowers, each flower has three attributes: flower shape (s), color (c), odor (m), and three integers. Now to rate each flower, the level of a flower is that it has more beautiful flowers than it can have. Define a flower A to be more beautiful than another flower B if and only if Sa > = Sb, Ca > = Cb, Ma > = Mb. Obviously, two flowers may have the same attributes. The number of flowers for each grade needs to be counted.
Input
The first behavior N, K (1 <= N <= 100,000, 1 <= K <= 200,000) denotes the number of flowers and the maximum attribute value, respectively.
The following N lines, three integers si, ci, mi (1 <= si, ci, mi <= K) per line, denote the attributes of the first flower
Output
Contains N rows, representing ratings of 0. Number of flowers per level of N-1.
Sample Input
10 3

3 3 3

2 3 3

2 3 1

3 1 1

3 1 2

1 3 1

1 1 2

1 2 2

1 3 2

1 2 1

Sample Output
3

1

3

0

1

0

1

0

0

1

HINT

1 <= N <= 100,000, 1 <= K <= 200,000

Solution: Three-dimensional... First-dimensional sorting, second-dimensional tree array, third-dimensional treap. Complexity: O(n*logn*logn)

//bzoj 3262
//One-Dimensional Sorting, Two-Dimensional BIT, Three-Dimensional Balanced Tree
#include <bits/stdc++.h>
using namespace std;
int n, m, tmp, root[200005], cnt[200005], ans[200005];
struct node{
    int a, b, c;
    node(){}
    node(int a, int b, int c) : a(a), b(b), c(c) {}
    bool operator < (const node &rhs) const{
        if(a == rhs.a && b == rhs.b) return c < rhs.c;
        if(a == rhs.a) return b < rhs.b;
        return a < rhs.a;
    }
} a[200005];
namespace multi_treap{
    struct data{
        int l, r, v, size, rnd, w;
    }tree[5000005];
    int size;
    void update(int k)
    {
        tree[k].size = tree[tree[k].l].size + tree[tree[k].r].size + tree[k].w;
    }
    void rturn(int &k)
    {
        int t = tree[k].l;
        tree[k].l = tree[t].r;
        tree[t].r = k;
        tree[t].size = tree[k].size;
        update(k);
        k = t;
    }
    void lturn(int &k)
    {
        int t = tree[k].r;
        tree[k].r = tree[t].l;
        tree[t].l = k;
        tree[t].size = tree[k].size;
        update(k);
        k=t;
    }
    void insert(int &k, int x)
    {
        if(k == 0)
        {
            size++;
            k = size;
            tree[k].size = tree[k].w = 1;
            tree[k].v = x;
            tree[k].rnd = rand();
            return ;
        }
        tree[k].size++;
        if(tree[k].v==x) tree[k].w++;//Each node incidentally records the number of identical values with that node
        else if(x > tree[k].v)
        {
            insert(tree[k].r, x);
            if(tree[tree[k].r].rnd < tree[k].rnd) lturn(k);//Maintaining heap properties
        }
        else
        {
            insert(tree[k].l, x);
            if(tree[tree[k].l].rnd < tree[k].rnd) rturn(k);
        }
    }
    void del(int &k,int x)
    {
        if(k == 0)return;
        if(tree[k].v == x)
        {
            if(tree[k].w > 1)
            {
                tree[k].w--;
                tree[k].size--;
                return;//If there are more than one of the same values, delete one
            }
            if(tree[k].l * tree[k].r == 0)k = tree[k].l + tree[k].r;//One son is empty.
            else if(tree[tree[k].l].rnd < tree[tree[k].r].rnd)
                rturn(k),del(k,x);
            else lturn(k),del(k,x);
        }
        else if(x > tree[k].v)
            tree[k].size--, del(tree[k].r,x);
        else tree[k].size--, del(tree[k].l,x);
    }
    void query_rank(int k, int num){
        if(!k) return ;
        if(num == tree[k].v){tmp += tree[tree[k].l].size + tree[k].w; return;}
        else if(num < tree[k].v) query_rank(tree[k].l, num);
        else{tmp += tree[tree[k].l].size + tree[k].w; query_rank(tree[k].r, num);}
    }
}
using namespace multi_treap;
namespace BIT{
    inline int lowbit(int x){return x & (-x);}
    inline void update(int x, int y) {for(int i = x; i <= m; i += lowbit(i)) insert(root[i], y);}
    inline void query(int x, int y) {for(int i = x; i; i -= lowbit(i)) query_rank(root[i], y);}
}
using namespace BIT;
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].a, &a[i].b, &a[i].c);
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; i++){
        if(a[i].a == a[i+1].a && a[i].b == a[i+1].b && a[i].c == a[i+1].c && i!=n){
            cnt[i+1] += cnt[i]+1;
        }
        else{
            tmp = 0;
            query(a[i].b, a[i].c);
            ans[tmp] += cnt[i] + 1;
        }
        update(a[i].b, a[i].c);
    }
    for(int i = 0; i < n; i++) printf("%d\n", ans[i]);
    return 0;
}

Another approach is CDQ:
Maintain three-dimensional partial order:
The first order, the second CDQ divide and conquer, and the third tree array.

First, we can combine all three-dimensional phases together and calculate them together.

We sort them by s and assign them to 1. tot in turn (where s is analogous to id).

Next, sort by c, and start the CDQ divide and conquer.

When calculating the impact of the left on the right:
A on the left is less than a on the right.
(2) On each side b also increases in turn.
(3) We only need to scan the right side and add the m of the flower whose left b is less than or equal to the current flower to the tree array. The number of m less than or equal to the current flower in the tree array is counted as the left three-dimensional is less than or equal to the current flower.

Complexity: O(n*logn*logn), but relative to the tree nest tree, the space complexity and programming complexity of CDQ are much smaller, and the running time is fast.

//bzoj 3262
//1-D sorting, 2-D dividing and conquering, 3-D tree array
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n, m, ans[maxn], tree[maxn*4];
struct node{
    int a, b, c, s, ans; //s handles the same continuity, and ans is smaller than the current beauty value
    node(){}
    node(int a, int b, int c, int s, int ans) : a(a), b(b), c(c), s(s), ans(ans) {}
    bool operator < (const node &rhs) const{ //Sort by y
        if(b == rhs.b) return c < rhs.c;
        return b < rhs.b;
    }
}a[maxn], p[maxn];
bool cmp(node x, node y){ //Sort by x
    if(x.a == y.a && x.b == y.b) return x.c < y.c;
    if(x.a == y.a) return x.b < y.b;
    return x.a < y.a;
}
namespace BIT{
    inline int lowbit(int x) {return x&-x;}
    inline void update(int x, int y){for(int i = x; i <= m; i+=lowbit(i)) tree[i] += y;}
    inline int query(int x){int res = 0; for(int i = x; i; i -= lowbit(i)) res += tree[i]; return res;}
}
using namespace BIT;
void CDQ(int l, int r)
{
    if(l == r) return;
    int mid = (l + r) >> 1;
    CDQ(l, mid);
    CDQ(mid+1, r);
    sort(p + l, p + mid + 1);
    sort(p + mid + 1, p + r + 1);
    int i = l, j = mid + 1;
    while(j <= r){
        while(i <= mid && p[i].b <= p[j].b){
            update(p[i].c, p[i].s);
            i++;
        }
        p[j].ans += query(p[j].c);
        j++;
    }
    for(int j = l; j < i; j++) update(p[j].c, -p[j].s);
}
int main(){
    int nn;
    scanf("%d%d", &nn, &m);
    for(int i = 1; i <= nn; i++){
        scanf("%d%d%d", &a[i].a, &a[i].b, &a[i].c);
    }
    sort(a + 1, a + nn + 1, cmp); //According to x row
    int cnt = 0; //unique
    for(int i = 1; i <= nn; i++){
        cnt++;
        if(a[i].a != a[i+1].a || a[i].b != a[i+1].b || a[i].c != a[i+1].c){
            p[++n] = a[i];
            p[n].s = cnt;
            cnt = 0;
        }
    }
    CDQ(1, n);
    for(int i = 1; i <= n; i++){
        ans[p[i].ans + p[i].s - 1] += p[i].s;
    }
    for(int i = 0; i < nn; i++){
        printf("%d\n", ans[i]);
    }
    return 0;
}

Posted by ldsmike88 on Sun, 31 Mar 2019 02:09:30 -0700