Line Painting

Time limit: 2.0 second
Memory limit: 64 MB
The segment of numerical axis from 0 to 109 is painted into white color. After that some parts of this segment are painted into black, then some into white again and so on. In total there have been made N re-paintings (1 ≤ N ≤ 5000). You are to write a program that finds the longest white open interval after this sequence of re-paintings.

Input

The first line of input contains the only number N. Next N lines contain information about re-paintings. Each of these lines has a form:
ai bi ci
where ai and bi are integers, ci is symbol 'b' or 'w', aibici are separated by spaces. 
This triple of parameters represents repainting of segment from ai to bi into color ci ('w' — white, 'b' — black). You may assume that 0 < ai < bi < 109.

Output

Output should contain two numbers x and y (x < y) divided by space(s). These numbers should define the longest white open interval. If there are more than one such an interval output should contain the one with the smallest x.

Sample

input output
4
1 999999997 b
40 300 w
300 634 w
43 47 b
47 634

分析:离散化+线段树+答案二分;

   坑点2个:一是要把0和1e9边界考虑到,二是要注意染色和答案都是左闭右开区间;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e4+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,c[maxn],ma;
int ans[];
struct Node
{
int sum, lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
int t = T[rt].lazy;
T[rt<<].sum = t * (mid - L + );
T[rt<<|].sum = t * (R - mid);
T[rt<<].lazy = T[rt<<|].lazy = t;
T[rt].lazy = ;
} void Update(int l, int r, int v, int L, int R, int rt)
{
if(l==L && r==R)
{
T[rt].lazy = v;
T[rt].sum = v * (R - L + );
return ;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) Update(l, r, v, Lson);
else if(l > mid) Update(l, r, v, Rson);
else
{
Update(l, mid, v, Lson);
Update(mid+, r, v, Rson);
}
PushUp(rt);
}
int Query(int l, int r, int L, int R, int rt)
{
if(l==L && r== R)
{
return T[rt].sum;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) return Query(l, r, Lson);
else if(l > mid) return Query(l, r, Rson);
return Query(l, mid, Lson) + Query(mid + , r, Rson);
}
struct node
{
int x,y;
char b[];
}a[maxn];
int main()
{
int i,j;
j=;
scanf("%d",&n);
c[j++]=,c[j++]=1e9-;
rep(i,,n)scanf("%d%d%s",&a[i].x,&a[i].y,a[i].b),c[j++]=a[i].x,c[j++]=a[i].y,c[j++]=a[i].x-,c[j++]=a[i].y-;
sort(c,c+j);
int num=unique(c,c+j)-c;
rep(i,,n)
{
a[i].x=lower_bound(c,c+num,a[i].x)-c+;
a[i].y--;
a[i].y=lower_bound(c,c+num,a[i].y)-c+;
Update(a[i].x,a[i].y,(a[i].b[]=='b'),,num,);
}
rep(i,,num)
{
int l=i,r=num;
while(l<=r)
{
int mid=l+r>>;
if(Query(i,mid,,num,)==)
{
if(ma<c[mid-]-c[i-]+)
{
ma=c[mid-]-c[i-]+;
ans[]=c[i-];
ans[]=c[mid-]+;
}
l=mid+;
}
else r=mid-;
}
}
printf("%d %d\n",ans[],ans[]);
//system("pause");
return ;
}

ural1019 Line Painting的更多相关文章

  1. Line Painting

    题目大意;说是可以吧一段区间变成白色或者黑色, 区间(0-10^9)初始都是白色,问经过n次操作以后最大的连续白色区间 Problem Description The segment of numer ...

  2. URAL-1019 Line Painting----暴力或线段树

    题目链接: https://cn.vjudge.net/problem/URAL-1019 题目大意: 一个0~1e9的区间,初始都是白的,现进行N次操作,每次将一段区间图上一中颜色.最后问说连续最长 ...

  3. 1019.Line Painting(线段树 离散化)

    1019 离散化都忘记怎么写了 注意两个端点 离散化后用线段树更新区间 混色为-1  黑为2  白为1  因为N不大 最后直接循环标记这一段的颜色查找 #include <iostream> ...

  4. URAL 1019 - Line Painting

    跟前面某个题一样,都是区间染色问题,还是用我的老方法,区间离散化+二分区间端点+区间处理做的,时间跑的还挺短 坑爹的情况就是最左端是0,最右端是1e9,区间求的是开区间 #include <st ...

  5. Aizu The Maximum Number of Customers

    http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_A The Maximum Number of Customers Ide ...

  6. 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)

    原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解    By 岩之痕 目录: 一:综述 ...

  7. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  8. Codeforces Round #353 (Div. 2)Restoring Painting

    Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was sto ...

  9. hdu-4810 Wall Painting(组合数学)

    题目链接: Wall Painting Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. b端商家赋值权限

  2. List<T>转换为ObservableCollection<T>

    ObservableCollection能通知他变化了也正是因为它实现了INotifyPropertyChanged接口, 在wpf项目中经常会遇到把List<T>转换为Observabl ...

  3. ExceL转PDF

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = ...

  4. PHP导出Excel 数字末尾变0或小数点解决办法

    为了管理方便,查阅直观性.很多网站, 或者系统都会自带导出excel或者cvs的功能.但是很多情况下,由于数字超过15位,会被显示成0或者加小数点处理.造成这种情况是由于Excel内 置的数值有效范围 ...

  5. eclipse的调试方法的简单介绍

    声明:本文不是自己 作为编程人员,程序的调试是一项基本功.在不使用IDE的时候,程序的调试多数是通过日志或者输入语句(System.out.println)的方式.可以把程序运行的轨迹或者程序运行过程 ...

  6. liunx 定时执行 php文件

    which php    寻找php路径

  7. Jenkins email-ext邮件通知模板

    http://blog.csdn.net/houyefeng/article/details/51344337 示例 以html格式发送送如下内容:邮件内容为项目名称.构建次数.触发原因.构建日志前1 ...

  8. HTTP的学习

    一个完整的HTTP请求: 1 简历TCP连接 2 web浏览器像web服务器发送请求命令 3 web浏览器发送请求头信息 4 web服务器应答 5 web服务器发送应答头信息 6 web服务器像浏览器 ...

  9. android 5.0新特性学习--视图轮廓

    ViewOutlineProvider -- 视图轮廓setOutlineProvider--可以使用View.setClipToOutline方法去剪切一个视图的outline区域.只有rectan ...

  10. android 5.0新特性学习--CardView

    CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. 官网地址:https://de ...