zoj1610-Count the Colors【线段树】
生活随笔
收集整理的這篇文章主要介紹了
zoj1610-Count the Colors【线段树】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
題目:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610
題意
有一條長m的線,有n條長度和顏色不同的線段,每個顏色可以看到的段數。
解題思路
標記顏色-2表示有多種顏色,然后用color表示上次的顏色以去重。
代碼
#include<cstdio> #include<cstring> using namespace std; struct xjq{int l,r,cover; }tree[30001]; int flag[30001]; int n,ll,rr,w,s,cl,color; void build(int x,int a,int b)//建樹 {tree[x].l=a;tree[x].r=b;tree[x].cover=-1;if (b-a==1) return;else{int m=(a+b)/2;build(x*2,a,m);build(x*2+1,m,b);} } void inster(int x,int a,int b,int c)//插入 {if (tree[x].cover==c) return;if (tree[x].l==a && tree[x].r==b){tree[x].cover=c;return;}if (tree[x].cover>=-1){tree[x*2].cover=tree[x].cover;tree[x*2+1].cover=tree[x].cover;tree[x].cover=-2;}int m=tree[x*2].r;if (b<=m) inster(x*2,a,b,c);else if (a>=m) inster(x*2+1,a,b,c);else{inster(x*2,a,m,c);inster(x*2+1,m,b,c);}return; } void find(int x)//查詢 {if (tree[x].cover>=-1){if (tree[x].cover!=-1 && tree[x].cover!=color)flag[tree[x].cover]++;//統計color=tree[x].cover;//標記return;}if (tree[x].r-tree[x].l==1) return;else{find(x*2);find(x*2+1);} } int main() {while (scanf("%d",&n)!=EOF){memset(tree,0,sizeof(tree));memset(flag,0,sizeof(flag));for (int i=1;i<=8000;i++) tree[i].cover=-1;build(1,0,8000);for (int i=1;i<=n;i++){scanf("%d%d%d",&ll,&rr,&cl);inster(1,ll,rr,cl);}s=0;color=-3;find(1);for (int i=0;i<=30000;i++)if (flag[i])printf("%d %d\n",i,flag[i]);//輸出printf("\n");//printf("%d\n",s);} }總結
以上是生活随笔為你收集整理的zoj1610-Count the Colors【线段树】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 植树节是几月几号
- 下一篇: P3368-Frequent value