EntropyIncreaser

SHOI2007 Tree

Nov 17th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. #include <algorithm>
  4.  
  5. #define LOG(FMT...) fprintf(stderr, FMT)
  6.  
  7. using namespace std;
  8.  
  9. struct node {
  10.     int x, y, id, type;
  11.  
  12.     bool operator<(const node& x) const {
  13.         if (y == x.y)
  14.             return node::x < x.x;
  15.         return y < x.y;
  16.     }
  17. };
  18.  
  19. const int N = 500010;
  20.  
  21. int n, m;
  22.  
  23. int a[N], px[N], ans[N];
  24. node t[N * 5];
  25.  
  26. inline int low_bit(int x) { return x & -x; }
  27. void change(int x);
  28. int query(int x);
  29.  
  30. int main() {
  31. #ifdef USE_CODE_RUNNER
  32.     freopen("test.in", "r", stdin);
  33. #endif
  34.  
  35.     int x1, x2, y1, y2, i1, i2, i3, i4, rep;
  36.     scanf("%d%d", &n, &m);
  37.     for (int i = 1; i <= n; ++i) {
  38.         scanf("%d%d", &t[i].x, &t[i].y);
  39.         px[i] = t[i].x;
  40.     }
  41.     sort(px + 1, px + n + 1);
  42.     for (int i = 1; i <= n; ++i)
  43.         t[i].x = lower_bound(px + 1, px + n + 1, t[i].x) - px;
  44.     for (int i = 1; i <= m; ++i) {
  45.         scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  46.         i1 = i * 4 - 3 + n;
  47.         i2 = i * 4 - 2 + n;
  48.         i3 = i * 4 - 1 + n;
  49.         i4 = i * 4 + n;
  50.         t[i1].id = t[i2].id = t[i3].id = t[i4].id = i;
  51.         t[i1].x = t[i3].x = lower_bound(px + 1, px + n + 1, x1) - px - 1;
  52.         t[i2].x = t[i4].x = upper_bound(px + 1, px + n + 1, x2) - px - 1;
  53.         t[i1].y = t[i2].y = y1 - 1;
  54.         t[i3].y = t[i4].y = y2;
  55.         t[i2].type = t[i3].type = -1;
  56.         t[i1].type = t[i4].type = 1;
  57.     }
  58.     rep = n + m * 4;
  59.     stable_sort(t + 1, t + rep + 1);
  60.     for (int i = 1; i <= rep; ++i)
  61.         if (!t[i].type)
  62.             change(t[i].x);
  63.         else
  64.             ans[t[i].id] += t[i].type * query(t[i].x);
  65.     for (int i = 1; i <= m; ++i)
  66.         printf("%d\n", ans[i]);
  67.     return 0;
  68. }
  69.  
  70. inline void change(int x) {
  71.     for (; x <= n; x += low_bit(x))
  72.         ++a[x];
  73. }
  74.  
  75. inline int query(int x) {
  76.     int ret = 0;
  77.     for (; x; x -= low_bit(x))
  78.         ret += a[x];
  79.     return ret;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment