HDU 1199 Color the Ball

Color the Ball

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1199
64-bit integer IO format: %I64d      Java class name: Main
There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence.
 

Input

First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b'.

There are multiple cases, process to the end of file.
 

Output

Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god".
 

Sample Input

3
1 4 w
8 11 w
3 5 b

Sample Output

8 11

Source

 
解题:线段树。。。区间涂色。。最后问最长的连续白色区间起止位置
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100001;
 4 int tree[maxn<<2],d[maxn<<2],x[maxn],y[maxn],tot;
 5 bool color[maxn],wb[maxn];
 6 inline void pushdown(int v) {
 7     tree[v<<1] = tree[v<<1|1] = tree[v];
 8     tree[v] = -1;
 9 }
10 inline void pushup(int v) {
11     if(tree[v<<1] == tree[v<<1|1])
12         tree[v] = tree[v<<1];
13     else tree[v] = -1;
14 }
15 void update(int L,int R,int lt,int rt,int val,int v) {
16     if(lt <= L && rt >= R) {
17         tree[v] = val;
18         return;
19     }
20     int mid = (L + R)>>1;
21     if(tree[v] >= 0) pushdown(v);
22     if(lt < mid) update(L,mid,lt,rt,val,v<<1);
23     if(rt > mid) update(mid,R,lt,rt,val,v<<1|1);
24     pushup(v);
25 }
26 void query(int L,int R,int v) {
27     if(tree[v] >= 0) {
28         for(int i = L; tree[v]&&i < R; ++i)
29             color[i] = true;
30         return;
31     }
32     //if(R >= L) return;
33     if(tree[v] >= 0) pushdown(v);
34     int mid = (L + R)>>1;
35     query(L,mid,v<<1);
36     query(mid,R,v<<1|1);
37 }
38 int main() {
39     int n;
40     char op[3];
41     while(~scanf("%d",&n)){
42         memset(tree,0,sizeof tree);
43         memset(color,false,sizeof color);
44         memset(wb,false,sizeof wb);
45         for(int i = tot = 0; i < n; ++i){
46             scanf("%d%d%s",x+i,y+i,op);
47             wb[i] = *op == 'w';
48             y[i]++;
49             d[tot++] = x[i];
50             d[tot++] = y[i];
51         }
52         sort(d,d+tot);
53         tot = unique(d,d+tot) - d;
54         for(int i = 0; i < n; ++i){
55             int nx = lower_bound(d,d+tot,x[i])-d;
56             int ny = lower_bound(d,d+tot,y[i])-d;
57             update(0,tot,nx,ny,wb[i],1);
58         }
59         query(0,tot,1);
60         int i = 0,j = 0,ret = 0,L = 0,R = 0;
61         while(i < tot && j < tot){
62             if(color[i]){
63                 j = i;
64                 while(i < tot && color[i]) ++i;
65                 if(d[i] - d[j] + 1 > ret){
66                     ret = d[i] - d[j];
67                     L = d[j];
68                     R = d[i]-1;
69                 }
70             }
71             i++;
72         }
73         if(L >= R) puts("Oh, my god");
74         else printf("%d %d\n",L,R);
75     }
76     return 0;
77 }
View Code

 

posted @ 2015-04-30 21:02  狂徒归来  阅读(239)  评论(0编辑  收藏  举报