Line Painting

Posted mxzf0213

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Line Painting相关的知识,希望对你有一定的参考价值。

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;
}

以上是关于Line Painting的主要内容,如果未能解决你的问题,请参考以下文章

如何修复此错误:偏移参数包含 NaN 值。 'dart:ui/painting.dart':断言失败:第 43 行:'<优化输出>'

C. Painting the Fence

UVA 253 Cube painting

[USACO 2017DEC] Barn Painting

Rotational Painting(hdu 3685 凸包+多边形重心 模板题

Rotational Painting(hdu 3685 凸包+多边形重心 模板题