ZOJ1610_Count the Colors(線段樹/成段更新),線段樹成段更新

來源:互聯網
上載者:User

ZOJ1610_Count the Colors(線段樹/成段更新),線段樹成段更新

解題報告

題意:

一根長度8000的線段上染色,求染完之後,每個顏色線上段上有多少個間斷的區間。

思路:

區間問題用線段樹,成段的更新區間,最後把所有的區間下壓到葉子結點,統計葉子結點的顏色。

#include <iostream>#include <cstring>#include <cstdio>using namespace std;int lz[32000],_hash[10000],color[10000],cnt;void push_down(int rt){    if(lz[rt])    {        lz[rt*2]=lz[rt*2+1]=lz[rt];        lz[rt]=0;    }}void update(int rt,int l,int r,int ql,int qr,int v){    if(ql>r||qr<l)return ;    if(ql<=l&&r<=qr)    {        lz[rt]=v;        return ;    }    push_down(rt);    int mid=(l+r)/2;    update(rt*2,l,mid,ql,qr,v);    update(rt*2+1,mid+1,r,ql,qr,v);}void bin(int rt,int l,int r){    if(l==r)    {        color[cnt++]=lz[rt];        return ;    }    push_down(rt);    bin(rt*2,l,(l+r)/2);    bin(rt*2+1,(l+r)/2+1,r);}int main(){    int n,m,i,j,ql,qr,a;    while(~scanf("%d",&n))    {        cnt=0;        memset(lz,0,sizeof(lz));        memset(_hash,0,sizeof(_hash));        m=8000;        int cmax=-1;        for(i=0; i<n; i++)        {            scanf("%d%d%d",&ql,&qr,&a);            update(1,1,m,ql+1,qr,a+1);        }        bin(1,1,m);        for(i=0; i<cnt;)        {            j=i+1;            _hash[color[i]]++;            while(color[j]==color[i]&&j<cnt)                j++;            i=j;        }        for(i=1; i<=m+1; i++)        {            if(_hash[i])                printf("%d %d\n",i-1,_hash[i]);        }        printf("\n");    }    return 0;}
附手動隨機資料

input:102 4 6 4 6 21 9 34 6 21 20 32 4 3 6 7 13 7 94 6 92 6 41043 54 8000323 4342 123234 2332 3212 6 2354 546 12843 8888 80003000 8000 023 4329 9923 2323 82390 3293 1101 34 8000 43 343 99341 3414 80007999 8000 8000344 345 1434 3455 034 45 800043 56 4556 64 0898 4599 8000output:3 24 19 10 11 18 19 323 10 21 145 199 18000 5

Count the ColorsTime Limit: 2 Seconds      Memory Limit: 65536 KBPainting 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




zoj 1610 線段樹 題目:Count the Colors 為何總是 segment fault ?什情況下會出現segment fault?

比方說數組開小了,然後數組下標是負數,或者遞迴死在裡面爆棧了,被0除了之類的。。
 
ACM 線段樹塗色問題

我改了一些,不過沒有改對,你參考一下My Code吧
#include<cstdio>
#include<cstring>
#define MAXN 8100//區間最大[0,8000]
int answer[MAXN*4];//用於儲存我們需要的“答案”即answer[i]代表the count of segments of color i;
struct node
{
int left,right;//此結點所代表的區間
int col;//此區間上覆蓋的顏色
}tree[4*MAXN];//用tree數組類比線段樹,注意結點個數
void init()
{
memset(answer,0,sizeof(answer));
}
void build(int step,int s,int e)//用遞迴建樹
{
tree[step].col=-1;//初始化顏色屬性
tree[step].left=s;
tree[step].right=e;
if(s==e-1) return;//構造完畢
int mid=(s+e)/2;
build(step*2,s,mid);
build(step*2+1,mid,e);
}
void insert(int s,int e,int c,int step)//step 是樹的數組的下標,將[s,e]塗成顏色c
{
/*
1
0 8000 1
上面這個資料不正常
*/
if(tree[step].left>=s&&tree[step].right<=e)//這裡改了一下,
{
tree[step].col=c;
return;
}
int mid=(tree[step].left+tree[step].right)/2;
if(e<=mid)
insert(s,e,c,step*2);
else if(s>=mid)
insert(s,e,c,step*2+1);
else
{
insert(s,mid,c,step*2);
insert(s,mid,c,step*2+1);
}
}
void count(int step)
{
if(tree[step].col!=-1)
answer[tree[step].col]++;
if(tree[step].left==tree[step].right-1)
return;
count(step*2);
count(step*2+1);
}
int main()
{
int t,a,b,c;
while(scanf("%d",&t)!=EOF)
{
init();
build(1,0,MAXN);
while(t--)
{
scanf("%d %d %d",&a,&b,&c);
insert(a,b+1,c,1);//你是左閉右開的,這裡要加1

}
count(1);......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.