codeforces 1042d Petya and Array 线段树|树状数组|平衡树

本文介绍了一种使用splay树解决数组中求和小于特定值的子序列数量问题的方法。通过预处理前缀和并维护splay树,可以高效地计算出所有符合条件的子序列。文章详细阐述了splay树的旋转、插入和查找操作,以及如何利用这些操作解决给定问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Petya and Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya has an array aa consisting of nn integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array.

Now he wonders what is the number of segments in his array with the sum less than tt. Help Petya to calculate this number.

More formally, you are required to calculate the number of pairs l,rl,r (l≤rl≤r) such that al+al+1+⋯+ar−1+ar<tal+al+1+⋯+ar−1+ar<t.

Input

The first line contains two integers nn and tt (1≤n≤200000,|t|≤2⋅10141≤n≤200000,|t|≤2⋅1014).

The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (|ai|≤109|ai|≤109) — the description of Petya's array. Note that there might be negative, zero and positive elements.

Output

Print the number of segments in Petya's array with the sum of elements less than tt.

Examples

input

Copy

5 4
5 -1 3 4 -1

output

Copy

5

input

Copy

3 0
-1 2 -3

output

Copy

4

input

Copy

4 -1
-2 1 -2 3

output

Copy

3

题目大意:给定数组a和一个数字t,问有多少对i,j使得子序列(i,j)的和小于t。

预处理前缀和sum[i],对于位置i,即求有多少个j使得sum[i] - sum[j] < t,即sum[i] - t < sum[j],所以维护下sum数组中有多少个大于sum[i] - t即可,之前都是用线段树写的,这次用splay写了下。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

#define ll long long
#define lson tr[x][0]
#define rson tr[x][1]
const int maxn = 2e5 + 10;
const int INF = 1e9;
int root, tot, n;
ll t, ans;
int  fa[maxn], tr[maxn][2], sz[maxn];
ll sum[maxn], key[maxn];
int num[maxn], a[maxn];

int judge(int x) {
    return tr[fa[x]][1] == x;
}

void pushup(int x) {
    if (x) {
        sz[x] = sz[tr[x][0]] + sz[tr[x][1]] + num[x];
    }
}

void rotate(int x) {
    int y = fa[x], d = judge(x);
    if (tr[y][d] = tr[x][d ^ 1]) fa[tr[y][d]] = y;
    if (fa[x] = fa[y]) tr[fa[y]][judge(y)] = x;
    tr[fa[y] = x][d ^ 1] = y;
    pushup(y);
}

void splay(int x, int k) {
    for (int y; (y = fa[x]) != k; rotate(x))
        if (fa[y] != k) rotate(judge(x) == judge(y) ? y : x);
    pushup(x);
    if (!k) root = x;
}
   
void insert(ll val) {
    int node = root, f = 0;
    while (node && key[node] != val) {
        f = node;
        node = tr[node][key[node] < val];
    }
    if (node) num[node] += 1;
    else {
        node = ++tot;
        if (f) tr[f][key[f] < val] = node;
        fa[node] = f;
        key[node] = val;
        sz[node] = num[node] = 1;
    }
    splay(node, 0);
}

void find(ll x) {
    int r = root;
    while (key[r] != x && tr[r][key[r] < x])
        r = tr[r][key[r] < x];
    splay(r, 0);
}

int main() {
    scanf("%d%I64d", &n, &t);
    tot = root = ans = sum[0] = 0;
    insert(0);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        sum[i] = sum[i - 1] + a[i];
    }
    for (int i = 1; i <= n; i++) {
        find(sum[i] - t);
        if (key[root] > (sum[i] - t)) ans += (sz[tr[root][1]] + num[root]);
        else ans += sz[tr[root][1]];
        insert(sum[i]);
    }
    printf("%I64d\n", ans);
    return 0;
}

 

### CodeForces 上关于树状数组的题目列表 在CodeForces平台上,存在多个涉及树状数组的数据结构问题。以下是部分精选题目及其简短描述: #### 1. **799C - Fountains** 此题要求实现单点更新操作,在`A[]`数组中修改某一位置的值并相应地更新辅助数组`tree[]`。具体来说,当执行增加操作时,会通过循环不断更新受影响节点直到超出范围为止[^1]。 ```cpp void add(int k, int num) { while (k <= n) { tree[k] += num; k += k & (-k); } } ``` #### 2. **1042D - Array and Operations** 该挑战涉及到利用树状数组线段树来高效处理特定类型的查询请求。核心思路在于将原始问题转化为计算前缀和中小于给定阈值的数量统计问题,从而简化了解决方案的设计过程[^2]。 #### 3. **1635F - Closest Pair** 这道难题不仅考察了参赛者对树状数组的理解程度,还测试了其解决复杂逻辑的能力。题目背景设定在一个有序序列基础上,目标是在指定范围内找到具有最小化评估函数的一对索引组合[^3]。 #### 4. **703D - Mishka and Interesting Contest** 本题聚焦于如何有效地求解区间内所有不同数值的异或结果。采用的方法是预先按右端点排序各区间,并记录每种元素最后一次出现的位置;之后借助树状数组完成后续所需的快速查找与更新工作[^4]。 这些例子展示了树状数组作为一种强大工具的应用场景,能够显著提升算法效率特别是在面对大规模数据集的情况下表现尤为突出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值