xtu資料結構 G. Count the Colors

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   

G. Count the ColorsTime Limit: 2000msMemory Limit: 65536KB64-bit integer IO format: %lld      Java class name: Main  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

 

 解題:線段樹。。。比較奇葩的線段樹,此樹的葉子節點必須是一個長度為1的線段,而不是我通常所寫的那種葉子節點就是一個點的那種。 [0,4][0,2][2,4][0,1][1,2][2,3][3,4]是這種線段樹。 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f14 using namespace std;15 const int maxn = 8010;16 struct node {17     int lt,rt,color;18 } tree[maxn<<2];19 int col[maxn],temp;20 void build(int lt,int rt,int v) {21     int mid = (lt+rt)>>1;22     tree[v].lt = lt;23     tree[v].rt = rt;24     tree[v].color = -1;25     if(lt+1 == rt) return;26     build(lt,mid,v<<1);27     build(mid,rt,v<<1|1);28 }29 void update(int lt,int rt,int c,int v) {30     if(lt == rt || tree[v].color == c) return;31     if(tree[v].lt >= lt && tree[v].rt <= rt) {32         tree[v].color = c;33         return;34     }35     if(tree[v].color >= 0) {36         tree[v<<1].color = tree[v<<1|1].color = tree[v].color;37         tree[v].color = -2;38     }39     int mid = (tree[v].lt+tree[v].rt)>>1;40     if(rt <= mid) {41         update(lt,rt,c,v<<1);42     } else if(lt >= mid) {43         update(lt,rt,c,v<<1|1);44     } else {45         update(lt,mid,c,v<<1);46         update(mid,rt,c,v<<1|1);47     }48     tree[v].color = -2;49 }50 void query(int v) {51     if(tree[v].color == -1) {52         temp = -1;53         return;54     }55     if(tree[v].color != -2) {56         if(tree[v].color != temp) {57             temp = tree[v].color;58             col[temp]++;59         }60         return;61     }62     if(tree[v].lt+1 != tree[v].rt) {63         query(v<<1);64         query(v<<1|1);65     }66 }67 int main() {68     int n,i,j,x,y,c,mx;69     while(~scanf("%d",&n)) {70         build(0,8002,1);71         memset(col,0,sizeof(col));72         mx = 0;73         for(i = 0; i < n; i++) {74             scanf("%d%d%d",&x,&y,&c);75             update(x,y,c,1);76             if(c > mx) mx = c;77         }78         temp = -1;79         query(1);80         for(i = 0; i <= mx; i++)81             if(col[i]) printf("%d %d\n",i,col[i]);82         printf("\n");83     }84     return 0;85 }
View Code

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.