题解 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;
}
本作品由happyZYM采用知识共享 署名-非商业性使用-相同方式共享 4.0 (CC BY-NC-SA 4.0) 国际许可协议(镜像(简单版)镜像(完整版))进行许可。
转载请注明出处:https://www.cnblogs.com/happyZYM/p/11379039.html (近乎)全文转载而非引用的请在文首添加出处链接。