思路:题目意思是有多个等差数列,每一个等差数列都有起点终点和公差,每一个数都会在x轴对应位置上累加1,最后所有的点中只有一个点上的数字是奇数。
用前缀和+二分的思想来看mid左边的区间和如果是奇数说明答案在左边,要让r=mid,否则让l=mid+1,求某一段有一个数列在某个区间上的点的数量和公式:(min(e,mid)-s)/d+1;计算n次就可以得到mid左边的点的数量。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=2e5+10;
typedef pair<int,int>PII;
typedef long long LL;
struct node
{
int s,e,d;
}e[N];
int n;
LL get_sum(int x)
{
LL res=0;
for(int i=1;i<=n;i++)
if(e[i].s<=x)
res+=(min(e[i].e,x)-e[i].s)/e[i].d+1;
return res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int l=0,r=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int s,e1,d;
scanf("%d%d%d",&s,&e1,&d);
e[i]={s,e1,d};
r=max(r,e1);
}
if(!(get_sum(r)&1))
{puts("There's no weakness.");
continue;
}
while(l<r)
{
int mid=(l+r)>>1;
if(get_sum(mid)&1) r=mid;
else l=mid+1;
}
LL sum=get_sum(r)-get_sum(r-1);
if(sum%2) printf("%d %lld\n",r,sum);
else puts("There's no weakness.");
}
return 0;
}