fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8. typedef tree<
  9. int,
  10. null_type,
  11. less<int>,
  12. rb_tree_tag,
  13. tree_order_statistics_node_update> ordered_set;
  14.  
  15. main()
  16. {
  17. freopen("river.in", "r", stdin);
  18. freopen("river.out", "w", stdout);
  19. ios::sync_with_stdio(0);
  20. cin.tie(0);
  21. int n, p;
  22. cin >> n >> p;
  23. ordered_set points;
  24. points.insert(0);
  25. int64_t ans = 0;
  26. while(n--)
  27. {
  28. int x;
  29. cin >> x;
  30. ans += x * x;
  31. points.insert(*--points.end() + x);
  32. }
  33. points.insert(2e9);
  34. cout << ans << "\n";
  35. cin >> n;
  36. while(n--)
  37. {
  38. int t, x;
  39. cin >> t >> x;
  40. auto it = points.find_by_order(x);
  41. auto it1 = it, it2 = it;
  42. int64_t v = *it;
  43. int64_t l = *--it1;
  44. int64_t ll = 0;
  45. if(l != 0)
  46. ll = *--it1;
  47. int64_t r = *++it2;
  48. ans -= (v - l) * (v - l);
  49. int m = (v + l) / 2;
  50. points.insert(m);
  51. if(t == 1)
  52. {
  53. points.erase(v);
  54. points.erase(l);
  55. if(l == 0)
  56. {
  57. points.erase(m);
  58. points.insert(l);
  59. ans -= (r - v) * (r - v);
  60. ans += r * r;
  61. }
  62. else if(r == 2e9)
  63. {
  64. points.erase(m);
  65. points.insert(v);
  66. ans -= (l - ll) * (l - ll);
  67. ans += (v - ll) * (v - ll);
  68. }
  69. else
  70. {
  71. ans -= (r - v) * (r - v);
  72. ans -= (l - ll) * (l - ll);
  73. ans += (r - m) * (r - m);
  74. ans += (m - ll) * (m - ll);
  75. }
  76. }
  77. else
  78. {
  79. ans += (m - v) * (m - v);
  80. ans += (m - l) * (m - l);
  81. }
  82. cout << ans << "\n";
  83. }
  84. }
Runtime error #stdin #stdout #stderr 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_filebuf::underflow error reading the file