Description
Solition
CDQ分治,第一维直接快排,第二维归并,第三维树状数组
Code
//Au: Hany01
//Date: Dec 24nd, 2017
//Prob: CDQ
//Email: hany01@foxmail.com
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif
inline void File()
{
#ifdef hany01
freopen("cdq.in" , "r" , stdin);
freopen("cdq.out" , "w" , stdout);
#endif
}
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register char c_;
register int _ , __;
for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 100005, maxm = 200005;
int n, k, cnt, Sum[maxn];
struct Node
{
int x, y, z, id, num, Ans;
bool operator == (const Node &A) const { return x == A.x && y == A.y && z == A.z; }
}A[maxn];
struct BIT
{
int c[maxm];
inline int lowbit(int x) { return x & -x; }
inline void update(int pos, int dt) { for ( ; pos <= k; pos += lowbit(pos)) c[pos] += dt; }
inline int query(int pos) { int Ans = 0; for ( ; pos; pos -= lowbit(pos)) Ans += c[pos]; return Ans; }
}FT;
inline bool cmp1(const Node &A, const 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;
}
inline bool cmp2(const Node &A, const Node &B)
{
if (A.y != B.y) return A.y < B.y;
return A.z < B.z;
}
inline void Init()
{
n = read(); k = read();
For(i, 1, n) A[i].x = read(), A[i].y = read(), A[i].z = read(), A[i].id = i;
sort(A + 1, A + 1 + n, cmp1);
}
inline void Unique()
{
A[cnt = 1].num = 1;
For(i, 2, n) if (A[i] == A[i - 1]) ++ A[cnt].num; else A[++ cnt] = A[i], A[cnt].num = 1;
}
inline void CDQ(int l, int r)
{
if (l == r) return ;
register int mid = (l + r) >> 1;
CDQ(l, mid); CDQ(mid + 1, r);
sort(A + l, A + mid + 1, cmp2); sort(A + mid + 1, A + r + 1, cmp2);
register int x = l, y = mid + 1;
while (y <= r) {
while (x <= mid && A[x].y <= A[y].y) { FT.update(A[x].z, A[x].num); ++ x; }
A[y].Ans += FT.query(A[y].z); ++ y;
}
For(i, l, x - 1) FT.update(A[i].z, -A[i].num);
}
inline void Print()
{
For(i, 1, cnt) Sum[A[i].Ans + A[i].num - 1] += A[i].num;
For(i, 0, n - 1) printf("%d\n", Sum[i]);
}
int main()
{
File();
Init();
Unique();
CDQ(1, cnt);
Print();
return 0;
}
//寄相思,寒雨灯窗,芙蓉旧院。
// -- 吴文英《宴清都·秋感》