Advertisement
a53

consecutive1

a53
Aug 31st, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMAX 100001
  3. using namespace std;
  4. ifstream fin("consec.in");
  5.  
  6. int tree_min[4*NMAX];
  7. int N, Q, arr[NMAX];
  8. long long sum[NMAX];
  9. int min(int a, int b)
  10. {
  11. return a < b ? a : b;
  12. }
  13. void build_min(int arr[], int node, int left, int right)
  14. {
  15. if (left == right)
  16. tree_min[node] = arr[left];
  17. else
  18. {
  19. build_min(arr, 2 * node, left,(left + right) / 2);
  20. build_min(arr, 2 * node + 1,(left + right) / 2 + 1, right);
  21. tree_min[node] = min(tree_min[2 * node],tree_min[2 * node + 1]);
  22. }
  23. }
  24. int query_min(int node, int c_l, int c_r, int l, int r)
  25. {
  26. if (c_l > r || c_r < l)
  27. return 1e9;
  28. if (c_l >= l && c_r <= r)
  29. return tree_min[node];
  30. else
  31. return min(query_min(2 * node, c_l,(c_l + c_r) / 2, l, r),
  32. query_min(2 * node + 1,(c_l + c_r) / 2 + 1,c_r, l, r));
  33. }
  34. int main()
  35. {
  36. int i, l, r, q;
  37. cin>>N>>arr[0];
  38. sum[0]=arr[0];
  39. for(i=1; i<N; ++i)
  40. {
  41. cin>>arr[i];
  42. sum[i]=(0LL + sum[i-1]+arr[i]);
  43. }
  44. build_min(arr, 1, 0, N - 1);
  45. cin>>Q;
  46. for(q=0; q<Q; ++q)
  47. {
  48. cin>>l>>r;
  49. l--,r--;
  50. int minim=query_min(1, 0, N - 1, l, r);
  51. if(l==0)
  52. cout<<(sum[r]==1LL*(r-l+1)*minim+1LL*(r-l)*(r-l+1)/2);
  53. else
  54. cout<<(sum[r]-sum[l-1]==1LL*(r-l+1)*minim+1LL*(r-l)*(r-l+1)/2);
  55. }
  56. return 0;
  57. }
  58. /**
  59. SO
  60. #include <bits/stdc++.h>
  61. #define nmax 100001
  62. using namespace std;
  63.  
  64. int n, a[nmax], rmin[18][nmax], rmax[18][nmax];
  65. int P[nmax]; /// P[i] = j : secventa de la j la i are doar
  66. /// numere distincte
  67. int fr[nmax], lg[nmax];
  68.  
  69. void Citire()
  70. {
  71. cin >> n;
  72. for (int i = 1; i <= n; i++)
  73. cin >> a[i];
  74. }
  75.  
  76. void ConstructieP()
  77. {
  78. int i, j, x;
  79. j = 1;
  80. for (i = 1; i <= n; i++)
  81. {
  82. x = a[i];
  83. while (fr[x] != 0)
  84. {
  85. fr[a[j]]--;
  86. j++;
  87. }
  88. fr[x]++;
  89. P[i] = j;
  90. }
  91. }
  92.  
  93. void RMQ()
  94. {
  95. int i, j, k;
  96. /// lg[i] = k - exponentul maxim cu proprietatea
  97. /// ca 2^k este mai mic sau egal cu i
  98. lg[1] = 0;
  99. for (i = 2; i <= n; i++)
  100. lg[i] = lg[i / 2] + 1;
  101.  
  102. /// ---------------- rmin ------------------------
  103. for (i = 1; i <= n; i++)
  104. rmin[0][i] = a[i];
  105.  
  106. for (i = 1; (1 << i) <= n; i++)
  107. for (j = (1 << i); j <= n; j++)
  108. {
  109. k = 1 << (i - 1);
  110. rmin[i][j] = min(rmin[i - 1][j], rmin[i - 1][j - k]);
  111. }
  112.  
  113. /// ---------------- rmax ------------------------
  114. for (i = 1; i <= n; i++)
  115. rmax[0][i] = a[i];
  116.  
  117. for (i = 1; (1 << i) <= n; i++)
  118. for (j = (1 << i); j <= n; j++)
  119. {
  120. k = 1 << (i - 1);
  121. rmax[i][j] = max(rmax[i - 1][j], rmax[i - 1][j - k]);
  122. }
  123. }
  124.  
  125. void Interogari()
  126. {
  127. int Q, x, y, k, m, M;
  128. cin >> Q;
  129. while (Q--)
  130. {
  131. cin >> x >> y;
  132. if (P[y] <= x) /// secventa a[x..y] are doar nr. distincte
  133. {
  134. k=lg[y - x + 1];
  135. m = min(rmin[k][x + (1<<k) - 1], rmin[k][y]);
  136. M = max(rmax[k][x + (1<<k) - 1], rmax[k][y]);
  137. if (M - m == y - x) cout << "1";
  138. else cout << "0";
  139. }
  140. else cout << "0";
  141. }
  142. }
  143.  
  144. int main()
  145. {
  146. Citire();
  147. ConstructieP();
  148. RMQ();
  149. Interogari();
  150. return 0;
  151. }
  152. */
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement