zoj 1610 Count the Colors

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610

View Code
  1 #include <cmath>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <iostream>
5 #include <cstring>
6 #include <string>
7 #include <algorithm>
8 #include <queue>
9 #include <stack>
10 #include <climits>
11 #define L(x) (x << 1)
12 #define R(x) (x << 1 | 1)
13
14 using namespace std;
15
16 const int MAX = 8010;
17 struct Tnode{ int col; int l,r;};
18 Tnode node[MAX*4];
19 int col[MAX],len[MAX];;
20 void init()
21 {
22 memset(node,0,sizeof(node));
23 memset(col,-1,sizeof(col));
24 }
25 void Build(int t,int l,int r)
26 {
27 node[t].l = l;
28 node[t].r = r;
29 node[t].col = -1;
30 if( l == r - 1 ) return ;
31 int mid = ( l + r ) >> 1;
32 Build(L(t), l, mid);
33 Build(R(t), mid, r);
34 }
35
36 void Updata(int t,int l,int r,int col)
37 {
38 if( node[t].l >= l && node[t].r <= r )
39 {
40 node[t].col = col;
41 return ;
42 }
43 if( node[t].col == col ) return ;
44 if( node[t].col >= 0 )
45 {
46 node[R(t)].col = node[t].col;
47 node[L(t)].col = node[t].col;
48 node[t].col = -1;
49 }
50 int mid = (node[t].l + node[t].r) >> 1;
51 if( l >= mid )
52 Updata(R(t),l,r,col);
53 else
54 if( r <= mid )
55 Updata(L(t),l,r,col);
56 else
57 {
58 Updata(L(t),l,mid,col);
59 Updata(R(t),mid,r,col);
60 }
61 }
62 void Compute(int t,int l,int r)
63 {
64 if( node[t].col >= 0 )
65 {
66 for(int i=l; i<r; i++)
67 col[i] = node[t].col;
68 return ;
69 }
70 if( node[t].l == node[t].r - 1 ) return ;// 如果父亲节点已经是这种颜色了,就没必要再染色了
71 int mid = (node[t].l + node[t].r) >> 1;
72 if( l >= mid )
73 Compute(R(t),l,r);
74 else
75 if( r <= mid )
76 Compute(L(t),l,r);
77 else
78 {
79 Compute(L(t),l,mid);
80 Compute(R(t),mid,r);
81 }
82 }
83
84 int main()
85 {
86 int n,x,y,color;
87
88 while( ~scanf("%d",&n) )
89 {
90 init();
91 Build(1,0,8000);
92 while( n-- )
93 {
94 scanf("%d%d%d",&x,&y,&color);
95 Updata(1,x,y,color);
96 }
97 Compute(1,0,8000);
98 memset(len,0,sizeof(len));
99 for(int i=0; i<MAX; i++)
100 if( col[i+1] != col[i] && col[i] != -1 )
101 len[col[i]]++;
102 for(int i=0; i<MAX; i++)
103 if( len[i] )
104 printf("%d %d\n",i,len[i]);
105 printf("\n");
106 }
107
108 return 0;
109 }

 

posted @ 2012-02-27 17:52  我们一直在努力  阅读(129)  评论(0编辑  收藏  举报