Korotkodul

Корневая декомпозиция

Nov 16th, 2021 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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 <int> v){
  10. for (auto x: v) cout<<x<<' ';
  11. cout<<'\n';
  12. }
  13.  
  14. vector <int> a = { 0,1,2,3,4,5,6,7,8,9 ,10};
  15.  
  16. int N = -1;
  17.  
  18. struct sct{
  19. int mn, mx, sum, zr;
  20. };
  21.  
  22. void csc(sct x){
  23. cout<<x.mn<<'/'<<x.mx<<'/'<<x.sum<<'\n';
  24. }
  25. vector <sct> wrk(1);
  26.  
  27.  
  28. int s = -2;
  29. int inf = 2e9;
  30.  
  31. int mini_bug(int L, int R){
  32. cout<<"LR = "<<L<<" "<<R<<'\n';
  33. int mn = inf;
  34. cout<<"ONE\n";
  35. while (L % s != 0 && L <= R){
  36. cout<<"L = "<<L<<'\n';
  37. cout<<"a[L] = "<<a[L]<<'\n';
  38. mn = min(mn, a[L]);
  39. cout<<"mn = "<<mn<<'\n';
  40. L++;
  41. }
  42. cout<<"TWO\n";
  43. while ((L + s - 1) <= R && L <= R){
  44. cout<<"L = "<<L<<'\n';
  45. cout<<"a[L] = "<<a[L]<<'\n';
  46. int id = L / s;
  47. cout<<"id = "<<id<<'\n';
  48. cout<<"wrk[id].mn = "<<wrk[id].mn<<'\n';
  49. mn = min(mn, wrk[id].mn);
  50. cout<<"mn = "<<mn<<'\n';
  51. L += s;
  52. }
  53. cout<<"THREE\n";
  54. while (L <= R){
  55. cout<<"L = "<<L<<'\n';
  56. cout<<"a[L] = "<<a[L]<<'\n';
  57. mn = min(mn, a[L]);
  58. cout<<"mn = "<<mn<<'\n';
  59. L++;
  60. }
  61.  
  62. return mn;
  63. }
  64.  
  65. int mini(int L, int R){
  66. int mn = inf;
  67. while (L % s != 0 && L <= R){
  68. mn = min(mn, a[L]);
  69. L++;
  70. }
  71. while ((L + s - 1) <= R && L <= R){
  72. int id = L / s;
  73. mn = min(mn, wrk[id].mn);
  74. L += s;
  75. }
  76. while (L <= R){
  77. mn = min(mn, a[L]);
  78. L++;
  79. }
  80.  
  81. return mn;
  82. }
  83.  
  84. int maxi(int L, int R){
  85. int mx = -inf;
  86. while (L % s != 0 && L <= R){
  87. mx = max(mx, a[L]);
  88. L++;
  89. }
  90. while ((L + s - 1) <= R && L <= R){
  91. int id = L / s;
  92. mx = max(mx, wrk[id].mx);
  93. L += s;
  94. }
  95. while (L <= R){
  96. mx = max(mx, a[L]);
  97. L++;
  98. }
  99. return mx;
  100. }
  101.  
  102. int sumi(int L, int R){
  103. int sum = 0;
  104. while (L % s != 0 && L<=R){
  105. sum += a[L];
  106. L++;
  107. }
  108. while ((L + s - 1) <= R && L<=R){
  109. int id = L / s;
  110. sum += wrk[id].sum;
  111. L += s;
  112. }
  113. while (L <= R){
  114. sum += a[L];
  115. L++;
  116. }
  117. return sum;
  118. }
  119.  
  120. int zeros(int L, int R){
  121. int zr = 0;
  122. while (L % s != 0 && L<=R){
  123. if (a[L] == 0) zr++;
  124. L++;
  125. }
  126. while ((L + s - 1) <= R && L<=R){
  127. int id = L / s;
  128. zr += wrk[id].zr;
  129. L += s;
  130. }
  131. while (L <= R){
  132. if (a[L] == 0) zr++;
  133. L++;
  134. }
  135. return zr;
  136. }
  137.  
  138.  
  139. int main()
  140. {
  141. ios::sync_with_stdio(0);
  142. cin.tie(0);
  143. cout.tie(0);
  144. cin>>N;
  145. a.resize(N);
  146. for (int &x: a) cin>>x;
  147. /*for (int i =0;i<N;++i){
  148. a[i] = i;
  149. }*/
  150. int K;
  151. cin>>K;
  152. //reverse(a.begin(), a.end());
  153. s = sqrt(N);
  154. //cout<<s<<'\n';
  155. double Nd = N, sd = s;
  156. double q = Nd/sd;
  157. //cout<<q<<'\n';
  158. int sz = ceil(q);
  159. //cout<<sz<<'\n';
  160. wrk.resize(sz);
  161. int id = -9;
  162. for (int i = 0;i<sz;++i){
  163. sct sm = {inf, -inf, 0, 0};
  164. for (int j =0;j<s && i*s+j<N;++j){
  165. id = i*s+j;
  166. sm.mx = max(sm.mx, a[id]);
  167. sm.mn = min(sm.mn, a[id]);
  168. sm.sum += a[id];
  169. if (a[id] == 0) sm.zr++;
  170. //cout<<a[id]<<' ';
  171. }/*cout<<'\n';
  172. csc(sm);
  173. cout<<'\n';*/
  174. wrk[i] = sm;
  175. //cout<<'\n';
  176. }
  177. //for (auto x: wrk) csc(x);
  178.  
  179. int l,r;
  180. vector <int> ans;
  181. for (int i=0; i<K; ++i){
  182. cin>>l>>r;
  183. l--;
  184. r--;
  185. //cout<<maxi(l,r)<<' ';
  186. //ans.push_back(maxi(l,r));
  187. cout<<zeros(l,r)<<' ';
  188. }
  189. //cv(ans);
  190. }
  191.  
  192.  
  193.  
  194. /*
  195. 10 1
  196. 3 5 7 9 2 4 6 8 0 -1
  197. 3 5 7 9 2 4 6 0 0 -1
  198.  
  199. 10 2
  200. 3 5 7 9 2 4 6 8 0 -1
  201. */
  202.  
  203.  
  204.  
Add Comment
Please, Sign In to add comment