Advertisement
artcstr

Untitled

Nov 12th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define inf 0x3f3f
  5.  
  6.  
  7. struct Node {
  8. int maxPrefixSum;
  9. int maxSuffixSum;
  10. int totalSum;
  11. int maxSubarraySum;
  12.  
  13. Node()
  14. {
  15. maxPrefixSum = maxSuffixSum = maxSubarraySum = -inf;
  16. totalSum = -inf;
  17. }
  18. };
  19.  
  20. Node merge(Node leftChild, Node rightChild)
  21. {
  22. Node parentNode;
  23. parentNode.maxPrefixSum = max(leftChild.maxPrefixSum,
  24. leftChild.totalSum +
  25. rightChild.maxPrefixSum);
  26.  
  27. parentNode.maxSuffixSum = max(rightChild.maxSuffixSum,
  28. rightChild.totalSum +
  29. leftChild.maxSuffixSum);
  30.  
  31. parentNode.totalSum = leftChild.totalSum +
  32. rightChild.totalSum;
  33.  
  34. parentNode.maxSubarraySum = max({leftChild.maxSubarraySum,
  35. rightChild.maxSubarraySum,
  36. leftChild.maxSuffixSum +
  37. rightChild.maxPrefixSum});
  38.  
  39. return parentNode;
  40. }
  41. void constructTreeUtil(Node* tree, int arr[], int start,
  42. int end, int index)
  43. {
  44.  
  45. if (start == end) {
  46.  
  47. tree[index].totalSum = arr[start];
  48. tree[index].maxSuffixSum = arr[start];
  49. tree[index].maxPrefixSum = arr[start];
  50. tree[index].maxSubarraySum = arr[start];
  51. return;
  52. }
  53.  
  54. int mid = (start + end) / 2;
  55. constructTreeUtil(tree, arr, start, mid, 2 * index);
  56. constructTreeUtil(tree, arr, mid + 1, end, 2 * index + 1);
  57.  
  58. tree[index] = merge(tree[2 * index], tree[2 * index + 1]);
  59. }
  60.  
  61.  
  62. Node* constructTree(int arr[], int n)
  63. {
  64.  
  65. int x = (int)(ceil(log2(n)));
  66.  
  67. int max_size = 2 * (int)pow(2, x) - 1;
  68. Node* tree = new Node[max_size];
  69.  
  70.  
  71. constructTreeUtil(tree, arr, 0, n - 1, 1);
  72.  
  73.  
  74. return tree;
  75. }
  76.  
  77.  
  78. Node queryUtil(Node* tree, int ss, int se, int qs,int qe, int index)
  79. {
  80.  
  81. if (ss > qe || se < qs) {
  82. Node nullNode;
  83. return nullNode;
  84. }
  85.  
  86.  
  87. if (ss >= qs && se <= qe) {
  88. return tree[index];
  89. }
  90.  
  91. int mid = (ss + se) / 2;
  92. Node left = queryUtil(tree, ss, mid, qs, qe,
  93. 2 * index);
  94. Node right = queryUtil(tree, mid + 1, se, qs,
  95. qe, 2 * index + 1);
  96.  
  97.  
  98. Node res = merge(left, right);
  99. return res;
  100. }
  101.  
  102.  
  103. int query(Node* tree, int start, int end, int n)
  104. {
  105. Node res = queryUtil(tree, 0, n - 1, start, end, 1);
  106. return res.maxSubarraySum;
  107. }
  108.  
  109.  
  110. Node* add(Node* tree,int k, int x,int n) {
  111. k += n;
  112. tree[k].totalSum = x;
  113. tree[k].maxSuffixSum = x;
  114. tree[k].maxPrefixSum = x;
  115. tree[k].maxSubarraySum = x;
  116. for (k /= 2; k >= 1; k /= 2) {
  117. tree[k] = merge(tree[2*k],tree[2*k+1]);
  118. }
  119.  
  120. return tree;
  121. }
  122.  
  123.  
  124. int main()
  125. {
  126. int n;
  127. cin>>n;
  128. int arr[n];
  129. for(int i=0;i<n;i++){
  130. int y;
  131. cin>>y;
  132. arr[i]=y;
  133. }
  134.  
  135.  
  136. Node* Tree = constructTree(arr, n);
  137. int start, end, maxSubarraySum;
  138.  
  139.  
  140.  
  141. int m;
  142. cin>>m;
  143.  
  144. for(int i=0;i<m;i++){
  145. int c,a,b;
  146. cin>>c>>a>>b;
  147. if(c==1){
  148. start=a;
  149. end=b;
  150. cout<<query(Tree,start-1,end-1,n)<<endl;
  151. }else{
  152. Tree=add(Tree,a-1,b,n);
  153. }
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement