AcWing
(我写这道题的时候CH已经萎掉了,所以只有AcWing的链接)
分析
由于这题数据范围很大,我们无法直接统计每个位置的防具数量,于是考虑二分,在每次二分的过程中统计二分范围内的防具数量,记为 \(cnt\) 。然后因为偶数+偶数=偶数,而奇数+偶数=奇数,我们可以利用这个性质来判断是否符合条件,于是 \(check\) 函数直接返回 \(cnt \ \text{&} \ 1\) 即可。
注意要用 \(unsigned\) 。
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define il inline
#define re register
#define maxn 200005
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &x) {
T f = 1; x = 0; char c;
for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
x *= f;
}
int n;
unsigned l, r;
unsigned sum[maxn], s[maxn], e[maxn], d[maxn];
bool check(unsigned lim) {
unsigned cnt = 0;
for (int i = 1; i <= n; ++i)
if (s[i] <= lim)
cnt += (min(lim, e[i]) - s[i]) / d[i] + 1;
return cnt & 1;
}
void solve() {
read(n); l = 0, r = 1U << 31;
for (int i = 1; i <= n; ++i) read(s[i]), read(e[i]), read(d[i]);
while(l < r) {
unsigned mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (l == (1U << 31)) puts("There's no weakness.");
else {
unsigned cnt = 0;
for (int i = 1; i <= n; ++i)
if (s[i] <= l && e[i] >= l && (l - s[i]) % d[i] == 0)
cnt++;
printf("%u %u\n", l, cnt);
}
}
int main() {
int t;
read(t);
while(t--) solve();
return 0;
}