Description 有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),又三个整数表示。现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。定义一朵花A比另一朵花B要美丽,当且仅当Sa>=Sb,Ca>=Cb,Ma>=Mb。显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。 Input 第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值。 以下N行,每行三个整数si, ci, mi (1 <= si, ci, mi <= K),表示第i朵花的属性 Output 包含N行,分别表示评级为0…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
解题方法: 三维。。。第一维排序,第二维树状数组,第三维treap。 复杂度: O(n*logn*logn)
//bzoj 3262//1维排序,二维BIT,3维平衡树#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++;//每个结点顺便记录下与该节点相同值的数的个数 else if(x > tree[k].v) { insert(tree[k].r, x); if(tree[tree[k].r].rnd < tree[k].rnd) lturn(k);//维护堆性质 } 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(tree[k].l * tree[k].r == 0)k = tree[k].l + tree[k].r;//有一个儿子为空 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;}另外一种方法就是CDQ: 维护三维偏序: 第一维排序,第二位CDQ分治,第三维树状数组。
首先我们可以把三维都相的合并到一起,一块计算。
我们按照s排序,然后依次把他们赋值为1..tot(这里可以把s类比为id)。
接下来按照c排序,开始CDQ分治。
计算左边对右边的影响的时候: ①左边的a都小于右边 ②在每一边b也是依次递增的 ③我们只要扫描右边,把左边b小于等于当前的花的花的m加入到树状数组,统计目前树状数组中m小于等于当前扫描到的花的m的个数就是左边三维都小于等于当前花得个数。
复杂度: O(n*logn*logn) 但是相对于树套树,CDQ的空间复杂度和编程复杂度小的多,运行时间也飞快。
//bzoj 3262//1维排序,二维分治,3维树状数组#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处理相同连续,ans比当前美丽值小个数 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{ //按y排序 if(b == rhs.b) return c < rhs.c; return b < rhs.b; }}a[maxn], p[maxn];bool cmp(node x, node y){ //按照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); //按照x排 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;}新闻热点
疑难解答