线段树
来源:互联网 发布:广东话seo是什么意思 编辑:程序博客网 时间:2024/06/11 03:31
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int tree[8100<<2],col[8010];int ans[8005],n;void pushdown(int rt){ if(tree[rt]>=0) { tree[rt<<1]=tree[rt<<1|1]=tree[rt]; tree[rt]=-1; }}void update(int L,int R,int c,int l,int r,int rt){ if(L<=l&&R>=r) { tree[rt]=c; return ; } int m=(l+r)>>1; pushdown(rt); if(L<=m) update(L,R,c,lson); if(R>m) update(L,R,c,rson);}void query(int l,int r,int rt){ if(l==r) { col[l]=tree[rt]; return; } pushdown(rt); int m=(l+r)>>1; query(lson); query(rson);}int main(){ while(~scanf("%d",&n)) { int x,y,c; memset(tree,-1,sizeof(tree)); memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) { scanf("%d%d%d",&x,&y,&c); update(x,y-1,c,0,8000,1); } memset(col,-1,sizeof(col)); query(0,8000,1); for(int i=0;i<=8000;i++) { if(col[i]==-1) continue; if(col[i]!=col[i+1]) ans[col[i]]++; } for(int i=0;i<=8000;i++) { if(ans[i]) printf("%d %d\n",i,ans[i]); } printf("\n"); } return 0;}
线段树#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>using namespace std;struct lt{ int l,r; int c,lazy;};lt rt[10010<<2];void build(int i,int l,int r){ rt[i].l=l; rt[i].r=r; rt[i].c=-1; rt[i].lazy=-1; if(l==r) { return; } int mid=(l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r);}void push_down(int i){ if(~rt[i].lazy) { rt[i<<1].lazy=rt[i].lazy; rt[i<<1|1].lazy=rt[i].lazy; rt[i<<1].c=rt[i].lazy; rt[i<<1|1].c=rt[i].lazy; rt[i].lazy=-1; }}void updata(int i,int l,int r,int c){ if(rt[i].l>r||rt[i].r<l) { return; } if(rt[i].l>=l&&rt[i].r<=r) { rt[i].c=c; rt[i].lazy=c; return; } push_down(i); updata(i<<1,l,r,c); updata(i<<1|1,l,r,c);}int nc;int ans[8080];void query(int i){ if(rt[i].l==rt[i].r) { if(rt[i].c!=nc) { ++ans[nc]; nc=rt[i].c; } return; } push_down(i); query(i<<1); query(i<<1|1);}int main(){ int n; while(~scanf("%d",&n)) { memset(ans,0,sizeof(ans)); build(1,1,10000); for(int i=0;i<n;i++) { int x,y,c; scanf("%d%d%d",&x,&y,&c); updata(1,x+1,y,c); } nc=8010; query(1); for(int i=0;i<=8000;i++) { if(ans[i]) { printf("%d %d\n",i,ans[i]); } } printf("\n"); } return 0;}
0 0
- 线段树?线段树!
- 线段树?线段树!
- 线段_线段树
- 线段_线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- How to Get Even with Your Annoying Neighbor by Bumping Them Off Their WiFi Network —Undetected
- 强化学习系列<2>、Q-Learning
- <设计模式>(1)策略模式
- mysql Key_buffer_size参数的设置
- 单例模式
- 线段树
- iOS 制作动态库 报错 image not found
- Linphone-移植(二)
- GEM 帧格式
- [LeetCode.547]Friend Circles
- bootstrap_blue_print_volume ii 读书笔记
- 工作问题总结,bms,第一天
- HttpClient 之get请求
- OkHttp3源码分析(三)