bhupixb

Secomd max using SEGment tree

Aug 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. #define ll long long int
  4. #define rep(i,a,n) for(int i = a; i < n; i++)
  5. #define A(v) (v.begin(), v.end())
  6. #define S(x) scanf("%I64d", &x)
  7. #define s(x) scanf("%d", &x)
  8. #define pb(x) push_back(x)
  9. #define V vector < int >
  10. const int N = 4e5+5;
  11. vector < int > ans1, ans2;
  12. bool al, ar;
  13. struct st{
  14. int x, y;
  15. }tree[N];
  16. int arr[N];
  17. void build(int l, int r, int node){
  18. if(l == r){
  19. tree[node].x = arr[l];
  20. tree[node].y = 0;
  21. return;
  22. }
  23. int mid = (l+r) >> 1, lc = node + node;
  24. build(l, mid, lc);
  25. build(mid + 1, r, lc | 1);
  26. vector < int > a;
  27. a.pb(tree[lc].x),a.pb(tree[lc].y),a.pb(tree[lc|1].x),a.pb(tree[lc|1].y);
  28. sort(a.begin(), a.end());
  29. tree[node].x = a.back(); tree[node].y = a[2];
  30. }
  31. void query(int l, int r, int ql, int qr, int node, bool a){
  32. if(qr < l or ql > r)return ;
  33. /* if(l == r and (l == ql || l == qr)){
  34. if(l == ql)al = true;
  35. else ar = true;
  36. return 0;//return tree[node].x;
  37. }
  38. // if(l == r){ans.pb(arr[l]);return 0;}
  39. // if(a and ql <= l and r <= qr)return tree[node].y;
  40. // if(!a and ql <= l and r <= qr)return tree[node].x;*/
  41. if(a and ql <= l and r <= qr){
  42. ans1.pb(tree[node].y);
  43. ans1.pb(tree[node].x);
  44. return;
  45. }
  46.  
  47. int mid = (l+r) >> 1, lc = node + node;
  48. query(l, mid, ql, qr, lc, a), query(mid + 1, r, ql, qr, lc|1, a);
  49. }
  50. void upd(int l, int r, int pos, int val, int node){
  51. if(pos < l or pos > r)return;
  52. if(l == r){
  53. tree[node].x = val;
  54. tree[node].y = 0;
  55. arr[l] = val;
  56. return ;
  57. }
  58. // if(ql <= l and )
  59.  
  60. int mid = (l+r) >> 1, lc = node + node;
  61. upd(l, mid, pos, val, lc);
  62. upd(mid + 1, r, pos, val, lc|1);
  63. vector < int > a;
  64. a.pb(tree[lc].x),a.pb(tree[lc].y),a.pb(tree[lc|1].x),a.pb(tree[lc|1].y);
  65. sort(a.begin(), a.end());
  66. tree[node].x = a.back(); tree[node].y = a[2];
  67. }
  68.  
  69. int main(){
  70. ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  71. int n, i, j, k, l, r, q, x, y;char ch;
  72. cin >> n;
  73. for(i = 1; i <= n; i++)cin >> arr[i];
  74. build(1, n, 1);
  75. //cout << query(1, n, 1, 4, 1, 0) << ' ' << query(1, n, 1, 4, 1, 1) << '\n';
  76. cin >> q;
  77.  
  78. while(q--){
  79. ans1.clear();al = 0, ar = 0;//ans2.clear();
  80. //scanf("%c %d %d", ch, &l, &r);
  81. cin >> ch >> l >> r;
  82. //cout << l << ' ' << r << '\n';
  83. // cout << ch << '\n';
  84. if(ch == 'U'){upd(1, n, l, r, 1);continue;}
  85. // cout << query(1, n, l, r, 1, 0) << ' ' << query(1, n, l, r, 1, 1) << '\n';
  86. // if(r <= n){
  87. //(query(1, n, l, r, 1, 0)) ;
  88. (query(1, n, l, r, 1,1));
  89. sort(ans1.begin(),ans1.end());
  90.  
  91. cout << ans1.back() + ans1[ans1.size()-2] << '\n';
  92. //printf("%d\n", x + y);
  93. // }
  94. }
  95. // rep(i,1,4*n)if(tree[i].x)cout << i << ' ' << tree[i].x << ' ' << tree[i].y << '\n';
  96. return 0;
  97. }
Add Comment
Please, Sign In to add comment