[线段树练习5] 线段的条数 - 统计点重复标记数
2017-02-05 23:39
681 查看
题目描述
X轴上从下向上依次叠放一定长度某种颜色的线段。问在某个单位区间上一共叠放了多少条线段?输入格式
第1行:2个整数XC,N。XC表示X轴的颜色,1<=N<=100000,表示线段的条数。接下来N行,每行3个整数L,R,C,-100000 <=L<=R<= 100000,表示一线段的左、右端点;1<=C<=100000,表示线段的颜色。
最后1行:1个整数P,表示单位区间的起点。-100000 <=P<100000
输出格式
第1行:1个整数M,表示[P, P+1]区间叠放了多少条线段。样例数据
样例输入
1 42 6 2
1 5 3
3 4 2
7 8 4
3
样例输出
3题目分析
相对于前几道题,这道题就水了。区间修改 单点查询
直接统计就可以了
注意偏移量
源代码
#include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=600000; struct Tree { //修改区间 查询点 int left,right,sum; }; struct Segment_Tree { //统计标记次数(颜色有用么) Tree tree[maxn*4]; int sum; void build(int index,int Left,int Right) { tree[index].left=Left; tree[index].right=Right; tree[index].sum=0; if(Left==Right)return; int mid=(Left+Right)/2; build(2*index,Left,mid); build(2*index+1,mid+1,Right); } void modify(int index,int Left,int Right) { if(Right<tree[index].left||Left>tree[index].right)return; //不相交 if(Left<=tree[index].left&&Right>=tree[index].right) { //完全包含 tree[index].sum++; return; } modify(index*2,Left,Right); modify(index*2+1,Left,Right); } void init() { sum=0; } void Query(int index,int target) { if(target<tree[index].left||target>tree[index].right)return; //不相交 sum+=tree[index].sum; Query(index*2,target); Query(index*2+1,target); } }; Segment_Tree st; int n,Delta=100001,color; int main() { color=Get_Int(); st.build(1,1,200001); n=Get_Int(); for(int i=1; i<=n; i++) { int l=Get_Int()+Delta,r=Get_Int()+Delta-1,color=Get_Int(); st.modify(1,l,r); } st.init(); st.Query(1,Get_Int()+Delta); printf("%d\n",st.sum); return 0; }
相关文章推荐
- [线段树练习4] 线段颜色条数 - 统计标记总数
- [线段树练习1] 线段统计 - 线段树懒标记
- [线段树练习2] 影子的宽度 - 统计标记个数
- [线段树练习3] 盒子的个数 - 统计标记种类
- Count the Colors (ZOJ_1610) 线段树 + 线段统计
- 操作数据库之查、增、删、改、统计、排序、混合查询、指定条数、不重复记录
- C#练习——统计整型数组中不重复数字的个数、将普通日期格式转换成汉字日期格式、在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数
- (Relax 线段树1.3)POJ 3667 Hotel(互不相交的线段的更新与统计)
- 线段树学习之:用线段数统计矩形的周长hdu(1828)
- 【解题报告】 HDU 1698 Just a Hook 线段树 (线段替换) 插线问线 + 延时标记
- 【BZOJ4515】游戏,树链剖分+永久化标记线段树维护线段信息(李超线段树)
- 4927 线段树练习5【多重标记】
- C#练习——去掉字符串两端的空格、统计字符出现的次数及出现的索引位置、随机生成10个1-100之间的不重复的偶数
- codevs 1082 线段树练习 3 区间更新+延迟标记
- 统计list中重复数据的条数
- codevs1080 线段树练习
- codevs 1080 线段树练习 CDQ分治
- codevs 4919 线段树练习4
- Python 练习 06 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释
- 数据库小练习---删除表内的重复部分、添加字段、删除字段、修改表名