fork download
  1. // Count of numbers of [a,b] range in [L, R] index
  2. #define nsz 100010
  3. #define tsz 6000010 //take 4n + qlogn
  4. ll a[nsz];
  5. ll NEXT_FREE;
  6. ll version[nsz];
  7. ll val[tsz], Left[tsz], Right[tsz];
  8. void build(ll node, ll lo, ll hi)
  9. {
  10. if(lo == hi) // leaf node
  11. {
  12. val[node] = 0;
  13. return;
  14. }
  15. Left[node] = NEXT_FREE++;
  16. Right[node] = NEXT_FREE++;
  17. ll mid = (lo + hi) >> 1;
  18. build(Left[node], lo, mid);
  19. build(Right[node], mid+1, hi);
  20. val[node] = val[Left[node]] + val[Right[node]];
  21. }
  22.  
  23. ll update(ll node, ll lo, ll hi, ll idx, ll v)
  24. {
  25. if(lo > idx || hi < idx)
  26. return node; // Out of range, use this node.
  27.  
  28. ll nnode = NEXT_FREE++; //Creating a new node, as idx is in [l, r]
  29.  
  30. if (lo == hi) // Leaf Node, create new node and return that.
  31. {
  32. val[nnode] = val[node]; //cloning current old leaf node's value to new leaf node
  33. val[nnode] += v; // adding or subtracting or replacing as needed
  34. return nnode;
  35. }
  36. ll mid = (lo + hi) >> 1;
  37. // Left[nnode] is new node's Left child, it might end up being the old one too
  38. // Left[node] is current old node's Left child.
  39. // So we call to update idx in Left child of old node.
  40. // And use it's return node as new node's Left child. Same for Right.
  41. Left[nnode] = update(Left[node], lo, mid, idx, v);
  42. Right[nnode] = update(Right[node], mid+1, hi, idx, v);
  43. val[nnode] = val[Left[nnode]] + val[Right[nnode]]; // Update value.
  44. return nnode; // Return the new node to parent.
  45. }
  46.  
  47. ll query(ll lnode, ll rnode, ll lo, ll hi, ll l, ll r)
  48. {
  49. if(lo > r || hi < l)
  50. return 0;
  51.  
  52. if (lo >= l && hi <= r)
  53. return val[rnode] - val[lnode];
  54.  
  55. ll mid = (lo + hi) >> 1;
  56.  
  57. return query(Left[lnode], Left[rnode], lo, mid, l, r)
  58. + query(Right[lnode], Right[rnode], mid+1, hi, l, r);
  59. }
  60.  
  61. // NEXT_FREE = 0
  62. // version[0] = NEXT_FREE++
  63. // build(version[0], 1, n)
  64.  
  65. // upd(ara[i], 1) -> increment the frequency
  66. // So, version[i] = update(version[i-1], 1, n, i, 1)
  67.  
  68. // query: Count of numbers of [a,b] range in [L, R] index
  69. // ans = query(version[l-1], version[r], 1, n, L, R)
  70.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:1: error: ‘ll’ does not name a type
 ll a[nsz];
 ^~
prog.cpp:5:1: error: ‘ll’ does not name a type
 ll NEXT_FREE;
 ^~
prog.cpp:6:1: error: ‘ll’ does not name a type
 ll version[nsz];
 ^~
prog.cpp:7:1: error: ‘ll’ does not name a type
 ll val[tsz], Left[tsz], Right[tsz];
 ^~
prog.cpp:8:12: error: variable or field ‘build’ declared void
 void build(ll node, ll lo, ll hi)
            ^~
prog.cpp:8:12: error: ‘ll’ was not declared in this scope
prog.cpp:8:21: error: ‘ll’ was not declared in this scope
 void build(ll node, ll lo, ll hi)
                     ^~
prog.cpp:8:28: error: ‘ll’ was not declared in this scope
 void build(ll node, ll lo, ll hi)
                            ^~
prog.cpp:23:1: error: ‘ll’ does not name a type
 ll update(ll node, ll lo, ll hi, ll idx, ll v)
 ^~
prog.cpp:47:1: error: ‘ll’ does not name a type
 ll query(ll lnode, ll rnode, ll lo, ll hi, ll l, ll r)
 ^~
stdout
Standard output is empty