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

时间:2023-03-10 06:11:26
URAL-1019 Line Painting----暴力或线段树

题目链接:

https://cn.vjudge.net/problem/URAL-1019

题目大意:

一个0~1e9的区间,初始都是白的,现进行N次操作,每次将一段区间图上一中颜色。最后问说连续最长的白色区间。

解题思路:

先离散化,之后可暴力,或者用线段树维护

离散化后,染色时候应注意从第二个点开始染色。

比如:1 2 3 4 5 6 7 8 9 10,现在染色7 - 9

那么只需要把8 9 染色即可,因为7-9是两段区间,用8表示7-8区间,9表示8-9区间。

这样表示不会出错。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = + ;
struct node
{
int u, v;
char s[];
}a[maxn];
int num[maxn], tot, color[maxn];
map<int, int>Map;
int main()
{
int n;
cin >> n;
num[tot++] = , num[tot++] = 1e9;
for(int i = ; i <= n; i++)
{
scanf("%d%d%s", &a[i].u, &a[i].v, &a[i].s);
num[tot++] = a[i].u;
num[tot++] = a[i].v;
}
sort(num, num + tot);
tot = unique(num, num + tot) - num;
for(int i = ; i < tot; i++)
{
Map[num[i]] = i;
}
for(int i = ; i <= n; i++)
{
int tmp = ;
if(a[i].s[] == 'b')tmp = ;
for(int j = Map[a[i].u] + ; j <= Map[a[i].v]; j++)
{
color[j] = tmp;
}
//for(int i = 0; i < tot; i++)cout<<color[i]<<" ";
//cout<<endl;
}
int l, r, length = ;
for(int i = ; i < tot; i++)
{
if(color[i])continue;
int j = i;
while(color[j] == && j < tot)j++;
if(length < num[j - ] - num[i - ])
{
l = num[i - ];
r = num[j - ];
length = r - l;
}
}
cout<<l<<" "<<r<<endl;
return ;
}

下次写上线段树的代码: