这题又用了两个之前不怎么用的函数,以及方法之前也没怎么用过,自己想的时候很挣扎,知道自己想的肯定会超时,然后看了别人的代码才明白,故记录。
题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=1316
以及题中用到的两个vector函数的详细链接:点击打开链接
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
LL sum[10010];
vector<LL> v;
int main()
{
int t;
cin>>t;
while(t--){
int n,L,m;
cin>>n>>L>>m;
v.clear();
memset(sum,0,10010);
for(int i=1;i<=n;i++){
int tmp;
scanf("%d",&tmp);
sum[i]=sum[i-1]+tmp;
}
LL l=1,r=L,ans=0;
v.push_back(0);
while(r<=n){
int div=sum[r]-m;//差值
int pl=upper_bound(v.begin(),v.end(),div)-v.begin();//比左边大的个数即有几个子区间满足(前缀贡献数)
ans+=pl;
int pr=lower_bound(v.begin(),v.end(),sum[l])-v.begin();//找到此前缀的位置插入vector
v.insert(pr+v.begin(),sum[l]);
r++;
l++;
}
printf("%lld\n",ans);
}
return 0;
}