fork(1) download
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cassert>
  4. #include <climits>
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <deque>
  11. #include <fstream>
  12. #include <functional>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <map>
  16. #include <numeric>
  17. #include <queue>
  18. #include <random>
  19. #include <set>
  20. #include <stack>
  21. #include <sstream>
  22. #include <tuple>
  23. #include <vector>
  24.  
  25. using namespace std;
  26.  
  27. #ifdef DEBUG
  28. //#define LOCAL_INPUT_FILE
  29. #else
  30. //#define USE_FILE_IO
  31. #endif
  32.  
  33. #ifdef USE_FILE_IO
  34. #define INPUT_FILE "input.txt"
  35. #define OUTPUT_FILE "output.txt"
  36. #define cin ____cin
  37. #define cout ____cout
  38. ifstream cin(INPUT_FILE);
  39. ofstream cout(OUTPUT_FILE);
  40. #else
  41. #ifdef LOCAL_INPUT_FILE
  42. #define cin ____cin
  43. ifstream cin("input.txt");
  44. #endif
  45. #endif
  46.  
  47. const int infinity = (int)1e9 + 42;
  48. const int64_t llInfinity = (int64_t)1e18 + 256;
  49. const int module = (int)1e9 + 7;
  50. const long double eps = 1e-8;
  51.  
  52. inline void raiseError(string errorCode) {
  53. cerr << "Error : " << errorCode << endl;
  54. exit(42);
  55. }
  56.  
  57. int block = 200;
  58.  
  59. struct Query {
  60. int l, r, idx;
  61.  
  62. inline pair<int, int> toPair() const {
  63. return make_pair(l / block, ((l / block) & 1) ? -r : +r);
  64. }
  65. };
  66.  
  67. inline bool operator<(const Query &a, const Query &b) {
  68. return a.toPair() < b.toPair();
  69. }
  70.  
  71. signed main() {
  72. #ifndef USE_FILE_IO
  73. ios_base::sync_with_stdio(false);
  74. #endif
  75.  
  76. mt19937 rnd(42);
  77.  
  78. int n, m, k; cin >> n >> m; k = rnd() % 1048576;
  79. vector<int> p(n+1);
  80. for (int i = 0; i < n; i++) {
  81. int val = rnd() % 1048576;
  82. p[i+1] = p[i] ^ val;
  83. }
  84. block = n / sqrt(m) + 1;
  85.  
  86. vector<Query> qry(m);
  87. for (int i = 0; i < m; i++) {
  88. int l = rnd() % n + 1, r = rnd() % n + 1;
  89. if (l > r) {
  90. swap(l, r);
  91. }
  92. qry[i].l = l; qry[i].r = r;
  93. qry[i].idx = i;
  94. }
  95.  
  96. int64_t ans = 0;
  97. vector<int64_t> res(m);
  98. vector<int64_t> cnt((int)2e6, 0);
  99. sort(qry.begin(), qry.end());
  100. int l = 0, r = 1;
  101. ans = (p[1] == k);
  102. cnt[p[0]]++; cnt[p[1]]++;
  103.  
  104. for (Query q: qry) {
  105. while (l > q.l) {
  106. l--;
  107. ans += cnt[p[l] ^ k];
  108. cnt[p[l]]++;
  109. }
  110. while (r < q.r) {
  111. r++;
  112. ans += cnt[p[r] ^ k];
  113. cnt[p[r]]++;
  114. }
  115. while (l < q.l) {
  116. cnt[p[l]]--;
  117. ans -= cnt[p[l] ^ k];
  118. l++;
  119. }
  120. while (r > q.r) {
  121. cnt[p[r]]--;
  122. ans -= cnt[p[r] ^ k];
  123. r--;
  124. }
  125. res[q.idx] = ans;
  126. }
  127.  
  128. uint64_t rhsh = 0;
  129. for (int i = 0; i < m; i++) {
  130. rhsh *= (uint64_t)1e9 + 7;
  131. rhsh += (uint64_t)res[i];
  132. }
  133. cout << rhsh << "\n";
  134.  
  135. return 0;
  136. }
  137.  
Success #stdin #stdout 0.01s 18264KB
stdin
Standard input is empty
stdout
0