Advertisement
Guest User

Untitled

a guest
Nov 14th, 2012
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #define N 8
  3. using namespace std;
  4. int A[4*N], i, j;
  5. void build_tree(int node, int start, int end, int a[]){
  6. if(start == end){
  7. A[node]=a[start];
  8. return;
  9. }
  10. else{
  11. int mid = (start + end) / 2;
  12. build_tree(2*node, start, mid, a);
  13. build_tree(2*node+1, mid + 1, end,a);
  14. A[node]=min(A[2*node],A[2*node+1]);
  15. }
  16. }
  17.  
  18. void update(int node, int start, int end, int pos, int val){
  19. if(start == end){
  20. A[node]=val;
  21. }
  22. else{
  23. int mid = (start + end) / 2;
  24. if(pos <= mid)
  25. update(2*node,start,end,pos,val);
  26. else
  27. update(2*node+1,start,end,pos,val);
  28. A[node]=min(A[node*2],A[1+node*2]);
  29. }
  30. }
  31.  
  32. int query(int node, int start, int end, int l, int r){
  33. if(l >= start || r >= end)return A[node];
  34. else{
  35. int mid = (start + end) / 2;
  36. int res = 1111111111;
  37. if(l <= mid && start <= r )
  38. res = min(res, query(2*node,start,mid,l,r));
  39. else
  40. res = min(res, query(2*node+1,mid+1,end,l,r));
  41. return res;
  42. }
  43. }
  44. int main(){
  45. int a[]={1,4,5,2,8,3,9,6};
  46. build_tree(1,0,7,a);
  47. for(i = 0; i < 32; i++)
  48. cout << A[i] << endl;
  49. cout << query(1,0,7,1,5)<<endl;
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement