博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Line Painting
阅读量:5287 次
发布时间:2019-06-14

本文共 2990 字,大约阅读时间需要 9 分钟。

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

Problem Description
The segment of numerical axis from 0 to 10
9 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', 
ai
bi
ci 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 < 10
9.
 

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
 
input output
4 1 999999997 b 40 300 w 300 634 w 43 47 b
47 634
 

 

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 5000
struct node
{
    int L, R, color;
    int Mid(){return (L+R)/2;}
};
node a[maxn*4*2];
int point[maxn*2+10], npoint;
int maxv, minv, First, Last, color;
void BuildTree(int r, int L, int R);
void Insert(int r, int L, int R, int color);
void Query(int r);
int main()
{
    int N;
    while(scanf("%d", &N) != EOF)
    {
        int i, L[maxn+10], R[maxn+10], c[maxn+10];
        char ch;
        for(npoint=i=0; i<N; i++)
        {
            cin >> L[i] >> R[i] >> ch;
            c[i] = (ch == 'w' ? 0 : 1);
            point[npoint++] = L[i];
            point[npoint++] = R[i];
        }
        point[npoint++] = 0, point[npoint++] = 1000000000;
        sort(point, point+npoint);
        npoint = unique(point, point+npoint) - point;
        BuildTree(1, 0, npoint-1);
        for(i=0; i<N; i++)
        {
            L[i] = lower_bound(point, point+npoint, L[i]) - point;
            R[i] = lower_bound(point, point+npoint, R[i]) - point;
            Insert(1, L[i], R[i], c[i]);
        }
        maxv = minv = First = Last = 0;
        color = 0;
        Query(1);
        printf("%d %d\n", minv, maxv);
    }
    return 0;
}
void BuildTree(int r, int L, int R)
{
    a[r].L = L, a[r].R = R, a[r].color = 0;
    if(R - L == 1)return ;
    BuildTree(r*2, L, a[r].Mid());
    BuildTree(r*2+1, a[r].Mid(), R);
}
void Insert(int r, int L, int R, int C)
{
    if(a[r].color == C)return ;
    if(a[r].L == L && a[r].R == R)
    {
        a[r].color = C;
        return ;
    }
    if(a[r].color >= 0)
        a[r*2].color = a[r*2+1].color = a[r].color;
    a[r].color = -1;
    if(R <= a[r].Mid())
        Insert(r*2, L, R, C);
    else if(L >= a[r].Mid())
        Insert(r*2+1, L, R, C);
    else
    {
        Insert(r*2, L, a[r].Mid(), C);
        Insert(r*2+1, a[r].Mid(), R, C);
    }
}
void Query(int r)
{
    if(a[r].color == 0)
    {
        Last = a[r].R;
        if(color == 1)
        {
            color = a[r].color;
            First = a[r].L;
        }
        if(maxv-minv < point[Last] - point[First])
                maxv = point[Last], minv = point[First];
        return ;
    }
    if(a[r].color == 1)
    {
        color = 1;
        return ;
    }
    Query(r*2);
    Query(r*2+1);
}

 

转载于:https://www.cnblogs.com/liuxin13/p/3873506.html

你可能感兴趣的文章
揭开Redis的神秘面纱
查看>>
Object流
查看>>
Windows Phone开发(8):关于导航的小技巧 转:http://blog.csdn.net/tcjiaan/article/details/7285062...
查看>>
Ajax学习笔记1之第一个Ajax应用程序
查看>>
css3新单位vw、vh、vmin、vmax的使用详解(转载)
查看>>
软件测试培训第30天
查看>>
centos7 关闭防火墙
查看>>
04-jQuery的属性操作
查看>>
response实现文件下载
查看>>
【WP7】页面之间数据交互
查看>>
C++中的unique函数
查看>>
小白学数据分析----->流失分析设计
查看>>
FontAwesome 奥森图标的学习
查看>>
request response cookie session
查看>>
NMON记录服务器各项性能数据
查看>>
未找到arm-linux-gcc解决办法
查看>>
统计Xcode项目代码行数
查看>>
认识 service worker
查看>>
第五次团队作业:项目展示
查看>>
C#面向对象(二):封装和继承
查看>>