hdu 1116 敌兵布阵(树状数组区间求和)
题意:
给出一行数字,然后可以修改其中第i个数字,并且可以询问第i至第j个数字的和(i <= j)。
输入:
首行输入一个t,表示共有t组数据。
接下来每行首行输入一个整数n,表示共有n个数字。
接下来每行首先输入一个字符串,如果是”Add”,接下来是两个整数a,b,表示给第i个数增加b。如果是”Query”,接下来是两个整数a,b,表示查询从第i个数到第j个数之间的和。如果是”End”,表示这组数据结束。
输出:
每组数据首先输出”Case X: ”,其中X表示第x组数。
每次查询,输出计算结果,每个结果占一行。
题解:
点修改与区间求和,可以用树状数组模板。
具体见代码——
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; const int N = ; int t, n;
int a[N];
int c[N];
char s[];
int x, y; int lowbit(int x)
{
return x&(-x);
} void Add(int x, int y)
{
a[x] += y;
while(x <= n)
{
c[x] += y;
x += lowbit(x);
}
} int Sum(int x)
{
int rt = ;
while(x > )
{
rt += c[x];
x -= lowbit(x);
}
return rt;
} int Summ(int l, int r)
{
int rt = ;
while(r >= l)
{
if(r-lowbit(r) < l)
{
rt += a[r];
r -= ;
}
else
{
rt += c[r];
r -= lowbit(r);
}
}
return rt;
} void Query(int a, int b)
{
printf("%d\n", Sum(b)-Sum(a-));
//printf("%d\n", Summ(a, b));
} int main()
{
//freopen("test.in", "r", stdin);
scanf("%d", &t);
for(int tm = ; tm <= t; tm++)
{
scanf("%d", &n);
memset(c, , sizeof(c));
memset(a, , sizeof(a));
int y;
for(int i = ; i <= n; i++)
{
scanf("%d", &y);
Add(i, y);
}
printf("Case %d:\n", tm); scanf("%s", s);
while(s[] != 'E')
{
scanf("%d%d", &x, &y);
if(s[] == 'A') Add(x, y);
if(s[] == 'Q') Query(x, y);
if(s[] == 'S') Add(x, -y);
scanf("%s", s);
}
}
}
树状数组区间求和模板——
http://www.cnblogs.com/mypride/p/5001858.html
hdu 1116 敌兵布阵(树状数组区间求和)的更多相关文章
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- HDU 1166 敌兵布阵(树状数组)
之前用过了线段树的做法,树状数组的也补上吧 #include<iostream> #include<cstdio> #include<cstring> using ...
- HDU 1166 敌兵布阵 树状数组小结(更新)
树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
- HDU1166 敌兵布阵(树状数组)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- hdu1166 敌兵布阵 树状数组/线段树
数列的单点修改.区间求和 树状数组或线段树入门题 #include<stdio.h> #include<string.h> ],N; void add(int x,int a) ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- hdoj 1166 敌兵布阵(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...
- hdu-1166 敌兵布阵---树状数组模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 维护动态的区间和,单点更新,就是模板题 #include<iostream& ...
- HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)
题目链接 AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
随机推荐
- c#的委托用法delegate
- Lua程序设计(三)面向对象实现一个简单的类
1.Lua面向对象实现步骤 ①创建一个全局表(称之为元表) ②设置这个元表的__index值(值通常为元表自己,这样就能通过__index查找到对应的属性和方法)__index 赋值其实是一个func ...
- Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang
Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang The hugely popular Liberty Mut ...
- 【LibreOJ】#6396. 「THUPC2018」弗雷兹的玩具商店 / Toyshop 线段树+完全背包
[题目]#6396. 「THUPC2018」弗雷兹的玩具商店 / Toyshop [题意]给定一个长度为n的物品序列,每个物品有价值.不超过m的重量.要求支持以下三种操作:1.物品价值区间加减,2.物 ...
- SVM Kernel Functions
==================================================================== This article came from here. Th ...
- 终极利器!利用appium和mitmproxy登录获取cookies
环境搭建 参考我之前写的https://www.cnblogs.com/c-x-a/p/9163221.html appium 代码start_appium.py # -*- coding: utf- ...
- bulk_write&Replace_one
ns=[]ns.append(ReplaceOne({'ip': ok['ip']}, ok, upsert=True))#更新插入 if len(ns) > 0: res = coll.bul ...
- 局域网搭建https局域网
局域网搭建https局域网 1.使用tomcat作为服务器搭建局域网访问https 需要使用java jdk\bin下的keytool.exe来创建证书 使用命令:keytool -genkenpai ...
- sqlmap工作流程图
- Windows 10安装pip方法
pip是一款非常方便的python包管理工具,本文主要介绍在windows 10下安装pip方法. 1. 下载pip 地址:https://pypi.python.org/pypi/pip#downl ...