题目描述

一个长度为N的数组A, 所有数都是整数 ,0 <= A[i] <= 1000000,1 <= i <= N,1 <= N <= 100000,对于 任意i,j ,1 <=  i  <=  j  <= N,[i, j]中所有数为原数组的一个子区间, 现在要求子区间的和小于等于K的子区间有多少个, 0 <=  K  <= 10000。

虽然xry111很SB,但还是在O(N)的时间复杂度内就做出了这题,你呢?

输入

第一行整数T 代表数据组数,1 <= T  <=  12

每组数据第一行 整数 N, K。

接下来一行N个整数,  由空格隔开。

输出

输出子区间的和小于等于K的子区间的个数。 每组输出占一行。

--正文

明明是简单的题,却做了半天。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
long long a[];
int n,k,i,j;
int main(){
int time,T; scanf("%d",&T);
for (time=;time<=T;time++){
int now = ;
LL sum = ,res = ;
scanf("%d %d",&n,&k);
for (i=;i<=n;i++){
scanf("%lld",&a[i]);
sum += a[i];
if (sum <= k) {
res += i - now + ;
}
else {
while (sum > k){
sum = sum - a[now]; now ++;
}
res += i - now + ;
}
}
printf("%lld\n",res);
}
return ;
}

XidianOJ 1099 A simple problem的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  4. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

随机推荐

  1. mybatis实战教程(mybatis in action)之三:实现数据的增删改查

    前面已经讲到用接口的方式编程.这种方式,要注意的一个地方就是.在User.xml  的配置文件中,mapper namespace="com.yihaomen.mybatis.inter.I ...

  2. 实践总结 - 不可错过的Angular应用技巧

    angular的核心思想是通过数据驱动一切,其他东西都是数据的延伸. 套用Javascript一切皆对象的思想,在angular中可以说一切皆数据. 关于项目构建 (1) requirejs以及Yeo ...

  3. USVN

    我们最近将快盘上的东西迁移到了svn上,因为快盘总是不会不小心删掉或者修改了某些文件.为了能保留历史记录我们统一迁移到svn上.为了方便权限管理,我对比了几个svn的权限管理工具,最后觉得还是usvn ...

  4. Katana

    - KAT101 - Katana has many nodes for operation, e.g. MaterialAssign, - The connections between nodes ...

  5. Mysql5.5源码安装步骤笔记记录

    1.cmake软件的安装wget https://cmake.org/files/v3.5/cmake-3.5.0-rc3.tar.gztar xf cmake-3.5.0.tar.gzcd cmak ...

  6. hadoop2升级的那点事情(详解)

    前言 前阵子,公司的hadoop从hadoop1.02升级到hadoop2.4.1,记录下升级的步骤和遇到的问题,和大家分享,希望别人可以少走一些弯路 技术选型 当前使用版本:     apache ...

  7. C++11实现生产者消费者问题

    生产者消费者问题是多线程并发中一个非常经典的问题.我在这里实现了一个基于C++11的,单生产者单消费者的版本,供大家参考. #include <windows.h> #include &l ...

  8. Centos 6.5 SNMP客户端安装及配置版本net-snmp-5.7.3

    Centos 6.5 SNMP客户端安装及配置SNMP版本:net-snmp-5.7.3.tar.gz1.下载软件cd /usr/local/srcyum -y install gccwget htt ...

  9. 500lines项目简介

    "500行或更少" "What I cannot create, I do not understand." -- Richard Feynman <50 ...

  10. jquery.validate[.unobtrusive]和Bootstrap实现tooltip错误提示

    类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效果如下: 后端使用Asp.net mvc5,前端框架有:jquery.validate.jq ...