线段树解决区间覆盖问题

问题:

给定一系列的区间,求其覆盖的区间的总长度。

/*************************************************************************
  * File Name: Solution.cpp
  * Description: 
  * Author: Yuji CAO
  * Mail: [email protected]
  * Created_Time: 2015-08-03 10时44分46秒
  * Last modified: 2015-08-03 10时44分46秒
 ************************************************************************/
#include
#include
#include
#include
#include
#include
using namespace std;

struct TTNode{
    int _val;
    int _start;
    int _end;
    TTNode* _left;
    TTNode* _right;
    TTNode(int s,int e,int v):_val(v),_start(s),_end(e),_left(NULL),_right(NULL){}
    TTNode(const TTNode& ttn){
        this->_val=ttn._val;
        this->_start=ttn._start;
        this->_end=ttn._end;
        this->_left=ttn._left;
        this->_right=ttn._right;
    }
    TTNode& operator=(const TTNode& ttn){
        this->_val=ttn._val;
        this->_start=ttn._start;
        this->_end=ttn._end;
        this->_left=ttn._left;
        this->_right=ttn._right;
        return *this;
    }
    static void visit(TTNode* root){
        if(root==NULL){return;}
        cout<<"["<_start<<","<_end<<";"<_val<<"]"<_left);
        visit(root->_right);
    }
};

class TreadTree{
public:
    void build(TTNode* &cur,int l,int r){
        if(r==l+1){
            cur=new TTNode(l,r,0);
            return;
        }else{
            cur=new TTNode(l,r,0);
            build(cur->_left,l,(l+r)/2);
            build(cur->_right,(l+r)/2,r);
        }
    }
    void insert(TTNode* &root,int l,int r){
        if(l==r){
            return ;
        }
        if(root->_val==0){
            if(root->_start>=l&&root->_end<=r){
                root->_val=1;
                return ;
            }else{
                int mid=(root->_start+root->_end)/2;
                if(lint e=min(mid,r);
                    insert(root->_left,l,e);
                }
                if(r>=mid){
                    int s=max(mid,l);
                    insert(root->_right,s,r);
                }
            }
        }
    }
    int query(TTNode* root){
        if(root==NULL){
            return 0;
        }
        if(root->_val==1){
            int ret=root->_end-root->_start;
            return ret;
        }else{
            int ret=0;
            ret+=query(root->_left);
            ret+=query(root->_right);
            return ret;
        }
    }
    void query(vector& result,TTNode* root,int start,int end){
        if(root->_start>start||root->_endreturn;
        }
        if(root->_start==start&&root->_end==end){
            result.push_back(*root);
            return ;
        }
        int mid=(root->_start+root->_end)/2;
        if(start<=mid){
            int e=min(mid,end);
            query(result,root->_left,start,e);
        }
        if(end>mid){
            int e=min(end,root->_end);
            query(result,root->_right,mid,e);
        }
    }
};
struct Segment{
    int _start;
    int _end;
    Segment(int s,int e):_start(s),_end(e){}
};
/*
 *get result
 */
int getResult(vector& dat){
    int b=numeric_limits<int>::max();
    int e=numeric_limits<int>::min();
    for(auto ele:dat){
        if(ele._startif(ele._end>e){
            e=ele._end;
        }
    }

    TreadTree tt;
    TTNode* root=NULL;
    tt.build(root,b,e);
    for(auto ele:dat){
        tt.insert(root,ele._start,ele._end);
    }
    return tt.query(root);
}

int main(){
        int m;
        cin>>m;
        vector dat;
        for(int i=0;i0,0);
            cin>>sm._start>>sm._end;
            dat.push_back(sm);
        }
        cout<

你可能感兴趣的:(算法)