HRBUST 1161 树状数组区间更新求和
| Leyni | ||||||
|
||||||
| Description | ||||||
|
Leyni被人掳走,身在水深火热之中... 这个保护圈由n个小的小护盾围成一圈,从1到n编号。当某一块小护盾受到攻击的时候,小护盾就会抵消掉这次攻击,也就是说对这一块小护盾的攻击是无效攻
现在小奈叶专心战斗,Leyni昏迷,他们无法得知小护盾遭受的有效攻击次数,他们需要你的帮助。
只要能帮到他们,Leyni就会赠送出一份小奈叶写真集。 ![]() |
||||||
| Input | ||||||
|
第一行是一个整数T,表示有多少组测试数据。 |
||||||
| Output | ||||||
|
每一组测试数据,先输出一行"Case i:",i表示第i组测试数据,从1开始计数。 |
||||||
| Sample Input | ||||||
|
1 |
||||||
| Sample Output | ||||||
|
Case 1: |
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
const int MAXN=;
int c[MAXN],p[MAXN];
int xiji;
int n,q,t,aa,b; int lowbit(int x)
{
return x&(-x);
}
void add(int x,int val)
{
int i=x;
while(i<=n)
{
c[i]+=val;
i+=lowbit(i);
}
}
int get_sum(int x)
{
int s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
} int main()
{
int k;
int s;
char str[];
s=;
scanf("%d",&k);
for(int i=;i<k;i++)
{
s++;
memset(c,,sizeof(c));
scanf("%d%d%d",&n,&q,&t);
for(int i=; i<=n; i++)
p[i]=-t;
xiji=;
printf("Case %d:\n",s);
for(int i=; i<=q; i++)
{
scanf("%s",&str);
if(str[]=='A')
{
xiji++;
scanf("%d",&aa);
if(p[aa]+t<=xiji)
p[aa]=xiji;
else
{
add(aa,);
}
}
else if(str[]=='Q')
{
scanf("%d%d",&aa,&b);
if(aa>b)
swap(aa,b);
printf("%d\n",get_sum(b)-get_sum(aa-));
}
}
}
return ;
} 注:没有线段树快哦~~~~ get_sum()函数求起点到该点的和 ******************************************************
http://blog.csdn.net/shahdza/article/details/6314818
******************************************************
HRBUST 1161 树状数组区间更新求和的更多相关文章
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
- poj_3468: A Simple Problem with Integers (树状数组区间更新)
题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. 这道题也可以用线段树解,本文不做描述,下面分析如何用树状数组来解决这道题. 先把问 ...
- POJ 3468 A Simple Problem with Integers(树状数组区间更新) 续
这个题刚开始的时候是套模板的,并没有真的理解什么树状数组的区间更新,这几天想了一下,下面是总结: 区间更新这里引进了一个数组delta数组,delta[i]表示区间 [i, n] 的共同增量,每次你需 ...
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97217 ...
随机推荐
- VS2010错误
1.用VS2010生成C++程序时,链接器工具错误 LNK1123: fatal error LNK1123: failure during conversion to COFF: file inva ...
- 将某视图View转换为UIImage
+ (UIImage *)getSharedScreenView{ UIWindow *screenWindow = [[UIApplication sharedApplication]keyWind ...
- js处理img标签加载图片失败,显示默认图片
1.第一种方法: 如果已经引入了jquery插件,就很好办.没有的话,如果实在需要,可以附上代码: script(type='text/javascript', src="http://aj ...
- spring boot 启动自动跳到 断点 throw new SilentExitException
项目 debug 启动,自动跳到 断点 ,而且就算F8 ,项目还是停止启动. 百度了一下,答案都是 Eclipse -> Preferences ->Java ->Debug去掉&q ...
- VisualStudio2017密钥(key)我随便输入一下居然通过了?????????!!!!!!!!!!!!
VisualStudio2017密钥(key) 2017年12月30日 14:41:38 阅读数:5048 professional:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH ent ...
- centos 6.5 搭建zookeeper集群
为什么使用Zookeeper? 大部分分布式应用需要一个主控.协调器或控制器来管理物理分布的子进程(如资源.任务分配等)目前,大部分应用需要开发私有的协调程序,缺乏一个通用的机制协调程序的反复编写浪费 ...
- this高级应用 - 域隔离
在js环境中,this有很多指向(window.dom.object等),巧妙的利用this,可以有效的防止变量或方法被外界污染,保证代码健壮性,实例如下. demo: <!DOCTYPE ht ...
- .net获取本地ip地址
整理代码,.net获取本地ip地址,代码如下: string name = Dns.GetHostName(); IPHostEntry IpEntry = Dns.GetHostEntry(name ...
- javabean为什么需要序列化
无论用hibernate或者mybatis结合spring做开发还是其他,系统里持久类往往要实现序列化, implements Serializable.我还是比较好奇,为什么要这样做呢?一直只知道个 ...
- Socket 学习笔记 01 常用函数
常用方法 创建套接字: socket() 绑定本机端口: bind() 建立连接: connect(),accept() 侦听端口: listen() 数据传输: send() ...


