求调!悬关!必关!
查看原帖
求调!悬关!必关!
576387
amlhdsan楼主2024/12/31 23:11
//
//  C.cpp
//  mx
//
//  Created by 缪语博 on 12/31/24.
//

#include <bits/stdc++.h>

#define N 300010
#define int long long

using namespace std;

int n, q;
int a[N];
int t[N], top = 0;
int lst[N];
int sum[N];

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}

inline void write(int x) {
    if(x < 0) {
        putchar('-');
        x = -x;
    }
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

signed main() {
    
    n = read();
    q = read();
    
    for(int i = 1; i <= n; ++i) {
        a[i] = read();
        if(a[i] > 0) {
            t[++top] = i;
            int tmp = 0;
            for(int j = i - 1; j >= 1 && a[j] <= 0; --j) {
                tmp += a[j];
                if(tmp + a[i] > 0 && tmp + a[j - 1] + a[i] <= 0 || j == 1) {
                    lst[top] = j;
                    break;
                }
            }
        }
    }

    for(int i = 1; i <= top; ++i) {
        sum[i] = sum[i - 1] + t[i] - lst[i] + 1;
    }

    while(q--) {
        int l = read(), r = read();
        if(l > r) {
            write(0);
            putchar('\n');
            continue;
        }
        int ans = 0;
        int tl = lower_bound(t + 1, t + top + 1, l) - t;
        int tr = upper_bound(t + 1, t + top + 1, r) - t - 1;
        // cout << tl << " " << tr << endl;
        if(tl <= tr) {
            ans += sum[tr] - sum[tl - 1];
            if(lst[tl] < l) {
                ans -= l - lst[tl];
            }
        }
        write(ans);
        putchar('\n');
    }
    
    
    return 0;
}

2024/12/31 23:11
加载中...