爆0求助
查看原帖
爆0求助
575643
AaronLaw201楼主2023/10/1 23:20

两个样例都过了,思路跟35pts一样。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <map>
#define int long long
using namespace std;
const int N = 2e5+10;
int n, q, a[N];

inline int read() {
	int s = 0, w = 1;
	char ch = getchar();
	for (;!isdigit(ch);ch=getchar())	if (ch == '-')	w = -1;
	for (;isdigit(ch);ch=getchar())	s=(s<<1)+(s<<3)+(ch^48);
	return s * w;
}

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();
	while (q--) {
		int ans = 0, l = read(), r = read();
		int sum = 0, num = 0;
		for (int i = r; i >= l; i--) {
			if (a[i] > 0) {
				sum = a[i], ans++, i--;
				while (a[i] <= 0 && i >= l && sum+a[i] > 0)	ans++, i--, sum += a[i];
				i++;
			}
		}
		cout << ans << endl;
	}	
	
	return 0;
}
2023/10/1 23:20
加载中...