B. 【例题2】防具布置
题目
设
S
(
i
)
S(i)
S(i)为0~
i
i
i中所有的防具
若破绽在位置mid,那么
S
(
l
)
S(l)
S(l)为偶数,
S
(
r
)
S(r)
S(r)为奇数(
l
<
m
i
d
≤
r
l<mid\leq r
l<mid≤r)
二分答案
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
ll n,s[1000001],d[1000001],e[1000001],t;
void in()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>s[i]>>e[i]>>d[i];
}
}
ll S(ll a)
{
ll ans=0;
for(int i=1; i<=n; i++)
{
if(s[i]<=a) ans+=(min(a,e[i])-s[i])/d[i]+1;
}
return ans;
}
void work()
{
if(S(2147483647)%2==0) cout<<"There's no weakness."<<endl;
else
{
ll l=0,r=2147483647;
while(l<r)
{
ll mid=(l+r)>>1;
if(S(mid)%2==1) r=mid;
else l=mid+1;
}
cout<<l<<" "<<S(l)-S(l-1)<<endl;
}
}
int main()
{
cin>>t;
while(t--)
{
in();
work();
}
}