Range Add Query

数列 A = {a1,a2,...,an} に対し、次の2つの操作を行うプログラムを作成せよ。

  • add(s,t,x)as,as+1,...,at にxを加算する。
  • get(i)aiの値を出力する。

ただし、ai (i=1,2,...,n)は、0 で初期化されているものとする。

入力

n q
query1
query2
:
queryq

1行目にAの要素数n, クエリの数qが与えられる。続くq行に i 番目のクエリ queryi が与えられる。queryi は以下のいずれかの形式で与えられる。

0 s t x

または

1 i

各クエリの最初の数字は、クエリの種類を示し、'0'がadd(s,t,x)、'1'がget(i)を表す。

出力

getクエリについて、値を1行に出力せよ。

制約

  • 1≤n≤100000
  • 1≤q≤100000
  • 1≤stn
  • 1≤in
  • 0≤x≤1000

入力例 1

3 5
0 1 2 1
0 2 3 2
0 3 3 3
1 2
1 3

出力例 1

3
5

入力例 2

4 3
1 2
0 1 4 1
1 2

出力例 2

0
1

 
 

区间加 单点查

树状数组+差分 或 线段树

缩常数,继续打榜

 #include <cstdio>
#include <cstdlib> #define siz 10000000 char buf[siz], *bit = buf; inline int nextInt(void) {
register int ret = ;
register int neg = false; for (; *bit < ''; ++bit)
if (*bit == '-')neg ^= true; for (; *bit >= ''; ++bit)
ret = ret * + *bit - ''; return neg ? -ret : ret;
} int n, m; int pre[]; inline int ask(int p) {
int ret = ;
for (; p; p -= p&-p)
ret += pre[p];
return ret;
} inline void add(int p, int k) {
for (; p <= n; p += p&-p)
pre[p] += k;
} signed main(void) {
fread(buf, , siz, stdin); n = nextInt();
m = nextInt(); for (int i = ; i <= m; ++i) {
int c = nextInt();
if (c) // get(i)
printf("%d\n", ask(nextInt()));
else // add(s, t, x)
{
int x = nextInt();
int y = nextInt();
int k = nextInt();
add(x, k); add(y + , -k);
}
} //system("pause");
}

@Author: YouSiki

AOJ DSL_2_E Range Add Query (RAQ)的更多相关文章

  1. AOJ DSL_2_D Range Update Query (RUQ)

    Range Update Query 数列 A = {a0,a1 ,...,an−1} に対し.次の2つの操作を行うプログラムを作成せよ. update(s,t,x): as,as+1,...,at  ...

  2. AOJ DSL_2_A Range Minimum Query (RMQ)

    Range Minimum Query (RMQ) Write a program which manipulates a sequence A = {a0,a1,...,an−1} with the ...

  3. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  5. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  6. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  7. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. range for query

    static void range_test(Args _args) { Query                   Query; QueryRun                QueryRun ...

随机推荐

  1. ORA-00600: internal error code, arguments: [kcratr1_lastbwr], [], [], [], [], [], [], []

    今天在PlateSpin Forge(关于PlateSpin相关介绍,请见最下面部分简单介绍) 复制出来的一台数据库服务器上,测试数据库能否正常启动时,遇到了"ORA-00600: inte ...

  2. JVM之SerialOld收集器

    Serial收集器的老年代版本 单线程收集器 标记-整理算法 stop the world Client模式下的虚拟机使用 Server模式下,搭配Parallel Scavenge使用及CMS发生C ...

  3. 使用管道符在PowerShell中进行各种数据操作

    最近在培训PowerShell,在讲到Pipeline的时候,对于我这种长期和数据(数据库)打交道的人来说,觉得很实用,所以写此博文,记录一下. 无论是在Linux中写Bash脚本还是在Window上 ...

  4. SQL Server 2008 R2——使用计算列为表创建自定义的自增列

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  5. Echarts 之三 —— 地市联动数据统计二

    一.简介 除了是一个地图之外,我们也可以使用多地图进行地市.区县联动数据统计.需求如下:展示整改广东省的地图,并显示统计信息,当点击某一个地市的时候,就显示该地市的地图,并统计该地市区县的数据信息.二 ...

  6. linux shell取文本最后一行

    目前我知道的方法有四种 1.awk 'END {print}'   2.sed -n '$p'   3.sed '$!N;$!D'   4.awk '{b=a"\n"$0;a=$0 ...

  7. KVM 存储虚拟化 - 每天5分钟玩转 OpenStack(7)

    KVM 的存储虚拟化是通过存储池(Storage Pool)和卷(Volume)来管理的. Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种类型,后面会详细讨论.Volume 是 ...

  8. 编译软件基础知识(2/2) via LinuxSir

    首先说下/etc/ld.so.conf: 这个文件记录了编译时使用的动态链接库的路径. 默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件 如果你安装了某些库,比如在安装gtk+ ...

  9. Linux标准输入输出

    linux启动后,会默认打开3个文件描述符,分别是:标准输入standard input 0,正确输出standard output 1,错误输出:error output 2 以后打开文件后.新增文 ...

  10. Hibernate核心技术简介

    Hibernate核心技术简介 1.Hibernate映射文件开发     Hibernate映射文件就是项目中*.hbm.xml文件,其主要是完成各元素的配置,包括根元素.类元素.定义主键.设置主键 ...