有n朵花,每朵花有三个属性:花形(s)、颜色(c)、气味(m),又三个整数表示。现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量。定义一朵花A比另一朵花B要美丽,当且仅当Sa>=Sb,Ca>=Cb,Ma>=Mb。显然,两朵花可能有同样的属性。需要统计出评出每个等级的花的数量。
标签:www time mit limit inline algo desc input script
1 <= N <= 100,000, 1 <= K <= 200,000
#include <cstdio> #include <algorithm> using namespace std; char buf[10000000], *ptr = buf - 1; inline int readint(){ int n = 0; char ch = *++ptr; while(ch < ‘0‘ || ch > ‘9‘) ch = *++ptr; while(ch <= ‘9‘ && ch >= ‘0‘){ n = (n << 1) + (n << 3) + ch - ‘0‘; ch = *++ptr; } return n; } const int maxn = 100000 + 10, maxk = 200000 + 10; int n, k; int arr[maxk] = {0}; inline void Update(int w, int s){ for(; w <= k; w += w & -w) arr[w] += s; } inline int Query(int w){ int s = 0; for(; w; w -= w & -w) s += arr[w]; return s; } struct Node{ int a, b, c, id, s; Node(){} Node(int _a, int _b, int _c, int _id = 0, int _s = 1): a(_a), b(_b), c(_c), id(_id), s(_s){} }tp[maxn], no[maxn]; class cmp{ public: bool operator () (const Node &x, const Node &y){ return x.a == y.a ? x.b == y.b ? x.c < y.c : x.b < y.b : x.a < y.a; } }; int rank[maxn] = {0}, cnt[maxn] = {0}; void CDQ(int l, int r){ if(l == r) return; int mid = l + r >> 1, ll = l, rr = mid + 1, tcnt = 0; CDQ(l, mid); CDQ(mid + 1, r); while(ll <= mid && rr <= r){ if(no[ll].b <= no[rr].b){ Update(no[ll].c, no[ll].s); tp[++tcnt] = no[ll++]; } else{ rank[no[rr].id] += Query(no[rr].c); tp[++tcnt] = no[rr++]; } } while(ll <= mid){ Update(no[ll].c, no[ll].s); tp[++tcnt] = no[ll++]; } while(rr <= r){ rank[no[rr].id] += Query(no[rr].c); tp[++tcnt] = no[rr++]; } for(int i = l; i <= mid; i++) Update(no[i].c, -no[i].s); for(int i = 1; i <= tcnt; i++) no[l + i - 1] = tp[i]; } int main(){ fread(buf, sizeof(char), sizeof(buf), stdin); int N = readint(); n = 0; k = readint(); tp[0] = Node(0, 0, 0, 0, 0); for(int a, b, c, i = 1; i <= N; i++){ a = readint(); b = readint(); c = readint(); tp[i] = Node(a, b, c); } sort(tp + 1, tp + N + 1, cmp()); for(int i = 1; i <= N; i++){ if(tp[i].a == no[n].a && tp[i].b == no[n].b && tp[i].c == no[n].c) no[n].s++; else no[++n] = Node(tp[i].a, tp[i].b, tp[i].c, n); } CDQ(1, n); for(int i = 1; i <= n; i++) cnt[rank[no[i].id] + no[i].s - 1] += no[i].s; for(int i = 0; i < N; i++) printf("%d\n", cnt[i]); return 0; }
标签:www time mit limit inline algo desc input script
原文地址:http://www.cnblogs.com/ruoruoruo/p/7580651.html