Codeforces Round #510 (Div. 2) D. Petya and Array
程序员文章站
2022-05-09 15:08:02
...
题意:
给出一个序列a,求区间[i,j]的个数,使得和小于t
树状数组维护前缀和
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <cmath>
#include <queue>
using namespace std;
int n;
long long t;
long long a[200005];
long long b[200005];
int cnt;
long long tr[200005];
int lb(int x)
{
return x&(-x);
}
void add(int x)
{
while(x<=200002)
{
tr[x]++;
x+=lb(x);
}
}
long long get(int x)
{
long long ans=0;
while(x>0)
{
ans+=tr[x];
x-=lb(x);
}
return ans;
}
int main() {
while(~scanf("%d%lld",&n,&t))
{
memset(tr,0,sizeof(tr));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]),a[i]+=a[i-1];
for(int i=0;i<=n;i++)
{
b[i]=a[i];
}
sort(b,b+n+1);
cnt=unique(b,b+n+1)-b;
long long ans=0;
int id=lower_bound(b,b+cnt,a[0])-b;
add(id+1);
//sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
long long tp=a[i]-t;
id=upper_bound(b,b+cnt,tp)-b;
id--;id++;
if(id!=0)
{
ans+=(i-get(id));
} else
ans+=i;
id=lower_bound(b,b+cnt,a[i])-b;
add(id+1);
}
printf("%lld\n",ans);
}
return 0;
}
上一篇: Berland Fair CodeForces - 1073D(模拟题)
下一篇: 【Codeforces Round #510 (Div. 2) D.Petya and Array】 树状数组
推荐阅读
-
Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree
-
Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree(dfs)
-
Codeforces Round #619 (Div. 2) D. Time to Run
-
Codeforces Round #583 (Div. 1 + Div. 2,) D. Treasure Island(dfs+思维)
-
Codeforces Round #533 (Div. 2) D. Kilani and the Game(bfs+模拟)
-
Codeforces Round #643 (Div. 2) D. Game With Array
-
Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
-
Codeforces Round #260 (Div. 2) D. A Lot of Games(树上博弈)
-
Codeforces Round #662 (Div. 2) D. Rarity and New Dress
-
Codeforces Round #658 (Div. 2) D. Unmerge(dp,01背包)