fork download
  1. #include "iostream"
  2. #include "algorithm"
  3. #include "vector"
  4. #include "set"
  5. #include "map"
  6. #include "cstring"
  7. #include "string"
  8. #include "vector"
  9. #include "cassert"
  10. #include "queue"
  11. #include "cstdio"
  12. #include "cstdlib"
  13.  
  14. using namespace std;
  15.  
  16. typedef long long ll;
  17. typedef pair < int, int > ii;
  18.  
  19. const int N = 1e6 + 5;
  20.  
  21. int n, m, cnt, tick;
  22. int a[N], gobig[N], gosmall[N], h[N], st[N], nd[N], root[N];
  23. vector < int > g[N];
  24. vector < ii > qu[N];
  25. int fen[N], ans[N];
  26.  
  27. void dfs(int p, int x) {
  28. st[x] = ++tick;
  29. h[x] = 1;
  30. for(auto u : g[x])
  31. if(u != p)
  32. dfs(x, u);
  33. nd[x] = tick;
  34. }
  35.  
  36. inline void up(int x, int y, int k) {
  37. //printf("x = %d y = %d k = %d\n", x, y, k);
  38. for(; y; y -= y & -y)
  39. fen[y] += k;
  40. for(x--; x; x -= x & -x)
  41. fen[x] -= k;
  42. }
  43.  
  44. inline int get(int x) {
  45. int res = 0;
  46. for(; x < N; x += x & -x)
  47. res += fen[x];
  48. return res;
  49. }
  50.  
  51. void dfs2(int p, int x) {
  52. int y = x > n ? x - n : x + n;
  53. //printf("x = %d y = %d\n", x, y);
  54. up(st[x], nd[x], +1);
  55. up(st[y], nd[y], +1);
  56. for(auto u : qu[x]) {
  57. //printf("id = %d res = %d\n", u.second, get(st[u.first]));
  58. ans[u.second] = get(st[u.first]);
  59. }
  60. h[x] = 1;
  61. for(auto u : g[x])
  62. if(u != p)
  63. dfs2(x, u);
  64. //printf("del x = %d y = %d\n", x, y);
  65. up(st[x], nd[x], -1);
  66. up(st[y], nd[y], -1);
  67. }
  68.  
  69. int f(int x) {
  70. if(x != root[x])
  71. return root[x] = f(root[x]);
  72. return x;
  73. }
  74.  
  75. int main() {
  76.  
  77. scanf("%d", &n);
  78.  
  79. for(int i = 1; i <= n; i++) {
  80. scanf("%d", a + i);
  81. }
  82.  
  83. vector < ii > v;
  84.  
  85. for(int i = n; i >= 1; i--) {
  86. while(!v.empty() and v.back().first <= a[i])
  87. v.pop_back();
  88. if(v.size()) {
  89. gobig[i] = v.back().second;
  90. }
  91. v.push_back(ii(a[i], i));
  92. }
  93.  
  94. v.clear();
  95.  
  96. for(int i = n; i >= 1; i--) {
  97. while(!v.empty() and v.back().first >= a[i])
  98. v.pop_back();
  99. if(v.size()) {
  100. gosmall[i] = v.back().second;
  101. }
  102. v.push_back(ii(a[i], i));
  103. }
  104.  
  105. for(int i = 1; i <= n + n; i++)
  106. root[i] = i;
  107.  
  108. for(int i = 1; i <= n; i++) {
  109. if(gosmall[i]) {
  110. g[gosmall[i] + n].push_back(i);
  111. root[f(i)] = f(gosmall[i] + n);
  112. }
  113. if(gobig[i]) {
  114. g[gobig[i]].push_back(i + n);
  115. root[f(i + n)] = f(gobig[i]);
  116. }
  117. }
  118.  
  119. for(int i = 1; i <= n + n; i++) {
  120. if(!h[f(i)]) {
  121. cnt++;
  122. dfs(0, f(i));
  123. }
  124. }
  125.  
  126. scanf("%d", &m);
  127.  
  128. for(int i = 1; i <= m; i++) {
  129. int x, y;
  130. scanf("%d %d", &x, &y);
  131. qu[x].push_back(ii(y, i));
  132. }
  133.  
  134. memset(h, 0, sizeof(h));
  135.  
  136. for(int i = 1; i <= n + n; i++) {
  137. if(!h[f(i)]) {
  138. dfs2(0, f(i));
  139. }
  140. }
  141.  
  142. for(int i = 1; i <= m; i++)
  143. printf("%d\n", (bool) ans[i]);
  144.  
  145. return 0;
  146.  
  147. }
Success #stdin #stdout 0.03s 62056KB
stdin
Standard input is empty
stdout
Standard output is empty