本文介绍了如何利用树状数组(也称作线段树)来快速查找区间 [1, r] 内小于特定值 x 的数的个数。通过树状数组的构造和更新操作,可以实现对区间查询的高效解答。"
127083007,10320444,O(1)时间复杂度删除链表节点的方法,"['数据结构', '链表操作']
#include<bits/stdc++.h>usingnamespace std;#define FOR0(a,b) for(int i = a; i < b; ++i)#define FORE(a,b) for(int i = a; i <= b; ++i)typedeflonglong ll;typedef pair<int,int> pii;constint maxn =3e5+5;int n,m,tot;
ll a[maxn];
ll pre[maxn];
ll c[maxn];intf(ll x){returnint(lower_bound(pre,pre+tot,x)-pre)+1;}intlowbit(int x){return x&(-x);}voidadd(int x){while(x <= tot+1){
c[x]+=1;
x +=lowbit(x);}}
ll sum(int x){
ll ret =0;while(x >0){
ret += c[x];
x -=lowbit(x);}return ret;}intmain(){scanf("%d%d",&n,&m);for(int i =1; i <= n;++i){scanf("%lld",&a[i]);}for(ll i =1; i <= n;++i){
a[i]+= a[i-1];
pre[i]= a[i];}sort(pre,pre+n+1);
tot =unique(pre,pre+n+1)-pre;add(f(0));
ll ans =0;for(int i =1; i <= n;++i){
ll t =upper_bound(pre,pre+tot,a[i]-m)-pre;// cout << a[i]-m <<" " << t << endl;
ans +=sum(tot+1)-sum(t);add(f(a[i]));// cout << ans << endl;}
cout << ans << endl;return0;}