a53

Drumetie

a53
Jun 6th, 2020 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long i64;
  4. const i64 inf(1e18);
  5. class Arbore {
  6. public:
  7. Arbore() {}
  8. Arbore(const int& _n) : n(_n) {
  9. arb = vector<i64>(2 * n, inf);
  10. }
  11. inline void Update(const int& pos, const i64& val) {
  12. int p(pos + n - 1);
  13. arb[p] = val;
  14. while (p > 0) {
  15. arb[p >> 1] = min(arb[p], arb[p ^ 1]);
  16. p >>= 1;
  17. }
  18. }
  19. inline i64 Query(const int& l, const int& r) const {
  20. i64 res(1e18);
  21. int st(l + n - 1), dr(r + n);
  22. while (st < dr) {
  23. if (st & 1)
  24. res = min(res, arb[st++]);
  25. if (dr & 1)
  26. res = min(res, arb[--dr]);
  27. st >>= 1, dr >>= 1;
  28. }
  29. return res;
  30. }
  31. private:
  32. int n;
  33. vector<i64> arb;
  34. };
  35. namespace FastRead {
  36. const int Dim(1e6);
  37. char buff[Dim];
  38. int pos, len;
  39. inline char nc() {
  40. if (pos == len) {
  41. pos = 0;
  42. len = fread(buff, 1, Dim, stdin);
  43. if (!len) return EOF;
  44. }
  45. return buff[pos++];
  46. }
  47. template<class T> inline void Read(T& x) {
  48. char ch;
  49. int sgn(1);
  50. while (!isdigit(ch = nc()))
  51. if (ch == '-')
  52. sgn = -1;
  53. x = ch - '0';
  54. while (isdigit(ch = nc()))
  55. x = x * 10 + (ch - '0');
  56. x *= sgn;
  57. }
  58. }
  59. using namespace FastRead;
  60. int n, x, k, a;
  61. i64 curr;
  62. int main() {
  63. Read(n);
  64. Arbore arb(n);
  65. vector<vector<int>> tomark(n + 1);
  66. for (int i = 1; i <= n; ++i) {
  67. for (const int& x : tomark[i])
  68. arb.Update(x, inf);
  69. Read(x), Read(k);
  70. curr = x;
  71. if (i > 1)
  72. curr += arb.Query(max(1, i - k), i - 1);
  73. arb.Update(i, curr);
  74. if (i + k < n)
  75. tomark[i + k + 1].emplace_back(i);
  76. }
  77. cout << curr;
  78. return 0;
  79. }
Add Comment
Please, Sign In to add comment