kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3659字,纯文字阅读大概需要6分钟。
内容图文
![kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)](/upload/InfoBanner/zyjiaocheng/1205/0be7597d7cf74ca3862d14ce80eee3e0.jpg)
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.
<b< dd="">
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.
<b< dd="">
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
<b< dd="">
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
因为是线段树专题,就思考线段树,由于刚用线段树,这里的建树感觉没用,但是不确定,不过Pushup果断弃用。还有就是Query的时候是需要跑所有叶节点的,所以不需要那些判断, 大体思路正确,就是没想到用last记录。
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14usingnamespace std; 15#define FO freopen("in.txt","r",stdin); 16#define rep(i,a,n) for (int i=a;i<n;i++) 17#define per(i,a,n) for (int i=n-1;i>=a;i--) 18#define pb push_back 19#define mp make_pair 20#define all(x) (x).begin(),(x).end() 21#define fi first 22#define se second 23#define SZ(x) ((int)(x).size()) 24#define debug(x) cout << "&&" << x << "&&" << endl; 25#define lowbit(x) (x&-x) 26#define mem(a,b) memset(a, b, sizeof(a)); 27 typedef vector<int> VI; 28 typedef longlong ll; 29 typedef pair<int,int> PII; 30const ll mod=1000000007; 31constint inf = 0x3f3f3f3f; 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 34//head3536constint maxn=8010; //last 上一段的颜色37int lazy[maxn<<2],n,ans[maxn],last;//lazy就是树,不需要处理什么信息 3839void Pushdown(int rt) { 40if(lazy[rt]!=-1) { 41 lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt]; 42 lazy[rt]=-1; 43 } 44} 4546void Updata(int rt,int L,int R,int l,int r,int val) { 47if(L>=l&&R<=r) { 48 lazy[rt]=val; 49return; 50 } 51 Pushdown(rt); 52int mid=(L+R)>>1; 53if(l<=mid) Updata(rt<<1,L,mid,l,r,val); 54if(r>mid) Updata(rt<<1|1,mid+1,R,l,r,val); 55} 5657void Query(int rt,int L,int R,int l,int r) {//不要太死板,学会变通58if(L==R) { 59if(lazy[rt]!=-1&&lazy[rt]!=last) {//巧妙60 ans[lazy[rt]]++; 61 } 62 last=lazy[rt]; 63return; 64 } 65 Pushdown(rt); 66int mid=(L+R)>>1;//需要遍历所有叶节点67 Query(rt<<1,L,mid,l,r); 68 Query(rt<<1|1,mid+1,R,l,r); 69} 7071int main() { 72while(~scanf("%d",&n)) { 73 mem(ans,0); 74 mem(lazy,-1); 75int x,y,val; 76while(n--) { 77 scanf("%d%d%d",&x,&y,&val); 78 Updata(1,1,maxn,x+1,y,val);//把0 变为 179 } 80 last=-1; 81 Query(1,1,maxn,1,maxn); 82 rep(i,0,maxn) { 83if(ans[i]) printf("%d %d\n",i,ans[i]); 84 } 85 printf("\n"); 86 } 87 }
原文:https://www.cnblogs.com/ACMerszl/p/9864585.html
内容总结
以上是互联网集市为您收集整理的kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)全部内容,希望文章能够帮你解决kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。