EntropyIncreaser

NOI2017D1T1 Integer

Nov 17th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdint>
  3. #include <cstddef>
  4.  
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. struct node {
  10.     int l, r;
  11.     node *ls, *rs;
  12.     uint64_t d;
  13.  
  14.     void push_down();
  15.     void update();
  16.     bool plus(int x, uint64_t y);
  17.     bool plus();
  18.     bool minus(int x, uint64_t y);
  19.     bool minus();
  20.     uint64_t query(int x);
  21. };
  22.  
  23. const int N = 1000010;
  24.  
  25. int n;
  26. node* root;
  27.  
  28. node* build_tree(int l, int r);
  29.  
  30. int main() {
  31.     int op, b, rest;
  32.     long long a;
  33.     {
  34.         int ig;
  35.         scanf("%d%d%d%d", &n, &ig, &ig, &ig);
  36.     }
  37.     root = build_tree(0, max(52, (n >> 1) + 2));
  38.     while (n--) {
  39.         scanf("%d%lld", &op, &a);
  40.         if (op == 1) {
  41.             scanf("%d", &b);
  42.             rest = b & 63;
  43.             b >>= 6;
  44.             if (a < 0) {
  45.                 a = -a;
  46.                 if (rest) {
  47.                     root->minus(b, (uint64_t)a << rest);
  48.                     root->minus(b + 1, a >> (64 - rest));
  49.                 } else
  50.                     root->minus(b, a);
  51.             } else if (rest) {
  52.                 root->plus(b, (uint64_t)a << rest);
  53.                 root->plus(b + 1, a >> (64 - rest));
  54.             } else
  55.                 root->plus(b, a);
  56.         } else
  57.             printf("%d\n", bool(root->query(a >> 6) & (1ULL << (a & 63))));
  58.     }
  59.     return 0;
  60. }
  61.  
  62. node* build_tree(int l, int r) {
  63.     static node pool[N];
  64.     static int top;
  65.     node* p = pool + ++top;
  66.     p->l = l;
  67.     p->r = r;
  68.     if (l == r)
  69.         return p;
  70.     int mid = (l + r) >> 1;
  71.     p->ls = build_tree(l, mid);
  72.     p->rs = build_tree(mid + 1, r);
  73.     return p;
  74. }
  75.  
  76. inline void node::push_down() {
  77.     if (d == 0)
  78.         ls->d = rs->d = 0;
  79.     else if (d == UINT64_MAX)
  80.         ls->d = rs->d = UINT64_MAX;
  81. }
  82.  
  83. inline void node::update() {
  84.     if (ls->d == rs->d)
  85.         d = ls->d;
  86.     else d = 1;
  87. }
  88.  
  89. inline uint64_t node::query(int x) {
  90.     if (l == r)
  91.         return d;
  92.     push_down();
  93.     if (ls->r >= x)
  94.         return ls->query(x);
  95.     return rs->query(x);
  96. }
  97.  
  98. bool node::plus(int x, uint64_t y) {
  99.     if (l == r) {
  100.         uint64_t cur = d;
  101.         d += y;
  102.         return cur > d;
  103.     }
  104.     push_down();
  105.     if (ls->r >= x) {
  106.         if (ls->plus(x, y))
  107.             if (rs->plus()) {
  108.                 update();
  109.                 return true;
  110.             }
  111.         update();
  112.         return false;
  113.     }
  114.     if (rs->plus(x, y)) {
  115.         update();
  116.         return true;
  117.     }
  118.     update();
  119.     return false;
  120. }
  121.  
  122. bool node::minus(int x, uint64_t y) {
  123.     if (l == r) {
  124.         uint64_t cur = d;
  125.         d -= y;
  126.         return cur < d;
  127.     }
  128.     push_down();
  129.     if (ls->r >= x) {
  130.         if (ls->minus(x, y))
  131.             if (rs->minus()) {
  132.                 update();
  133.                 return true;
  134.             }
  135.         update();
  136.         return false;
  137.     }
  138.     if (rs->minus(x, y)) {
  139.         update();
  140.         return true;
  141.     }
  142.     update();
  143.     return false;
  144. }
  145.  
  146. bool node::plus() {
  147.     if (d == UINT64_MAX) {
  148.         d = 0;
  149.         return true;
  150.     }
  151.     if (l == r) {
  152.         ++d;
  153.         return false;
  154.     }
  155.     if (!ls->plus()) {
  156.         update();
  157.         return false;
  158.     }
  159.     bool f = rs->plus();
  160.     update();
  161.     return f;
  162. }
  163.  
  164. bool node::minus() {
  165.     if (!d) {
  166.         d = UINT64_MAX;
  167.         return true;
  168.     }
  169.     if (l == r) {
  170.         --d;
  171.         return false;
  172.     }
  173.     if (!ls->minus()) {
  174.         update();
  175.         return false;
  176.     }
  177.     bool f = rs->minus();
  178.     update();
  179.     return f;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment