fork download
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <tuple>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. #define eprintf(...) fprintf(stderr, __VA_ARGS__)
  9.  
  10. const int N = 100500;
  11. const int K = 230;
  12.  
  13. typedef long long llong;
  14.  
  15. int D[N];
  16. int A[N];
  17.  
  18. llong PS[N];
  19. int st[N];
  20.  
  21. bool was[N];
  22. vector<int> E[N];
  23.  
  24. int n, k;
  25.  
  26. struct block {
  27. int len = 0;
  28. llong X[K] = {0};
  29. llong dA[K] = {0}; // Subject to further optimization
  30. llong glob = 0;
  31. llong max_req = -(llong)1e18;
  32. llong max_X = -(llong)1e18;
  33.  
  34. void add_global(llong d) {
  35. glob += d;
  36. }
  37.  
  38. inline llong get_max_req() const {
  39. return max_req;
  40. }
  41.  
  42. inline llong get_max_X() const {
  43. return max_X + glob;
  44. }
  45.  
  46. void recalc() {
  47. assert(glob == 0);
  48. max_req = -(llong)1e18;
  49. for (int i = 0; i < len; i++) {
  50. llong got = i ? dA[i - 1] : 0;
  51. if (got > k)
  52. break;
  53. max_req = max(max_req, X[i] + k - got);
  54. }
  55. max_X = *max_element(X, X + len);
  56. }
  57.  
  58. void flush() {
  59. for (int i = 0; i < len; i++) {
  60. X[i] += glob;
  61. dA[i] += glob;
  62. }
  63. max_X += glob;
  64. glob = 0;
  65. }
  66.  
  67. void add_suff(int x, llong d) {
  68. flush();
  69. for (int i = x; i < len; i++) {
  70. X[i] += d;
  71. dA[i] += d;
  72. }
  73. recalc();
  74. }
  75.  
  76. // ans, mx
  77. pair<int, llong> get(int l, llong prev_mx = -1e18) const {
  78. llong mx = prev_mx;
  79. if (l != -1)
  80. mx = X[l] + glob;
  81. int ans = 0;
  82. for (int i = l + 1; i < len; i++) {
  83. llong got_prev = (i ? dA[i - 1] : -(llong)1e18) + glob;
  84. llong got = dA[i] + glob;
  85. if (got_prev > k)
  86. break;
  87. if (X[i] + glob + k - got >= mx) {
  88. ans = max(ans, i - l);
  89. }
  90. mx = max(mx, X[i] + glob);
  91. }
  92. return make_pair(ans, mx);
  93. }
  94. } B[N / K + 2];
  95.  
  96. int blocks;
  97.  
  98. void add_suff(int l, llong d) {
  99. int id = l / K;
  100. B[id].add_suff(l - id * K, d);
  101. while (++id < blocks) {
  102. B[id].add_global(d);
  103. }
  104. }
  105.  
  106. int ans = 1;
  107.  
  108. void process(int l) {
  109. int ans;
  110. llong mx;
  111. int id = l / K;
  112. tie(ans, mx) = B[id].get(l - id * K);
  113. ++ans;
  114. int good_id = -1;
  115. llong good_mx = -42;
  116. int prev_good_id = -1;
  117. llong prev_good_mx = -42;
  118. while (++id < blocks) {
  119. if (B[id - 1].dA[K - 1] + B[id - 1].glob > k)
  120. break;
  121. if (B[id].get_max_req() >= mx) {
  122. prev_good_id = good_id;
  123. prev_good_mx = good_mx;
  124. good_id = id;
  125. good_mx = mx;
  126. }
  127. mx = max(mx, B[id].get_max_X());
  128. }
  129. if (good_id != -1) {
  130. if ((good_id + 1) * K - l <= ::ans)
  131. return;
  132. int ans2;
  133. tie(ans2, ignore) = B[good_id].get(-1, good_mx);
  134. if (ans2 == 0) {
  135. if (prev_good_id != -1) {
  136. if ((prev_good_id + 1) * K - l <= ::ans)
  137. return;
  138. tie(ans2, ignore) = B[prev_good_id].get(-1, prev_good_mx);
  139. assert(ans2 != -1);
  140. ans = ans2 + prev_good_id * K - l;
  141. }
  142. } else {
  143. ans = ans2 + good_id * K - l;
  144. }
  145. }
  146. //eprintf("l = %d -> ans = %d\n", l, ans);
  147. ::ans = max(::ans, ans);
  148. }
  149.  
  150. void DFS(int x, int p = -1) {
  151. was[x] = true;
  152. if (p != -1)
  153. add_suff(p - 1, PS[x] - PS[p]);
  154. process(x);
  155. for (int y : E[x]) {
  156. DFS(y, x);
  157. }
  158. if (p != -1)
  159. add_suff(p - 1, PS[p] - PS[x]);
  160. }
  161.  
  162. int main() {
  163. scanf("%d %d", &n, &k);
  164. for (int i = 0; i < n - 1; i++) {
  165. scanf("%d", &D[i]);
  166. }
  167. for (int i = 0; i < n; i++) {
  168. scanf("%d", &A[i]);
  169. }
  170. for (int i = 1; i < n; i++) {
  171. PS[i] = PS[i - 1] + A[i - 1] - D[i - 1];
  172. }
  173. int pt = 0;
  174. for (int i = n - 1; i >= 0; i--) {
  175. while (pt > 0 && PS[st[pt - 1]] >= PS[i])
  176. --pt;
  177. if (pt)
  178. E[st[pt - 1]].push_back(i);
  179. st[pt++] = i;
  180. }
  181. llong curX = 0;
  182. for (int i = 1; i < n; i++)
  183. curX = B[i / K].X[i % K] = curX + A[i] - D[i - 1];
  184. blocks = (n + K - 1) / K;
  185. for (int id = 0; id < blocks; id++) {
  186. B[id].len = min(K, n - id * K);
  187. B[id].recalc();
  188. }
  189. for (int i = 0; i < n; i++) {
  190. reverse(E[i].begin(), E[i].end());
  191. }
  192. for (int i = n - 1; i >= 0; i--) {
  193. if (!was[i])
  194. DFS(i);
  195. }
  196. printf("%d\n", ans);
  197. }
  198.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/5/tuple:35:0,
                 from prog.cpp:4:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
prog.cpp:27:15: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     int len = 0;
               ^
prog.cpp:28:20: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     llong X[K] = {0};
                    ^
prog.cpp:29:21: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     llong dA[K] = {0}; // Subject to further optimization
                     ^
prog.cpp:30:18: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     llong glob = 0;
                  ^
prog.cpp:31:29: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     llong max_req = -(llong)1e18;
                             ^
prog.cpp:32:27: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     llong max_X = -(llong)1e18;
                           ^
prog.cpp:28:20: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
     llong X[K] = {0};
                    ^
prog.cpp:29:21: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
     llong dA[K] = {0}; // Subject to further optimization
                     ^
prog.cpp: In function 'void process(int)':
prog.cpp:112:16: error: 'tie' was not declared in this scope
     tie(ans, mx) = B[id].get(l - id * K);
                ^
prog.cpp:133:19: error: 'ignore' was not declared in this scope
         tie(ans2, ignore) = B[good_id].get(-1, good_mx);
                   ^
prog.cpp: In function 'void DFS(int, int)':
prog.cpp:155:18: warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11
     for (int y : E[x]) {
                  ^
stdout
Standard output is empty