Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define re return
  6. #define pb push_back
  7. #define po pop_back
  8. #define all(x) (x).begin(), (x).end()
  9. #define fi first
  10. #define se second
  11. #define sqrt(x) sqrt(abs(x))
  12. #define mp make_pair
  13. #define pi 3.14159265359
  14. #define fo(i, n) for(int i = 0; i < n; ++i)
  15. #define ro(i, n) for(int i = n - 1; i >= 0; --i)
  16. #define fill(x, y) memset(x, y, sizeof(x))
  17.  
  18.  
  19. typedef vector<int> vi;
  20. typedef vector<vi> vvi;
  21. typedef pair<int, int> ii;
  22. typedef vector<ii> vii;
  23. typedef vector<string> vs;
  24. typedef double D;
  25. typedef long double ld;
  26. typedef long long ll;
  27. typedef pair<ll, ll> pll;
  28. typedef vector<ll> vll;
  29.  
  30. class node{
  31. public:
  32. ll value;
  33. ll plus;
  34. node *l;
  35. node *r;
  36. node() {
  37. plus = 0;
  38. value = (ll) 1e14;
  39. l = r = nullptr;
  40. }
  41. };
  42.  
  43. #define tree node *
  44. #define left (0)
  45. #define right (300000)
  46.  
  47. node root[1000000];
  48. int now = 1;
  49.  
  50. void add(tree t, int l, int r, int pos, int val) {
  51. if (t -> value > (ll) val) t -> value = (ll)val;
  52. if (r - l == 1) re;
  53. int c = (l + r) >> 1;
  54. if (pos < c) {
  55. if (!t -> l) t -> l = &root[now++];
  56. add(t -> l, l, c, pos, val);
  57. }
  58. else {
  59. if (!t -> r) t -> r = &root[now++];
  60. add(t -> r, c, r, pos, val);
  61. }
  62. }
  63.  
  64. void update(tree t, int l, int r, int L, int R, int val) {
  65. if (l == L && r == R) {
  66. t -> plus += val;
  67. re;
  68. }
  69. int c = (l + r) >> 1;
  70. if (L < c && t -> l) update(t -> l, l, c, L, min(c, R), val);
  71. if (R >= c && t -> r) update(t -> r, c, r, max(c, L), R, val);
  72. if (t -> l && t -> r) t -> value = min(t -> l -> value + t -> l -> plus, t -> r -> value + t -> r -> plus);
  73. else {
  74. if (t -> l) t -> value = t -> l -> value + t -> l -> plus;
  75. else {
  76. if (t -> r) t -> value = t -> r -> value + t -> r -> plus;
  77. else t -> value = (ll) 1e14;
  78. }
  79. }
  80. }
  81.  
  82. ll get_min(tree t, int l, int r, int L, int R) {
  83. if (l == L && r == R) re t -> value + t -> plus;
  84. int c = (l + r) >> 1;
  85. ll cur1, cur2;
  86. cur1 = cur2 = (ll) 1e14;
  87. if (L < c && t -> l) cur1 = get_min(t -> l, l, c, L, min(c, R));
  88. if (R >= c && t -> r) cur2 = get_min(t -> r, c, r, max(c, L), R);
  89. if (cur1 < cur2) re cur1 + t -> plus;
  90. re cur2 + t -> plus;
  91. }
  92.  
  93. int main() {
  94. int n;
  95. scanf("%d", &n);
  96. fo(i, n) {
  97. int x;
  98. scanf("%d", &x);
  99. add(root, left, right, i, x);
  100. }
  101. int m;
  102. scanf("%d\n", &m);
  103. char c[150];
  104. fo(i, m) {
  105. gets(c);
  106. int q = 0;
  107. int l = 0, r = 0, p = 0;
  108. for (int i = 0; c[i] != '\n' && c[i] != '\0'; ++i) {
  109. if (!isdigit(c[i]) && c[i] != '-') continue;
  110. string str;
  111. while(isdigit(c[i]) || c[i] == '-') str += c[i++];
  112. switch (q) {
  113. case 0:
  114. l = atoi(str.c_str());
  115. break;
  116. case 1:
  117. r = atoi(str.c_str());
  118. break;
  119. case 2:
  120. p = atoi(str.c_str());
  121. break;
  122. }
  123. --i;
  124. ++q;
  125. }
  126. if (q == 2) {
  127. ll ans = (ll) 1e18;
  128. --ans;
  129. if (l > r) {
  130. ans = min (ans, get_min(root, left, right, 0, r + 1));
  131. ans = min (ans, get_min(root, left, right, l, n));
  132. }
  133. else ans = get_min(root, left, right, l, r + 1);
  134. printf ("%I64d\n", ans);
  135. //cout << '!' << ans << endl;
  136. }
  137. else {
  138. if (l > r) {
  139. update(root, left, right, 0, r + 1, p);
  140. update(root, left, right, l, n, p);
  141. }
  142. else update(root, left, right, l, r + 1, p);
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement