# Luogu 3810 & BZOJ 3263 陌上花开/三维偏序 | CDQ分治

## 题面

$n$个元素，每个元素有三个值：$a_i$, $b_i$$c_i$。定义一个元素的偏序是三个值都小于等于它的值的元素的个数，对于$[0, n)$的每个值$i$，求偏序为$i$的元素个数。

## 题解

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define space putchar(' ')
#define enter putchar('\n')
template <class T>
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}

const int N = 2000005;
int n, k, tot, tr[N], ans[N];
struct mem{
int a, b, c, cnt, sum;
bool operator < (const mem &B) const{
if(a != B.a) return a < B.a;
if(b != B.b) return b < B.b;
return c < B.c;
}
bool operator == (const mem &B) const{
return a == B.a && b == B.b && c == B.c;
}
} m[N], t[N];
bool cmp(const mem &A, const mem &B){
return A.b < B.b;
}
while(p <= k) tr[p] += x, p += p & -p;
}
int ret = 0;
while(p) ret += tr[p], p -= p & -p;
return ret;
}
void solve(int l, int r){
if(l == r) return;
int mid = (l + r) >> 1;
solve(l, mid), solve(mid + 1, r);
sort(m + l, m + mid + 1, cmp);
sort(m + mid + 1, m + r + 1, cmp);
int pl = l, pr = mid + 1;
while(pr <= r){
while(pl <= mid && m[pl].b <= m[pr].b)
pr++;
}
for(int i = l; i < pl; i++)
}
int main(){
for(int i = 1, a, b, c; i <= n; i++)
sort(t + 1, t + n + 1);
n = 0;
for(int i = 1; i <= tot; i++){
if(i == 1 || !(t[i] == t[i - 1]))
m[++n] = t[i];
m[n].cnt++;
}
solve(1, n);
for(int i = 1; i <= n; i++)
ans[m[i].sum + m[i].cnt - 1] += m[i].cnt;
for(int i = 0; i < tot; i++)
write(ans[i]), enter;
return 0;
}


posted @ 2017-11-24 17:37  胡小兔  阅读(318)  评论(0编辑  收藏  举报