Advertisement
Korotkodul

Задача A. RMQ

Nov 19th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <set>
  5. #include <string>
  6. #include <algorithm>
  7. using namespace std;
  8. using ll = long long;
  9. void cv(vector <ll> v){
  10. for (auto x: v) cout<<x<<' ';
  11. cout<<'\n';
  12. }
  13.  
  14. ll inf = pow(2, 32);
  15. vector <ll> ar(1);
  16. vector <pair<ll,ll>> qr(1);
  17. struct sct{
  18. ll l, r, mn, mx;
  19. };
  20.  
  21. void csc(sct f){
  22. cout<<f.l<<'-'<<f.r<<" ";
  23. //cout<<f.mn<<'/'<<f.mx<<'\n';
  24. }
  25. vector <vector <sct> > wrk;
  26.  
  27. vector <sct> power;
  28. sct sm;
  29. void make(){
  30. int lvl = 1, lst_lvl;
  31. int l,r;
  32. while (pow(2,lvl) <= ar.size()){
  33. lst_lvl = lvl / 2;
  34. int lst_lvl = pow(2, lst_lvl);
  35. //cout<<"lvl = "<<lvl<<'\n';
  36. vector <sct> last = wrk[wrk.size() - 1];
  37. //cv(ar);
  38. /*cout<<"last:\n";
  39. for (auto x: last) csc(x);
  40. cout<<'\n';*/
  41. int len = pow(2, lvl);
  42. int hlf = len / 2;
  43. for (int i = 0;i<=ar.size()-len;++i){
  44. //cv(ar);
  45. l = i;
  46. r = i + len - 1;
  47. //cout<<ar[l]<<' '<<ar[r]<<'\n';
  48. //cout<<"l = "<<l<<" r = "<<r<<'\n';
  49. sct L = last[l];
  50. sct R = last[l+hlf];
  51. sm = {l, r, min(L.mn, R.mn), max(L.mx, R.mx)};
  52. power.push_back(sm);
  53. /*csc(L);
  54. csc(R);
  55. csc(sm);
  56. cout<<'\n';*/
  57. }
  58. /*cout<<"power:\n";
  59. for (auto x: power){
  60. csc(x);
  61. }
  62. cout<<'\n';*/
  63. wrk.push_back(power);
  64. power.clear();
  65. //cout<<"sz = "<<wrk.size()<<'\n';
  66. //cout<<"\n\n\n\n";
  67. lvl++;
  68. }
  69. }
  70. int main()
  71. {
  72. ios::sync_with_stdio(0);
  73. cin.tie(0);
  74. cout.tie(0);
  75. ll N,M,A,B;
  76. while (true){
  77. cin>>N>>M>>A>>B;
  78. //cin>>N>>M>>A>>B;
  79. if (N==0 && M==0 && A==0 && B==0) break;
  80. ar.clear();
  81. qr.clear();
  82. ll d;
  83. ll d1, d2;
  84. for (int i = 1;i<=N+M*2;++i){
  85. if (i <= N){
  86. d = ( ((A % inf) * (i % inf)) % inf + (B % inf) ) % inf;
  87. ar.push_back(d);
  88. /* cout<<d<<'\n';
  89. if (d == rl[i-1]) cout<<"OK\n";
  90. else cout<<"NO\n";*/
  91. }
  92. else{
  93. d1 = ( ((A % inf) * (i % inf)) % inf + (B % inf) ) % inf % N;
  94. d1++;
  95. i++;
  96. d2 = ( ((A % inf) * (i % inf)) % inf + (B % inf) ) % inf % N;
  97. d2++;
  98. qr.push_back({d1, d2});
  99. /*cout<<d1<<' '<<d2<<'\n';
  100. if (d1 == qr[id].first && d2 == qr[id].second){
  101. cout<<"OK\n";
  102. }else{
  103. cout<<"NO\n";
  104. }*/
  105. //id++;
  106. }
  107. }
  108. //cout<<'\n';
  109. //cv(ar);
  110. for (int i = 0; i < ar.size();++i){
  111. sm = {i, i, ar[i], ar[i]};
  112. //cout<<ar[i]<<'\n';
  113. //csc(sm);
  114. power.push_back(sm);
  115. }
  116. wrk.push_back(power);
  117. power.clear();
  118. //cout<<'\n'<<'\n';
  119. make();
  120. ll ans;
  121. ll tot = 0;
  122. /*cout<<"wrk\n";
  123. for (int q=0;q<wrk.size();++q) {
  124. cout<<"pow= "<<q<<'\n';
  125. for (auto y: wrk[q]){
  126. csc(y);
  127. }cout<<'\n';
  128. }cout<<'\n';*/
  129. for (auto Q: qr){
  130. int a = min(Q.first, Q.second);
  131. int b = max(Q.first, Q.second);
  132. a--;
  133. b--;
  134. //cout<<a<<' '<<b<<'\n';
  135. //cout<<b - a + 1<<'\n';
  136. int LVL = floor(log(b-a+1) / log(2));
  137. //cout<<"LVL = "<<LVL<<'\n';
  138. vector <sct> now = wrk[LVL];
  139. sct lft = now[a];
  140. sct rgt = now[b - pow(2,LVL) + 1];
  141. ans = min(lft.mn, rgt.mn);
  142. tot += ans;
  143. //cout<<a<<' '<<b - pow(2,LVL) + 1<<'\n'<<'\n';
  144. }
  145. cout<<tot<<'\n';
  146. wrk.clear();
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement