ZOJ Unsafe Factor

这道题刚开始感觉是线段树问题,后然查了解题报告。发现方法很暴力,我还是有一些疑问,L的值10^7,n1和n2是10^5,为什么不会超时呢?解题报告上用了transform 和 find 两个SLT的函数,我把transform 换成了for循环后增加了10ms的时间,而把find函数换成for循环后直接TLE,网上查了说find的时间复杂度也是线性的,那为什么用find只要160ms,而用for循环就直接TLE了呢?

wataishi的代码如下,好好学习!

#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

bool bs[10001000];

int main() {
	int l, n1, n2, a, b, ans;

	while (scanf("%d%d%d", &l, &n1, &n2) != EOF) {
		n1 += n2;
		fill(bs, bs + l + 1, false);
		for (int i = 0; i < n1; ++i) {
			scanf("%d%d", &a, &b);
			transform(bs + a, bs + b + 1, bs + a, logical_not<bool>());
		}
		ans = 0;
		for (int i = 0; i <= l; ) {
			if (bs[i]) {
				int j = find(bs + i, bs + l + 1, false) - bs;
				ans = max(ans, j - i);
				i = j;
			} else {
				++i;
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值