ural1019 Line Painting

时间:2023-02-19 19:15:29

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&period;Line Painting&lpar;线段树 离散化)

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

  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&colon;&sol;&sol;blog&period;csdn&period;net&sol;zearot&sol;article&sol;details&sol;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 &num;353 &lpar;Div&period; 2&rpar;Restoring Painting

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

  9. hdu-4810 Wall Painting&lpar;组合数学&rpar;

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

随机推荐

  1. jquery easyui window中的datagrid,只能显示一次问题

    最近项目中用到easyui 的动态创建window ,window中嵌入了datagruid.第一次打开是能显示数据,但再次打开时确没显示: 注:url已成功返回了数据. 多次查阅easyui帮助文档 ...

  2. HW5&period;17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. Android 调试native的crash和anr

    1. 于trace找到相应的库.例如 liba.so和相应的地址信息 2. 采用addr2line 查看 addr2line 住址 -e liba.so -f 要么 arm-eabi-addr2lin ...

  4. button点击切换,获取按钮ID

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

  5. 小步快跑的公司可以最简化操作直接通过log4net将日志写入ElasticSearch

     很多小步快跑的公司,开发人员多则3-4个,面对巨大业务压力,日连夜的赶着上线,快速试错,自然就没时间搭建一些基础设施,比如说logCenter,但初期 项目不稳定,bug又多,每次都跑到生产去找日志 ...

  6. Volatile的那些事

    上一篇中,我们了解了Synchronized关键字,知道了它的基本使用方法,它的同步特性,知道了它与Java内存模型的关系,也明白了Synchronized可以保证"原子性",&q ...

  7. PAT甲题题解-1073&period; Scientific Notation &lpar;20&rpar;-字符串处理

    题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostrea ...

  8. Ubuntu12&period;10 使用JLink连接开发板用arm-gdb调试ARM程序

    Part1 环境搭建和工具安装 1.1 设置交叉编译环境 安装相关的编译工具: sudo apt-get install build-essential gcc-arm-linux-gnueabi 这 ...

  9. RDS for MySQL有哪些限制

    原文来自:https://help.aliyun.com/knowledge_detail/41834.html 1.不支持在命令行创建数据库和数据库账号.只支持在RDS管理控制台操作. 2.不支持M ...

  10. python爬虫搜片利器fmovice【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python/ 前言 讲真!小编不管看什么电影(大的.小的),不管什么电视剧,小编都没买 ...