Color the Ball


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

解题思路:n未知,因此离散化好处理,线段树的区间更新

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int maxn=;
struct node
{
int l,r,co;
}tree[maxn*];
int n,cnt,tot;
int num[maxn],ha[maxn],color[maxn],co[maxn],li[maxn],ri[maxn];
int find(int x)
{
int l=,r=cnt;
while(l<=r)
{
int mid=(l+r)>>;
if(ha[mid]==x) return mid;
else if(ha[mid]<x) l=mid+;
else r=mid-;
}
return -;
}
void build(int l,int r,int root)
{
tree[root].l=l,tree[root].r=r,tree[root].co=;
if(l==r) return ;
int mid=(l+r)>>;
build(l,mid,root<<);
build(mid+,r,root<<|);
}
void update(int l,int r,int root,int val)
{
if(tree[root].l==l && tree[root].r==r)
{
tree[root].co=val;
return;
}
if(tree[root].co==val) return;
if(tree[root].co!=-)
{
tree[root<<].co=tree[root<<|].co=tree[root].co;
tree[root].co=-;
}
if(r<=tree[root<<].r) update(l,r,root<<,val);
else if(l>=tree[root<<|].l)update(l,r,root<<|,val);
else update(l,tree[root<<].r,root<<,val),update(tree[root<<|].l,r,root<<|,val);
}
void query(int l,int r,int root)
{
if(tree[root].co!=-)
{
for(int i=tree[root].l;i<=tree[root].r;i++)
color[i]=tree[root].co;
return;
}
if(r<=tree[root<<].r) query(l,r,root<<);
else if(l>=tree[root<<|].l) query(l,r,root<<|);
else query(l,tree[root<<].r,root<<),query(tree[root<<|].l,r,root<<|); }
int main(int argc,char *argv[])
{
char s[];
while(scanf("%d",&n)!=EOF)
{
tot=;
memset(color,,sizeof(color));
for(int i=;i<n;i++)
{
scanf("%d%d%s",&li[i],&ri[i],s);
co[i]=s[]=='b'?:;
num[tot++]=li[i];
num[tot++]=ri[i];
}
sort(num,num+tot);
int t=tot;
for(int i=;i<t;i++)
{
if(num[i]-num[i-]>) num[tot++]=num[i-]+;
if(num[i]-num[i-]>) num[tot++]=num[i]-;
}
sort(num,num+tot);
cnt=;
ha[++cnt]=num[];
for(int i=;i<tot;i++)
if(num[i-]!=num[i]) ha[++cnt]=num[i];
build(,maxn,);
for(int i=;i<n;i++)
{
int a=find(li[i]);
int b=find(ri[i]);
update(a,b,,co[i]);
}
query(,maxn,);
int s,e,ans=,i=,j;
while(i<=cnt)
{
if(color[i]==)
{
j=i;
while(color[j]== && j<=cnt) j++;
int t=ha[j-]-ha[i]+;
if(t>ans)
{
ans=t;
s=ha[i];
e=ha[j-];
}
i=j;
}
else i++;
}
if(!ans) printf("Oh,my god\n");
else printf("%d %d\n",s,e);
}
return ;
}

ZOJ 2301 Color the Ball 线段树(区间更新+离散化)的更多相关文章

  1. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  2. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  3. Color the ball 线段树 区间更新但点查询

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  4. hdu 1556 Color the ball 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

  5. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  6. hdu1556Color the ball线段树区间更新

    题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...

  7. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

随机推荐

  1. hdu1501 Zipper--DFS

    原题链接:pid=1501">http://acm.hdu.edu.cn/showproblem.php?pid=1501 一:原题内容 Problem Description Giv ...

  2. .Net中字典的使用

    /// <summary> /// 获取用户市信息 /// </summary> /// <param name="CustomerId">&l ...

  3. Gym - 100338E Numbers 贪心

    Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsign ...

  4. 分享一段wap框架样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. SQL server 事务介绍,创建与使用

    事务(Transaction)事务是一种机制,一个操作序列,包含一组操作指令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求(即要么全部执行,要么全部不执行)---------------- ...

  6. 2017-2018年红头发新版Cisco认证网络工程师(CCNA-R&S)全新讲解分享

    网名"红头发",多年授课经验,业内资深思科认证讲师,其所写的CISCO认证原创技术文章风靡各大网站与培训机构.精通CISCO各类路由交换产品,熟悉JUNIPER M/T系列路由产品 ...

  7. which---查找并显示给定命令的绝对路径

    which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就可 ...

  8. Swift学习笔记(14)--方法

    1.分类 方法分为实例方法和类型方法 实例方法(Instance Methods):与java中的类似,略 类型方法(Type Methods):与java.oc中的类方法类似.声明类的类型方法,在方 ...

  9. zoj 2317 Nice Patterns Strike Back(矩阵乘法)

    problemId=1317">http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=1317 给出一个n*m的矩阵( ...

  10. es6 -- 透彻掌握Promise的使用,读这篇就够了

    Promise的重要性我认为我没有必要多讲,概括起来说就是必须得掌握,而且还要掌握透彻.这篇文章的开头,主要跟大家分析一下,为什么会有Promise出现. 在实际的使用当中,有非常多的应用场景我们不能 ...