题解 CH0805 【防线】

题目链接:Link

Problem

Solution

由于题目中提到了奇数位置要么没有,要么有1个,所以若区间[L,R]中有奇数位置,等价于这个区间里的防具数量为奇数个。
考虑到n只有200000,可以考虑二分奇数点的位置p,然后判断区间[0,p]中的防具的个数。
显然对于一组防具\((s_i,e_i,d_i)\),它对于个数的贡献是$\Large \frac{min(e_i,p)-s_i}{d_i} +1 \( 但要注意:\)min(e_i,p)\(有可能小于\)s_i$,需要特判。

Code

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=200005;
int T,n;
LL s[maxn],e[maxn],d[maxn],maxe;
inline LL sum(LL p)
{
	LL tot=0;
	for(int i=0;i<n;i++) if(min(e[i],p)>=s[i]) tot+=(min(e[i],p)-s[i])/d[i]+1;
	return tot;
}
int main()
{
#ifdef local
	freopen("pro.in","r",stdin);
#endif
	scanf("%d",&T);
	while(T-->0)
	{
		scanf("%d",&n);
		maxe=0;
		for(int i=0;i<n;i++)
		{
			scanf("%lld%lld%lld\n",&s[i],&e[i],&d[i]);
			maxe=max(maxe,e[i]);
		}
		LL L=0,R=maxe+1,M,res=maxe+1;
		while(L<=R)
		{
			M=(L+R)>>1;
			if(sum(M)&1) res=M,R=M-1;
			else L=M+1;
		}
		if(res==maxe+1) puts("There's no weakness.");
		else printf("%lld %lld\n",res,sum(res)-sum(res-1));
	}
	return 0;
}
posted @ 2019-08-19 19:10  happyZYM  阅读(140)  评论(0编辑  收藏  举报