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

ZOJ Monthly, February 2005

解题:线段树。。。区间涂色。。最后问最长的连续白色区间起止位置

 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 }

时间: 2024-12-30 03:21:21

HDU 1199 Color the Ball的相关文章

hdu 1199 Color the Ball 离散线段树

C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 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 t

线段树 [HDU 1199] Color the Ball

Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4529    Accepted Submission(s): 1114 Problem Description There are infinite balls in a line (numbered 1 2 3 ....), and initially a

HDU 1199 - Color the Ball 离散化

[题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后找到最长的连续白色球,输出左右端点符号 [离散化]因为l[i]和r[i]都在int范围,显然不不可以开一个2^31-1那么大的数组.将l[i]和r[i]+1离散化,再模拟染色即可. 如果你不知道离散化: 将l[i]数组所有数与r[i]+1数组所有数取出来从小到大排序,做一个映射. 如样例 3 1 4 w

ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化,为了防止离散后错误,不仅将L,R离散,还要加入L+1,L-1,R+1,R-1一起离散,这样就绝不会有问题了.然后建线段树,线段树维护四个值: 1.col  区间颜色  0 表示黑  1 表示白  -1表示无标记 2.maxi 区间内最大白区间的长度,由于白色用1表示,所以最大白区间的长度即为区间最

HDU 1556 Color the ball 线段树

HDU 1556 Color the ball 线段树模版题,存个模板 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #inclu

hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8984    Accepted Submission(s): 4594 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球

hdu 1556:Color the ball(线段树,区间更新,经典题)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7941    Accepted Submission(s): 4070 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电

HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)

HDU - 1556 Color the ball Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气

hdu 1556 Color the ball(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气