AcWing - 120 - defense and the prefix + = dichotomy

https://www.acwing.com/problem/content/122/

Question is intended: to 2e6 intervals, each interval number three Si, Ei, Di, expressed in [, Ei Si] in the range of from every other Si has a Di equipment, the range [0, INT_MAX].

Topic ensure that there are an odd number of equipment up to a point, to find this point.

Commentary title, only to find this kind of a odd point, prefix and think it is very natural (after now learned very natural).

So half of this position and seek prefix on it.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, S[200005], E[200005], D[200005];

int query(int x, int i) {
    //询问x位置及其之前有多少个防具i
    if(x < S[i])
        return 0;
    if(D[i] == 0)
        return 1;
    if(x > E[i])
        x = E[i];
    return (x - S[i]) / D[i] + 1;
}

int sum(int x) {
    //询问x位置及其之前的防具前缀和是不是奇数
    int s = 0;
    for(int i = 1; i <= n; ++i) {
        s += query(x, i);
    }
    //printf("x=%d sum=%d\n", x, s);
    return s;
}

int check(int x) {
    return (sum(x) & 1);
}

int bs() {
    int l = 0, r = INT_MAX;
    while(1) {
        int m = l + ((r - l) >> 1);
        if(l == m) {
            if(check(l))
                return l;
            else if(check(r))
                return r;
            else
                return -1;
        }
        if(check(m))
            r = m;
        else
            l = m + 1;
    }
}

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i) {
            scanf("%d%d%d", &S[i], &E[i], &D[i]);
        }
        int ans = bs();
        if(ans == -1)
            puts("There's no weakness.");
        else
            printf("%d %d\n", ans, sum(ans) - sum(ans - 1));
    }
}

Guess you like

Origin www.cnblogs.com/Inko/p/11680308.html