zoukankan      html  css  js  c++  java
  • P3810 【模板】三维偏序(陌上花开)cdq分治

    传送门:https://www.luogu.org/problemnew/show/P3810

    cdq分治的模板题,第一层外部排序,第二层cdq归并排序,这个时候不用考虑第一次的顺序,第三次用树状数组。

    注意,不要用memset,用队列保存加上的值,最后在把加上的值减去就行了。

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    //#include <unordered_map>
    /*
    
    ⊂_ヽ
      \\ Λ_Λ  来了老弟
       \('ㅅ')
        > ⌒ヽ
       /   へ\
       /  / \\
       レ ノ   ヽ_つ
      / /
      / /|
     ( (ヽ
     | |、\
     | 丿 \ ⌒)
     | |  ) /
    'ノ )  Lノ
    
    */
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define boost ios::sync_with_stdio(false);cin.tie(0)
    #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    
    
    const ll oo = 1ll<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 998244353;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    inline void cmax(int &x,int y){if(x<y)x=y;}
    inline void cmax(ll &x,ll y){if(x<y)x=y;}
    inline void cmin(int &x,int y){if(x>y)x=y;}
    inline void cmin(ll &x,ll y){if(x>y)x=y;}
    #define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b))
    #define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b))
    
    /*-----------------------showtime----------------------*/
                const int maxn = 1e5+9;
                struct node{
                    int x,y,z;
                    int id;
                }a[maxn],b[maxn],tmp[maxn];
                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;
                    else return a.z < b.z;
                }
                int cnt[maxn],ans[maxn];
    
                int sum[maxn*2];
                int lowbit(int x){
                    return x & (-x);
                }
                void add(int x,int c){
                    while(x < maxn*2){
                        sum[x] += c;
                        x += lowbit(x);
                    }
                }
                int getsum(int x){
                    int res = 0;
                    while(x > 0) {
                        res += sum[x];
                        x -= lowbit(x);
                    }
                    return res;
                }
                int lazy[maxn];
                queue<int>que;
                void cdq(int le,int ri){
                    int mid = (le + ri) >> 1;
                    if(ri - le <= 0) return ;
                    cdq(le, mid);   cdq(mid+1, ri);
    
             //       memset(sum, 0, sizeof(sum));
    
                    int p = le, q = mid+1;
                    int id = 0;
                    while(p <= mid && q <= ri){
                        if(a[p].y <= a[q].y) {
                            add(a[p].z, lazy[a[p].id]);
                            que.push(p);
                            tmp[++id] = a[p++];
                        }
                        else {
                            cnt[a[q].id] += getsum(a[q].z);
                            tmp[++id] = a[q++];
                        }
                    }
                    while(p <= mid) tmp[++id] = a[p++];
                    while(q <= ri) {
                        cnt[a[q].id] += getsum(a[q].z);
                        tmp[++id] = a[q++];
                    }
                    while(!que.empty()){
                        int u = que.front(); que.pop();
                        add(a[u].z, -lazy[a[u].id]);
                    }
    
                    for(int i=1; i<=id; i++) a[i+le-1] = tmp[i];
    
                }
               // int out[maxn];
               map<p3,int>mp;
    
    
    int main(){
                int n,k;
                scanf("%d%d", &n, &k);
                int tot = 0;
                rep(i, 1, n) {
                    int x, y, z;
                    scanf("%d%d%d", &x, &y, &z);
                 //   read(x);read(y);read(z);
                    p3 tp = p3(x, pii(y, z));
                    if(mp.count(tp)) lazy[mp[tp]]++,cnt[mp[tp]]++;
                    else {
                        tot++;
                        a[tot].id = tot;
                        a[tot].x = x;
                        a[tot].y = y;
                        a[tot].z = z;
                        mp[tp] = tot;
                        lazy[tot] = 1;
                    }
                }
                sort(a+1, a+1+tot, cmp);
    
                cdq(1, tot);
    
                rep(i, 1, tot) ans[cnt[a[i].id]] += lazy[a[i].id];
                /*
                rep(i, 1, n) {
                    out[a[i].id] = cnt[a[i].id];
                }
                rep(i, 1, n)
                    cout<<cnt[i]<<" ";
                cout<<endl;
                */
                rep(i, 0, n-1) printf("%d
    ", ans[i]);
                return 0;
    }
    View Code
  • 相关阅读:
    Rust交叉编译Mac编译Linux/Windows平台
    SpringBoot 如何生成接口文档
    Echarts + Python 实现的动态实时大屏范例
    计算机中的0.1+0.2=0.3吗?(无可避免的浮点误差)
    Odin线刷失败的常见错误原因分析及解决方法(转载)
    Odin3 刷机工具刷机教程, BL、AP、CP 与 CSC 是什么意思(转载)
    各种常见USB接口类型
    三星S8+手机,刷机经验
    小米8手机,MIUI由12.5降级到9.5、安卓由10降到8;先ROOT,再安装Magisk、Xposed的步骤
    手机刷机相关,若干名词
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/10459390.html
Copyright © 2011-2022 走看看