P3810 模板三维偏序(陌上花开)cdq分治
Posted ckxkexing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
以上是关于P3810 模板三维偏序(陌上花开)cdq分治的主要内容,如果未能解决你的问题,请参考以下文章
洛谷 P3810 模板三维偏序(陌上花开) (cdq分治模板)
[bzoj] 3263 陌上花开 洛谷 P3810 三维偏序|| CDQ分治 && CDQ分治讲解