Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define N 8
- using namespace std;
- int A[4*N], i, j;
- void build_tree(int node, int start, int end, int a[]){
- if(start == end){
- A[node]=a[start];
- return;
- }
- else{
- int mid = (start + end) / 2;
- build_tree(2*node, start, mid, a);
- build_tree(2*node+1, mid + 1, end,a);
- A[node]=min(A[2*node],A[2*node+1]);
- }
- }
- void update(int node, int start, int end, int pos, int val){
- if(start == end){
- A[node]=val;
- }
- else{
- int mid = (start + end) / 2;
- if(pos <= mid)
- update(2*node,start,end,pos,val);
- else
- update(2*node+1,start,end,pos,val);
- A[node]=min(A[node*2],A[1+node*2]);
- }
- }
- int query(int node, int start, int end, int l, int r){
- if(l >= start || r >= end)return A[node];
- else{
- int mid = (start + end) / 2;
- int res = 1111111111;
- if(l <= mid && start <= r )
- res = min(res, query(2*node,start,mid,l,r));
- else
- res = min(res, query(2*node+1,mid+1,end,l,r));
- return res;
- }
- }
- int main(){
- int a[]={1,4,5,2,8,3,9,6};
- build_tree(1,0,7,a);
- for(i = 0; i < 32; i++)
- cout << A[i] << endl;
- cout << query(1,0,7,1,5)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement