数组计算机
来源:互联网 发布:遗传算法基本原理 编辑:程序博客网 时间:2024/06/16 21:48
数组计算机
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss
Problem Description
bLue 有一个神器的机器,这个机器可以读入一个数组,并按照用户要求快速地进行数组的处理和计算,它支持如下两种操作:
操作 1:把数组中第 p 个元素的值增加 v。
操作 2:计算数组中 [l, r] 区间内所有数的和。
这个机器就是这么的神奇,但是 bLue 的计算机坏掉了,你能帮他修一下吗?
Input
输入数据有多组(数据组数不超过 20),到 EOF 结束。
对于每组数据:
第 1 行输入一个整数 n (1 <= n <= 10^5),表示数组中元素的个数。
第 2 行输入 n 个用空格隔开的整数 ai (1 <= ai <= 10^10),表示初始输入到计算机中的数组。
第 3 行输入一个整数 q (1 <= q <= 50000),表示用户的操作次数。
接下来 q 行,每行输入先输入 1 个整数,表示操作类型,根据不同的操作类型:
如果类型为 1,则紧接着输入 2 个用空格隔开的整数 p (1 <= p <= n) 和 v (1 <= v <= 10^10),表示要把数组中第 p 个数的值增加 v。
如果类型为 2,则紧接着输入 2 个用空格隔开的整数 l, r (1 <= l <= r <= n),表示要计算区间 [l, r] 内所有数的和(数组下标从 1 开始)。
Output
对于每组数据中的每次类型为 2 的操作,输出 1 行,包含一个整数,表示计算出的和。
Example Input
5
1 2 3 4 5
5
2 1 2
2 1 5
1 4 10
2 4 5
2 1 5
Example Output
3
15
19
25
Hint
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{ long long val;}tree[400000];long long data[400000];void bulid(int root,int left,int right){ if(left==right) { tree[root].val = data[left]; return ; } int mid = (left+right)/2; bulid(root*2+1,left,mid); bulid(root*2+2,mid+1,right); tree[root].val = tree[root*2+1].val +tree[root*2+2].val;}void updata(int root,int left,int right,int pos,long long val){ if(left==right) { tree[root].val += val; return ; } int mid = (left + right)/2; if(pos<=mid) updata(root*2+1,left,mid,pos,val); else updata(root*2+2,mid+1,right,pos,val); tree[root].val = tree[root*2+1].val + tree[root*2+2].val;}long long query(int root,int left,int right,int qleft,int qright){ long long res = 0; if(qleft<=left&&qright>=right) return tree[root].val; int mid = (left + right)/2; if(qleft<=mid) res += query(root*2+1,left,mid,qleft,qright); if(qright>mid) res += query(root*2+2,mid+1,right,qleft,qright); return res;}int main(){ int n; while(~scanf("%d",&n)) { memset(data,0,sizeof(data)); memset(tree,0,sizeof(tree)); for(int i = 0;i < n;i++) { scanf("%lld",&data[i]); } bulid(0,0,n-1); int m; scanf("%d",&m); while(m--) { long long x,y,z; scanf("%lld %lld %lld",&x,&y,&z); if(x==1) { updata(0,0,n-1,y-1,z); } else if(x==2) { printf("%lld\n",query(0,0,n-1,y-1,z-1)); } } }}
- 数组计算机
- 数组计算机
- 树状数组----数组计算机
- sdutacm-数组计算机
- 线段树—数组计算机
- 数组计算机(线段树+水题)
- 北航计算机机试09数组排序
- 数组计算机 单点更新和区间查询
- 计算机数学作业之求两个数组的交集问题
- 简单的计算机二维数组的元素和
- 计算机图形学(二)输出图元_12_OpenGL顶点数组
- SDUT 3771 数组计算机 线段树 单点增减
- 计算机数组作为函数形参的一些基本问题
- 计算机
- 计算机
- 计算机
- 计算机
- 计算机
- MapReduce原理与设计思想
- MySQL的源码编译与配置
- Mybatis学习笔记五 修改数据与删除数据
- Web前端之注册信息
- mobilenet
- 数组计算机
- DevOps指北
- 资深面试官谈如何面试
- Ubuntu上安装protobuf 和protobuf-c
- Action的自定义方法
- JAVA 执行 dos/shell命令的实现
- [译文]CSS的水平/垂直居中:一篇完整的指南
- Unity学习——UGUI开发之滑动条Slider
- linux shell编程多线程和wait命令学习