Advertisement
Guest User

segment tree

a guest
Jan 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int niza[1000];
  5.  
  6. class node
  7. {
  8.  
  9. public:
  10.  
  11. int val;
  12. node *leftchild;
  13. node *rightchild;
  14.  
  15. int leftbound;
  16. int rightbound;
  17. int mid;
  18. node (int lb, int rb)
  19. {
  20.  
  21. leftbound=lb;
  22. rightbound=rb;
  23.  
  24. if(lb==rb)
  25. val=niza[lb];
  26. else
  27. {
  28. mid=(lb+rb)/2;
  29. leftchild= new node(leftbound, mid);
  30. rightchild=new node( mid+1,rightbound);
  31. val= min(leftchild->val, rightchild->val);
  32. }
  33. }
  34. void pecati()
  35. {
  36.  
  37. if(leftbound==rightbound)
  38. {
  39. cout<<val<<endl;
  40. return;
  41. }
  42. else
  43. {
  44. // cout<<leftbound<<" "<<rightbound<<" "<<val<<endl;
  45. leftchild->pecati();
  46. rightchild->pecati();
  47. }
  48. }
  49.  
  50. int find_minimum(int l, int r)
  51. {
  52.  
  53. //cout<<leftbound<<" "<<rightbound<<endl;
  54. if(l<=leftbound && rightbound<=r)
  55. return val;
  56.  
  57. else if(l<=rightbound && r>=leftbound)
  58. {
  59. return min((leftchild->find_minimum(l, r)), (rightchild->find_minimum(l, r)));
  60. }
  61. else
  62. return (1<<30);
  63. }
  64.  
  65. void update(int value, int position){
  66. if(leftbound==rightbound)
  67. val=value;
  68. else{
  69.  
  70. if(position>mid)
  71. rightchild->update(position, value);
  72. else
  73. leftchild->update(position, value);
  74.  
  75. val=min(leftchild->val, rightchild->val);
  76. }
  77. }
  78. };
  79.  
  80.  
  81. int main()
  82. {
  83. int n;
  84. cin>>n;
  85.  
  86. for(int i=0; i<n; i++)
  87. {
  88. cin>>niza[i];
  89. }
  90. node *root=new node(0, n-1);
  91.  
  92.  
  93. int num_queries;
  94. cin>>num_queries;
  95. vector <pair<int, pair<int, int> > > queries;
  96. for(int i=0; i<num_queries; i++)
  97. {
  98.  
  99. int a, b, c;
  100. cin>>a>>b>>c;
  101.  
  102. queries.push_back(make_pair(a, make_pair(b, c)));
  103. if(a==0)
  104. cout<<root->find_minimum(b, c);
  105. if(a==1)
  106. root->update(b,c);
  107. }
  108. root->pecati();
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement