题意:有从 1 开始递增依次编号的很多球,开始他们都是黑色的,现在依次给出 n 个操作(ai,bi,ci),每个操作都是把编号 ai 到 bi 区间内

的-所有球涂成 ci 表示的颜色(黑 or 白),然后经过 n 次给定的操作后,求最长的连续白色区间的左端点和右端点。

析:由于数比较大,可以先进行离散化,然后是区间更新,最后还循环暴力一次,单点求值。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 5;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
LL l, r;
int c;
};
Node a[maxn];
vector<LL> v;
map<LL, int> mp; int sum[maxn<<4];
int setv[maxn<<4]; void push_down(int rt){
if(setv[rt] == -1) return ;
setv[rt<<1] = setv[rt];
setv[rt<<1|1] = setv[rt];
sum[rt] = setv[rt];
sum[rt<<1] = setv[rt];
sum[rt<<1|1] = setv[rt];
setv[rt] = -1;
} void update(int L, int R, int val, int l, int r, int rt){
if(L <= l && r <= R){
setv[rt] = val;
sum[rt] = val;
return ;
}
push_down(rt);
int m = l + r >> 1;
if(L <= m) update(L, R, val, lson);
if(R > m) update(L, R, val, rson);
} int query(int M, int l, int r, int rt){
if(l == r) return sum[rt];
push_down(rt);
int m = l + r >> 1;
if(M <= m) return query(M, lson);
return query(M, rson);
} int main(){
while(scanf("%d", &n) == 1){
v.clear();
mp.clear();
char s[5];
for(int i = 0; i < n; ++i){
scanf("%d %d %s", &a[i].l, &a[i].r, s);
a[i].c = s[0] == 'b' ? 0 : 1;
v.push_back(a[i].l);
v.push_back(a[i].r);
v.push_back(a[i].l-1);
v.push_back(a[i].r-1);
v.push_back(a[i].l+1LL);
v.push_back(a[i].r+1LL);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i = 0; i < v.size(); ++i)
mp[v[i]] = i + 1;
memset(sum, 0, sizeof sum);
memset(setv, -1, sizeof setv);
int len = v.size();
for(int i = 0; i < n; ++i){
if(a[i].l > a[i].r) continue;
update(mp[a[i].l], mp[a[i].r], a[i].c, 1, len, 1);
}
LL l = -1, r = -1, ans = 0;
LL ll = -1, rr = -1;
for(int i = 0; i < len; ++i){
if(query(i+1, 1, len, 1)){
if(ll == -1) ll = v[i];
rr = v[i];
if(ans < rr - ll + 1){
ans = rr - ll + 1;
l = ll, r = rr;
} }
else ll = rr = -1;
} if(ans == 0) cout << "Oh, my god\n";
else cout << l << " " << r << endl;
}
return 0;
}

  

ZOJ 2301 Color the Ball (离散化+线段树)的更多相关文章

  1. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

    题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...

  2. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

  4. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 1556 Color the ball (技巧 || 线段树)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. Color the ball(线段树)

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. hdu1556 Color the ball 简单线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 简单的线段树的应用 直接贴代码了: 代码: #include<iostream> # ...

  9. hdu 1199 Color the Ball 离散线段树

    C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. BZOJ - 2618 凸多边形 (半平面交)

    题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...

  2. C#异步编程(四)混合模式线程同步

    之前讨论了基元用户模式和内核模式线程同步构造.其他所有线程同步构造都基于它们,而且一般都合并了用户模式和内核模式构造,我们称为混合线程同步构造.没有线程竞争时,混合构造提供了基元用户模式构造所具有的性 ...

  3. Git在不同环境换行符设置

    首先我们在eclipse查看两个环境文件的换行符区别: 产生背景 关于“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别.在计算机还没有出现之前,有一种叫 ...

  4. [转] CentOS系统目录学习

    最近初学Linux 对linux的目录产生了很多疑问,看到这篇文章,让我顿时对目录有了一个清晰的认识!推荐给大家! ------------------------------------------ ...

  5. spring容器启动的三种方式

    一.在Web项目中,启动Spring容器的方式有三种,ContextLoaderListener.ContextLoadServlet.ContextLoaderPlugin. 1.1.监听器方式: ...

  6. adb基本命令总结(Android Debug Bridge)

    adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作 adb devices           列出你的devices adb kill-server         杀掉ad ...

  7. vi,sed,tr,awk技巧

    将文件中的换行替换为逗号 使用sed: sed -e :a -e N -e '$!ba' -e 's/\n/,/g' filename 使用tr: cat filename | tr '\n' ',' ...

  8. Python之整数,浮点数和布尔类型

    整数和浮点数有那个四则运算: 两种类型的数可以直接进行加减,当整数和浮点数进行加减的时候,结果会自动的变为浮点数,其中除法运算是“/”来表示的, 而余数的算术符号是“%”来表示的.如果是为了求除完后的 ...

  9. X—shell的安装以及与Linux的链接(http://www.cnblogs.com/v-weiwang/p/5029559.html)

    X—shell作为一种强大的远程操作工具,使我们能够简单的去操作虚拟机,因此呢我们最好是能够在我们的电脑上进行安装. X—shell作为一个工具我们无论什么版本的都可以,在安装的时候呢也特别的简单,但 ...

  10. sql server导入excel等数据

    1.首先打开并登陆sql server数据库 2.选择要将表导入的数据库,右击选择任务-->导入数据 3.在弹出的窗口中选择下一步 4.在弹出的窗口中选择数据源,也就是从哪种文件导入,sql s ...