fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. using namespace __gnu_pbds;
  6.  
  7. typedef long long ll;
  8. typedef pair<ll, ll> pii;
  9. random_device rd;
  10. mt19937 rng(0);
  11. const int rdseed = (uint64_t)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count();
  12.  
  13. const ll N = 1e18;
  14. const ll NUM_ELEM = 2e5;
  15. const ll NUM_QUERIES = 2e5;
  16.  
  17. uniform_int_distribution<ll> uni(1LL, N);
  18.  
  19. /***************************************************/
  20.  
  21. struct Node {
  22. Node *lp, *rp;
  23. ll sum;
  24. };
  25. inline ll eval(Node* p) {
  26. return p ? p->sum : 0LL;
  27. }
  28.  
  29. const ll bufSize = NUM_ELEM * (log2(N) + 1);
  30. Node buf[bufSize];
  31. Node *newNode() {
  32. static auto ptr = buf;
  33. return ptr++;
  34. }
  35.  
  36. ll getSum(Node* cur, ll l, ll r, ll x, ll y) {
  37. if (!cur || x > r || y < l) return 0;
  38. if (x <= l && r <= y) {
  39. return cur->sum;
  40. }
  41. ll m = (l + r) / 2;
  42. return getSum(cur->lp, l, m, x, y) + getSum(cur->rp, m + 1, r, x, y);
  43. }
  44. void update(Node*& cur, ll l, ll r, ll pos, ll val) {
  45. if (!cur) cur = newNode();
  46. if (l == r) {
  47. cur->sum = val;
  48. } else {
  49. ll m = (l + r) / 2;
  50. if (pos <= m) {
  51. update(cur->lp, l, m, pos, val);
  52. } else {
  53. update(cur->rp, m + 1, r, pos, val);
  54. }
  55. cur->sum = eval(cur->lp) + eval(cur->rp);
  56. }
  57. }
  58. Node *root;
  59.  
  60. /***************************************************/
  61.  
  62. inline int hash_f(int x) { return ((x >> 16) ^ x) * 0x45d9f3b; }
  63.  
  64. struct chash {
  65. int operator()(int x) const { return hash_f(x); }
  66. };
  67.  
  68. gp_hash_table<ll, ll, chash> seg;
  69.  
  70. ll get(ll x) {
  71. auto res = seg.find(x);
  72. return (res == seg.end()) ? 0 : res->second;
  73. // return (seg.find(x) == seg.end()) ? 0 : seg[x];
  74. }
  75. void modify(ll p, ll val) {
  76. for (seg[p += N] = val; p > 0; p >>= 1) {
  77. seg[p >> 1] = get(p) + get(p ^ 1);
  78. }
  79. }
  80.  
  81. ll query(ll l, ll r) {
  82. ll res = 0;
  83. for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
  84. if (l & 1)
  85. res += get(l++);
  86. if (r & 1)
  87. res += get(--r);
  88. }
  89. return res;
  90. }
  91.  
  92. /***************************************************/
  93.  
  94. int main() {
  95. ios::sync_with_stdio(0);
  96. cin.tie(0);
  97. vector<pii> elems;
  98. for (ll i = 0; i < NUM_ELEM; i++) {
  99. elems.push_back({uni(rng), uni(rng)});
  100. }
  101. vector<pii> queries;
  102. for (ll i = 0; i < NUM_QUERIES; i++) {
  103. ll a = uni(rng), b = uni(rng);
  104. if (a > b)
  105. swap(a, b);
  106. queries.push_back({a, b});
  107. }
  108. vector<ll> ans1, ans2;
  109. clock_t begin;
  110. begin = clock();
  111. for (auto i : elems) {
  112. modify(i.first, i.second);
  113. }
  114. for (auto i : queries) {
  115. ans2.push_back(query(i.first, i.second + 1));
  116. }
  117. cout << "policy hash: " << (double)(clock() - begin) / CLOCKS_PER_SEC << endl;
  118. seg.clear();
  119. begin = clock();
  120. for (auto i : elems) {
  121. update(root, 1, N, i.first, i.second);
  122. }
  123. for (auto i : queries) {
  124. ans1.push_back(getSum(root, 1, N, i.first, i.second));
  125. }
  126. cout << "pointer: " << (double)(clock() - begin) / CLOCKS_PER_SEC << endl;
  127.  
  128. for (int i = 0; i < ans1.size(); i++) {
  129. assert(ans1[i] == ans2[i]);
  130. }
  131. }
Success #stdin #stdout 2.42s 306496KB
stdin
Standard input is empty
stdout
policy hash: 1.71423
pointer: 0.355541