线段树解决区间覆盖问题
2015-08-17 21:27
302 查看
问题:
给定一系列的区间,求其覆盖的区间的总长度。/************************************************************************* * File Name: Solution.cpp * Description: * Author: Yuji CAO * Mail: caoyuji@sogou-inc.com * Created_Time: 2015-08-03 10时44分46秒 * Last modified: 2015-08-03 10时44分46秒 ************************************************************************/ #include<iostream> #include<vector> #include<string> #include<map> #include<set> #include<limits> 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<<"["<<root->_start<<","<<root->_end<<";"<<root->_val<<"]"<<endl; visit(root->_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(l<mid){ int 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<TTNode>& result,TTNode* root,int start,int end){ if(root->_start>start||root->_end<end){ return; } 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<Segment>& dat){ int b=numeric_limits<int>::max(); int e=numeric_limits<int>::min(); for(auto ele:dat){ if(ele._start<b){ b=ele._start; } if(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<Segment> dat; for(int i=0;i<m;++i){ Segment sm(0,0); cin>>sm._start>>sm._end; dat.push_back(sm); } cout<<getResult(dat)<<endl; }
相关文章推荐
- d3d 程序简单demo 解析
- 经典C语言程序设计100例
- uva 11928 - The Busy Dog(几何)
- C语言-6
- poj 2488 A Knight's Journey
- hdu 5305 Friends 2015 Multi-University Training Contest 2
- UVALive 5789 双向链表解法
- 网络爬虫源码
- vijos- P1383盗窃-黑珍珠 (python + 代码优化)
- poj-2503-Babelfish-字典树orMAP or 字符串哈希
- TOYS(计算几何--点与线的关系(叉积+二分(二分之前必须排序!!!)))
- 编写高质量代码改善C#程序的157个建议——建议63:避免“吃掉”异常
- uva 10585 - Center of symmetry(水)
- 数据结构:可合并堆——左偏树
- 写报告心法
- 如何在github上提交项目
- Finding Nemo (poj 2049 超级蛋疼的bfs)
- grep 命令突然输出太多垃圾信息的解决办法
- Android Api Demos登顶之路(三十四)Device Admin
- Android的发展历史