ural1019 Line Painting

本文介绍了一种寻找经过多次重涂后的最长连续白色区间的算法。通过离散化、线段树及答案二分法,解决给定区间上多次重新着色后的最长白色开放区间问题。文中提供了一个完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

inputoutput
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+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,c[maxn],ma;
int ans[2];
struct Node
{
    int sum, lazy;
} T[maxn<<2];

void PushUp(int rt)
{
    T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum;
}

void PushDown(int L, int R, int rt)
{
    int mid = (L + R) >> 1;
    int t = T[rt].lazy;
    T[rt<<1].sum = t * (mid - L + 1);
    T[rt<<1|1].sum = t * (R - mid);
    T[rt<<1].lazy = T[rt<<1|1].lazy = t;
    T[rt].lazy = 0;
}

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 + 1);
        return ;
    }
    int mid = (L + R) >> 1;
    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+1, 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) >> 1;
    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 + 1, r, Rson);
}
struct node
{
    int x,y;
    char b[2];
}a[maxn];
int main()
{
    int i,j;
    j=0;
    scanf("%d",&n);
    c[j++]=0,c[j++]=1e9-1;
    rep(i,1,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-1,c[j++]=a[i].y-1;
    sort(c,c+j);
    int num=unique(c,c+j)-c;
    rep(i,1,n)
    {
        a[i].x=lower_bound(c,c+num,a[i].x)-c+1;
        a[i].y--;
        a[i].y=lower_bound(c,c+num,a[i].y)-c+1;
        Update(a[i].x,a[i].y,(a[i].b[0]=='b'),1,num,1);
    }
    rep(i,1,num)
    {
        int l=i,r=num;
        while(l<=r)
        {
            int mid=l+r>>1;
            if(Query(i,mid,1,num,1)==0)
            {
                if(ma<c[mid-1]-c[i-1]+1)
                {
                    ma=c[mid-1]-c[i-1]+1;
                    ans[0]=c[i-1];
                    ans[1]=c[mid-1]+1;
                }
                l=mid+1;
            }
            else r=mid-1;
        }
    }
    printf("%d %d\n",ans[0],ans[1]);
    //system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/5827677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值