线段树模板【模板--线段树】

目录

单点更新,区间最值(小)

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define TREE_SIZE (1<<(20))
//#define local

class IntervalTree
{
    private:    
        // Top 重要
        int Top[TREE_SIZE];
        int size;
        int _Query(int a,int b,int l,int r,int Ind)
        {
            if(a<=l&& b>=r)return  Top[Ind];
            int mid=(l+r)>>1,ret=INF;
            if(a<=mid) ret=min(ret,_Query(a,b,l,mid,Ind<<1));
            if(b>mid) ret=min(ret,_Query(a,b,mid+1,r,(Ind<<1)+1));
            return ret;         
        }
        void _Modify(int a,int l,int r,int Ind,int d)
        {
            if(l==r && l==a)
            {
                Top[Ind]=d;             
                return ;
            }
            int mid=(l+r)>>1;
            if(a<=mid) _Modify(a,l,mid,Ind<<1,d);
            else _Modify(a,mid+1,r,(Ind<<1)+1,d);
            Top[Ind]=min(Top[Ind<<1],Top[(Ind<<1)+1]);
        }
        public:
            IntervalTree()
            {
                //memset(Cover,INF,sizeof(Cover));
                memset(Top,INF,sizeof(Top));
                size=(TREE_SIZE>>2)-1;
            }
            IntervalTree(int size):size(size)
            {
                //memset(Cover,INF,sizeof(Cover));
                memset(Top,INF,sizeof(Top));
            }
            int Query(int a,int b)
            {
                return _Query(a,b,1,size,1);
            }
            void Modify(int a,int d)
            {
                return _Modify(a,1,size,1,d);
            }
}it;

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    #endif
    //IntervalTree (*it)=new IntervalTree(7);
    (it).Modify(1,2);   
    (it).Modify(2,8);
    (it).Modify(3,4);
    (it).Modify(4,1);
    (it).Modify(5,6);
    (it).Modify(6,7);
    (it).Modify(7,3);
    
    cout<<(it).Query(5,6)<<endl;    
    cout<<(it).Query(3,4)<<endl;
    cout<<(it).Query(1,7)<<endl;    
    cout<<(it).Query(1,3)<<endl;
    
    fclose(stdin);
    return 0;
}

单点更新,区间最大值

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define TREE_SIZE (1<<(20))
//#define local

class IntervalTree
{
    private:    
        // Top 重要
        int Top[TREE_SIZE];
        int size;
        int _Query(int a,int b,int l,int r,int Ind)
        {
            if(a<=l&& b>=r)return  Top[Ind];
            int mid=(l+r)>>1,ret=0;
            if(a<=mid) ret=max(ret,_Query(a,b,l,mid,Ind<<1));
            if(b>mid) ret=max(ret,_Query(a,b,mid+1,r,(Ind<<1)+1));
            return ret;         
        }
        void _Modify(int a,int l,int r,int Ind,int d)
        {
            if(l==r && l==a)
            {
                Top[Ind]=d;             
                return ;
            }
            int mid=(l+r)>>1;
            if(a<=mid) _Modify(a,l,mid,Ind<<1,d);
            else _Modify(a,mid+1,r,(Ind<<1)+1,d);
            Top[Ind]=max(Top[Ind<<1],Top[(Ind<<1)+1]);
        }
        public:
            IntervalTree()
            {
                //memset(Cover,0,sizeof(Cover));
                memset(Top,0,sizeof(Top));
                size=(TREE_SIZE>>2)-1;
            }
            IntervalTree(int size):size(size)
            {
                //memset(Cover,0,sizeof(Cover));
                memset(Top,0,sizeof(Top));
            }
            int Query(int a,int b)
            {
                return _Query(a,b,1,size,1);
            }
            void Modify(int a,int d)
            {
                return _Modify(a,1,size,1,d);
            }
}it;

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    #endif
    //IntervalTree (*it)=new IntervalTree(7);
    (it).Modify(1,2);   
    (it).Modify(2,8);
    (it).Modify(3,4);
    (it).Modify(4,1);
    (it).Modify(5,6);
    (it).Modify(6,7);
    (it).Modify(7,3);
    
    cout<<(it).Query(5,6)<<endl;    
    cout<<(it).Query(3,4)<<endl;
    cout<<(it).Query(1,7)<<endl;    
    cout<<(it).Query(1,3)<<endl;
    
    fclose(stdin);
    return 0;
}

单点更新,区间求和

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define TREE_SIZE (1<<(20))
//#define local

class IntervalTree
{
    private:    
        // Top 重要
        int Top[TREE_SIZE];
        int size;
        int _Query(int a,int b,int l,int r,int Ind)
        {
            if(a<=l&& b>=r)return  Top[Ind];
            int mid=(l+r)>>1,ret=0;
            if(a<=mid) ret+=_Query(a,b,l,mid,Ind<<1);
            if(b>mid) ret+=_Query(a,b,mid+1,r,(Ind<<1)+1);
            return ret;         
        }
        void _Modify(int a,int l,int r,int Ind,int d)
        {
            if(l==r && l==a)
            {
                Top[Ind]=d;             
                return ;
            }
            int mid=(l+r)>>1;
            if(a<=mid) _Modify(a,l,mid,Ind<<1,d);
            else _Modify(a,mid+1,r,(Ind<<1)+1,d);
            Top[Ind]=Top[Ind<<1]+Top[(Ind<<1)+1];
        }
        public:
            IntervalTree()
            {
                //memset(Cover,0,sizeof(Cover));
                memset(Top,0,sizeof(Top));
                size=(TREE_SIZE>>2)-1;
            }
            IntervalTree(int size):size(size)
            {
                //memset(Cover,0,sizeof(Cover));
                memset(Top,0,sizeof(Top));
            }
            int Query(int a,int b)
            {
                return _Query(a,b,1,size,1);
            }
            void Modify(int a,int d)
            {
                return _Modify(a,1,size,1,d);
            }
}it;

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    #endif
    //IntervalTree (*it)=new IntervalTree(7);
    (it).Modify(1,2);   
    (it).Modify(2,8);
    (it).Modify(3,4);
    (it).Modify(4,1);
    (it).Modify(5,6);
    (it).Modify(6,7);
    (it).Modify(7,3);
    
    cout<<(it).Query(5,6)<<endl;    
    cout<<(it).Query(3,4)<<endl;
    cout<<(it).Query(1,7)<<endl;    
    cout<<(it).Query(1,3)<<endl;
    
    fclose(stdin);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shengwang/p/9848538.html