EntropyIncreaser

三维偏序

Nov 24th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <cstdio>
  2. #include <climits>
  3.  
  4. #include <algorithm>
  5.  
  6. #define LOG(FMT...) fprintf(stderr, FMT)
  7. #define GET_CH(P, F) ((F) ? (P)->rs : (P)->ls)
  8.  
  9. using namespace std;
  10.  
  11. struct node {
  12.     int x, cnt, rnd;
  13.     node *ls, *rs;
  14. };
  15.  
  16. struct data {
  17.     int a, b, c;
  18.    
  19.     bool operator<(const data& x) const;
  20. };
  21.  
  22. const int N = 100010;
  23.  
  24. int n;
  25. int cnt[N], xb[N];
  26. node *t[N];
  27. data d[N];
  28.  
  29. int low_bit(int x);
  30. void change(int b, int c);
  31. int query(int b, int c);
  32. node* create(int x);
  33. void insert(node*& p, node* q);
  34. int query(node* p, int x);
  35. void rotate(node*& p, bool f);
  36. void update(node* p);
  37. int get_sz(node* p);
  38.  
  39. int main() {
  40.     int ib, cur;
  41.     {
  42.         int ig;
  43.         scanf("%d%d", &n, &ig);
  44.     }
  45.     for (int i = 1; i <= n; ++i)
  46.         scanf("%d%d%d", &d[i].a, &d[i].b, &d[i].c);
  47.     for (int i = 1; i <= n; ++i)
  48.         xb[i] = d[i].b;
  49.     sort(d + 1, d + n + 1);
  50.     sort(xb + 1, xb + n + 1);
  51.     for (int i = 1; i <= n;) {
  52.         cur = i;
  53.         do {
  54.             ib = lower_bound(xb + 1, xb + n + 1, d[i].b) - xb;
  55.             change(ib, d[i].c);
  56.             ++i;
  57.         } while (i <= n && d[i].a == d[i - 1].a);
  58.         for (int j = cur; j < i; ++j) {
  59.             ib = lower_bound(xb + 1, xb + n + 1, d[j].b) - xb;
  60.             ++cnt[query(ib, d[j].c)];
  61.         }
  62.     }
  63.     for (int i = 1; i <= n; ++i)
  64.         printf("%d\n", cnt[i]);
  65.     return 0;
  66. }
  67.  
  68. inline int low_bit(int x)
  69. { return x & -x; }
  70.  
  71. bool data::operator<(const data& x) const {
  72.     if (a == x.a) {
  73.         if (b == x.b)
  74.             return c < x.c;
  75.         return b < x.b;
  76.     }
  77.     return a < x.a;
  78. }
  79.  
  80. inline void change(int b, int c) {
  81.     for (; b <= n; b += low_bit(b))
  82.         insert(t[b], create(c));
  83. }
  84.  
  85. inline int query(int b, int c) {
  86.     int ret = 0;
  87.     for (; b; b -= low_bit(b))
  88.         ret += query(t[b], c);
  89.     return ret;
  90. }
  91.  
  92. inline int query(node* p, int x) {
  93.     int ret = 0;
  94.     while (p) {
  95.         if (p->x <= x) {
  96.             ret += get_sz(p->ls) + 1;
  97.             p = p->rs;
  98.         } else
  99.             p = p->ls;
  100.     }
  101.     return ret;
  102. }
  103.  
  104. inline int get_sz(node* p)
  105. { return p ? p->cnt : 0; }
  106.  
  107. inline node* create(int x) {
  108.     static node pool[N << 5];
  109.     static node* p = pool;
  110.     ++p;
  111.     p->x = x;
  112.     p->cnt = 1;
  113.     p->rnd = low_bit(p - pool);
  114.     return p;
  115. }
  116.  
  117. void insert(node*& p, node* q) {
  118.     if (!p) {
  119.         p = q;
  120.         return;
  121.     }
  122.     if (p->x > q->x) {
  123.         insert(p->ls, q);
  124.         update(p);
  125.         if (p->rnd < p->ls->rnd)
  126.             rotate(p, 0);
  127.     } else {
  128.         insert(p->rs, q);
  129.         update(p);
  130.         if (p->rnd < p->rs->rnd)
  131.             rotate(p, 1);
  132.     }
  133. }
  134.  
  135. inline void rotate(node*& p, bool f) {
  136.     node *son = GET_CH(p, f), *pson = GET_CH(son, !f);
  137.     GET_CH(son, !f) = p;
  138.     GET_CH(p, f) = pson;
  139.     update(p);
  140.     p = son;
  141.     update(p);
  142. }
  143.  
  144. inline void update(node* p)
  145. { p->cnt = get_sz(p->ls) + get_sz(p->rs) + 1; }
Advertisement
Add Comment
Please, Sign In to add comment