Advertisement
libobil

Untitled

Aug 29th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <unordered_map>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cassert>
  5. #include <numeric>
  6. #include <vector>
  7.  
  8. typedef long long llong;
  9. const int MAXN = 400000 + 10;
  10. const int MAXLOG = 6;
  11. const int BASE = 10;
  12.  
  13. std::unordered_map < llong,int > cnt;
  14. int sparse[MAXLOG][MAXN];
  15. int n, q;
  16.  
  17. void buildSparse()
  18. {
  19. for (int log = 1 ; log <= MAXLOG-1 ; ++log)
  20. {
  21. sparse[log][0] = -1;
  22. for (int i = 1 ; i <= n ; ++i)
  23. {
  24. sparse[log][i] = i;
  25. for (int j = 1 ; j <= BASE ; ++j)
  26. {
  27. sparse[log][i] = sparse[log-1][sparse[log][i]];
  28. if (sparse[log][i] == -1) break;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. int lifting(int from, int to)
  35. {
  36. int ans = 0;
  37. int pw = 100000;
  38. for (int log = MAXLOG-1 ; log >= 0 ; --log)
  39. {
  40. while (sparse[log][from] >= to-1)
  41. {
  42. ans += pw;
  43. from = sparse[log][from];
  44. }
  45.  
  46. pw /= BASE;
  47. }
  48.  
  49. return ans;
  50. }
  51.  
  52. void solve()
  53. {
  54. int l, r;
  55. buildSparse();
  56. std::cin >> q;
  57. for (int i = 1 ; i <= q ; ++i)
  58. {
  59. std::cin >> l >> r;
  60. std::cout << lifting(r, l) << '\n';
  61. }
  62. }
  63.  
  64. void read()
  65. {
  66. cnt[0] = 1;
  67. sparse[0][0] = -1;
  68.  
  69. int curr;
  70. llong sum = 0;
  71. std::cin >> n;
  72. for (int i = 1 ; i <= n ; ++i)
  73. {
  74. std::cin >> curr;
  75. sum += curr;
  76.  
  77. sparse[0][i] = std::max(sparse[0][i-1], cnt[sum] - 1);
  78. cnt[sum] = i + 1;
  79. }
  80. }
  81.  
  82. void fastIO()
  83. {
  84. std::ios_base :: sync_with_stdio(0);
  85. std::cout.tie(nullptr);
  86. std::cin.tie(nullptr);
  87. }
  88.  
  89. int main()
  90. {
  91. fastIO();
  92. read();
  93. solve();
  94.  
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement