Valhalla Siege

  • 又是水题一道。
  • 代码:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 200010
int n, q;
long long a[MAXN], b[MAXN], sum;
int main() {
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), sum += a[i];
    for (int i = 1; i <= n; i++) a[i] += a[i - 1];
    for (int i = 1; i <= q; i++) {
        scanf("%lld", &b[i]);
        b[i] += b[i - 1];
        if (b[i] >= sum) b[i] = 0;
    }
    for (int i = 1; i <= q; i++) {
        register int l = 1, r = n, ans = 0;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (a[mid] > b[i])
                ans = mid, r = mid - 1;
            else
                l = mid + 1;
        }
        printf("%d\n", n - ans + 1);
    }
    return 0;
}

Leave a Reply