Advertisement
Guest User

Персистентное дерево, с которым я упоролась

a guest
Dec 4th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. size_t w, h;
  12.  
  13. struct point {
  14.     size_t x, y;
  15. };
  16.  
  17. bool operator<(const point & p1, const point & p2) {
  18.     return p1.x < p2.x;
  19. }
  20.  
  21. struct vertex {
  22.     vertex * l, * r;
  23.     size_t sum;
  24.  
  25.     vertex(size_t val)
  26.         : l(NULL), r(NULL), sum(val)
  27.     { }
  28.  
  29.     vertex(vertex * l, vertex * r)
  30.         : l(l), r(r), sum(0)
  31.     {
  32.         if (l)  sum += l->sum;
  33.         if (r)  sum += r->sum;
  34.     }
  35. };
  36.  
  37. vertex * build(size_t tl, size_t tr) {
  38.     if (tl == tr)
  39.         return new vertex(0);
  40.     int tm = (tl + tr) >> 1;
  41.     return new vertex(
  42.         build(tl, tm),
  43.         build(tm + 1, tr)
  44.         );
  45. }
  46.  
  47. int get_sum(vertex * t, size_t tl, size_t tr, size_t l, size_t r) {
  48.     if (l > r)
  49.         return 0;
  50.     if (l == tl && tr == r)
  51.         return t->sum;
  52.     size_t tm = (tl + tr) >> 1;
  53.     return get_sum(t->l, tl, tm, l, min(r, tm)) + get_sum(t->r, tm + 1, tr, max(l, tm + 1), r);
  54. }
  55.  
  56. vertex * update(vertex * t, size_t tl, size_t tr, size_t pos, size_t new_val) {
  57.     if (tl == tr)
  58.         return new vertex(new_val);
  59.     size_t tm = (tl + tr) >> 1;
  60.     if (pos <= tm)
  61.         return new vertex(
  62.             update(t->l, tl, tm, pos, new_val),
  63.             t->r
  64.             );
  65.     else
  66.         return new vertex(
  67.             t->l,
  68.             update(t->r, tm + 1, tr, pos, new_val)
  69.             );
  70. }
  71.  
  72. void next_coord(size_t k, size_t & xlp, size_t & xhp, size_t & ylp, size_t & yhp) {
  73.     size_t x1 = ((xlp + k) * 13 + 7) % (w + 1);
  74.     size_t x2 = ((xhp + k) * 7 + 13) % (w + 1);
  75.     size_t y1 = ((ylp + k) * 13 + 7) % (h + 1);
  76.     size_t y2 = ((yhp + k) * 7 + 13) % (h + 1);
  77.     xlp = min(x1, x2);
  78.     xhp = max(x1, x2);
  79.     ylp = min(y1, y2);
  80.     yhp = max(y1, y2);
  81. }
  82.  
  83. int main() {
  84.     ios::sync_with_stdio(false);
  85.     cin.tie(nullptr);
  86.  
  87.     int n = 0;
  88.     cin >> w >> h >> n;
  89.    
  90.     vertex ** persistent_tree = new vertex *[w + 1];
  91.  
  92.     vector<point> sky = vector<point>(n);
  93.     size_t x, y;
  94.     for (int i = 0; i < n; ++i) {
  95.         cin >> sky[i].x >> sky[i].y;
  96.     }
  97.  
  98.     sort(sky.begin(), sky.end());
  99.     persistent_tree[0] = build(0, h);
  100.     int ind = 0;
  101.     for (int i = 0; i <= w; ++i) {
  102.         if (i != 0)
  103.             persistent_tree[i] = persistent_tree[i - 1];
  104.         while (ind < sky.size() && sky[ind].x == i) {
  105.             int old = get_sum(persistent_tree[i], 0, h, sky[ind].y, sky[ind].y);
  106.             persistent_tree[i] = update(persistent_tree[i], 0, h, sky[ind].y, old + 1);
  107.             ++ind;
  108.         }
  109.     }
  110.  
  111.     size_t Q;
  112.     cin >> Q;
  113.     size_t xl = w >> 1;
  114.     size_t xh = w;
  115.     size_t yl = h >> 1;
  116.     size_t yh = h;
  117.  
  118.     vector<size_t> answer = vector<size_t>(Q);
  119.     answer[0] = get_sum(persistent_tree[xh], 0, h, yl, yh) - get_sum(persistent_tree[xl - 1], 0, h, yl, yh);
  120.  
  121.     for (int i = 1; i < Q; ++i) {
  122.         next_coord(answer[i - 1], xl, xh, yl, yh);
  123.         if (xl != 0)
  124.             answer[i] = get_sum(persistent_tree[xh], 0, h, yl, yh) - get_sum(persistent_tree[xl - 1], 0, h, yl, yh);
  125.         else
  126.             answer[i] = get_sum(persistent_tree[xh], 0, h, yl, yh);
  127.     }
  128.     delete[] persistent_tree;
  129.  
  130.     for (size_t i = 0; i < answer.size(); ++i) {
  131.         cout << answer[i] << endl;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement