[Link](4316. 合适数对 - AcWing题库)
设 s i s_i si为 a i a_i ai的前缀和,等价于 s r − s l − 1 < t → s r − t < s l − 1 s_r-s_{l-1}
#include
#define x first
#define y second
#define debug(x) cout<<#x<<":"<
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 4e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
LL n, m, k, t;
vector<LL> ve;
int g(LL x) {
return lower_bound(ve.begin(), ve.end(), x) - ve.begin() + 1;
}
int tr[N];
int lowbit(int x) {
return x & -x;
}
void add(int x) {
for ( ; x < N; x += lowbit(x)) tr[x] ++;
}
int sum(int x) {
int res = 0;
for (; x; x -= lowbit(x)) res += tr[x];
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> t;
vector<LL> s(n + 1);
for (int i = 1; i <= n; i ++) cin >> s[i], s[i] += s[i - 1], ve.push_back(s[i]), ve.push_back(s[i] - t);
ve.push_back(0), ve.push_back(-t);
sort(ve.begin(), ve.end());
ve.erase(unique(ve.begin(), ve.end()), ve.end());
LL res = 0;
add(g(0));
for (int i = 1; i <= n; i ++) {
res += i - sum(g(s[i] - t));
add(g(s[i]));
}
cout << res << '\n';
return 0;
}
我们要找的是 s r − s l < t s_r-s_l
#include
#define x first
#define y second
#define debug(x) cout<<#x<<":"<
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 2e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
LL n, m, k, t;
LL a[N];
LL res;
void merge(int l, int r) {
if (l >= r) return ;
int mid = l + r >> 1;
merge(l, mid), merge(mid + 1, r);
for (int i = l, j = mid + 1; i <= mid; i ++) {
while (j <= r && a[j] - a[i] < t) j ++;
res += j - mid - 1;
}
inplace_merge(a + l, a + mid + 1, a + r + 1);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> t;
for (int i = 1; i <= n; i ++) cin >> a[i], a[i] += a[i - 1];
merge(0, n);
cout << res << '\n';
return 0;
}