Advertisement
tsypko

Untitled

Mar 2nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/rope>
  3. using namespace std;
  4. using namespace __gnu_cxx;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef unsigned int ui;
  8. typedef long double ld;
  9. typedef pair<int, int> ii;
  10. typedef pair<ii, ii> iii;
  11. ll MOD = 1e9 + 9;
  12. const ld E = 1e-7;
  13. #define null NULL
  14. #define ms(x) memset(x, 0, sizeof(x))
  15. #ifndef LOCAL
  16. #define endl "\n"
  17. #endif
  18. #ifndef LONG_LONG_MAX
  19. #define LONG_LONG_MAX LLONG_MAX
  20. #endif
  21. #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  22. #define _read(x) freopen(x, "r", stdin)
  23. #define _write(x) freopen(x, "w", stdout)
  24. #define files(x) _read(x ".in"); _write(x ".out")
  25. #define filesdatsol(x) _read(x ".dat"); _write(x ".sol")
  26. #define output _write("output.txt")
  27. #define input _read("input.txt")
  28. #define prev time_prev
  29. #define M_PI acos(-1)
  30. #define remove tipa_remove
  31. #define left tipa_left
  32. #define right tipa_right
  33. #define next tipa_next
  34. #define mod % MOD
  35. #define y1 hello_world
  36. unsigned char ccc;
  37. bool _minus = false;
  38. template<typename T>
  39. inline T sqr(T t) {
  40. return (t * t);
  41. }
  42. inline void read(ll &n) {
  43. n = 0;
  44. _minus = false;
  45. while (true) {
  46. ccc = getchar();
  47. if (ccc == ' ' || ccc == '\n')
  48. break;
  49. if (ccc == '-') {
  50. _minus = true;
  51. continue;
  52. }
  53. n = n * 10 + ccc - '0';
  54. }
  55. if (_minus)
  56. n *= -1;
  57. }
  58. inline void read(int &n) {
  59. n = 0;
  60. _minus = false;
  61. while (true) {
  62. ccc = getchar();
  63. if (ccc == ' ' || ccc == '\n')
  64. break;
  65. if (ccc == '-') {
  66. _minus = true;
  67. continue;
  68. }
  69. n = n * 10 + ccc - '0';
  70. }
  71. if (_minus)
  72. n *= -1;
  73. }
  74. char wwww[19];
  75. int kkkk;
  76. inline void write(ll y) {
  77. long long x = y;
  78. kkkk = 0;
  79. if (x < 0) {
  80. putchar('-');
  81. x *= -1;
  82. }
  83. if (!x)
  84. ++kkkk, wwww[kkkk] = '0';
  85. else
  86. while (x) {
  87. ++kkkk;
  88. wwww[kkkk] = char(x % 10 + '0');
  89. x /= 10;
  90. }
  91. for (int i = kkkk; i >= 1; --i)
  92. putchar(wwww[i]);
  93. }
  94.  
  95. #ifdef LOCAL
  96. #define DEBUG
  97. #endif
  98.  
  99. #ifdef DEBUG
  100. #define dbg if(1)
  101. #else
  102. #define dbg if(0)
  103. #endif
  104.  
  105. const int MAX = 5e5 + 10;
  106. const int MAX_LOG = 20;
  107. int _log2[MAX];
  108. int ar[MAX];
  109. int t1[MAX_LOG][MAX], t2[MAX_LOG][MAX];
  110. int n;
  111.  
  112. void build() {
  113. for (int i = 2; i < MAX; i++) {
  114. _log2[i] = _log2[i >> 1] + 1;
  115. }
  116. for (int i = 1; i <= n; i++) {
  117. cin >> ar[i];
  118. t1[0][i] = t2[0][i] = ar[i];
  119. }
  120. for (int i = 1; i < MAX_LOG; i++) {
  121. for (int j = 1; j + (1 << i) - 1 <= n; j++) {
  122. t1[i][j] = max(t1[i - 1][j], t1[i - 1][j + (1 << (i - 1))]);
  123. t2[i][j] = min(t2[i - 1][j], t2[i - 1][j + (1 << (i - 1))]);
  124. }
  125. }
  126. }
  127.  
  128. int get(int l, int r) {
  129. int i = _log2[r - l + 1];
  130. return max(t1[i][l], t1[i][r - (1 << i) + 1])
  131. - min(t2[i][l], t2[i][r - (1 << i) + 1]);
  132. }
  133.  
  134. ll solve(int a) {
  135. ll ans = 0;
  136. for (int i = 1; i <= n; i++) {
  137. int l = i, r = n;
  138. while (l < r) {
  139. int x = (l + r + 1) >> 1;
  140. if (get(i, x) <= a) {
  141. l = x;
  142. } else {
  143. r = x - 1;
  144. }
  145. }
  146. ans += l - i + 1;
  147. }
  148. return ans;
  149. }
  150.  
  151. ll solve(int a, int b) {
  152. return solve(b) - solve(a - 1);
  153. }
  154.  
  155. int solve_long(int a, int b) {
  156. int ans = 0;
  157. for (int i = 1; i <= n; i++) {
  158. int L = 0, R = 1e9;
  159. for (int j = i; j <= n; j++) {
  160. L = max(L, ar[j]);
  161. R = min(R, ar[j]);
  162. if (a <= L - R && L - R <= b) {
  163. ans++;
  164. }
  165. }
  166. }
  167. return ans;
  168. }
  169.  
  170. int main() {
  171. sync;
  172. srand(time(NULL));
  173. cout.precision(3);
  174. cout << fixed;
  175. #ifdef LOCAL
  176. input;
  177. #else
  178.  
  179. #endif
  180.  
  181. int q;
  182. cin >> n >> q;
  183. build();
  184.  
  185. while(q--) {
  186. int a, b;
  187. cin >> a >> b;
  188. cout << solve(a, b) << endl;
  189. }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement