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. IOS RunLoop浅析 三

    经过两篇的介绍我想对RunLoop应该有了简单的了解,至少不至于一无所知. 在这篇我想对“CFRunLoopObserverRef”做一下简单的补充. 在补充之前先说一下. 在现在的开发中已经很少见到 ...

  2. css padding 填充

    语法: padding:[ <length> | <percentage> ]{1,4} 默认值:看每个独立属性 适用于:所有元素,除 table-row-group | ta ...

  3. 如何转换SQL Server 2008数据库到SQL Server 2005

        背景介绍: 公司一套系统使用的是SQL SERVER 2008数据库,突然一天收到邮件,需要将这套系统部署到各个不同地方(海外)的工厂,需要在各个工厂部署该数据库,等我将准备工作做好,整理文档 ...

  4. socket-详细分析No buffer space available

    关键词:socket,tcp三次握手,tcp四次握手,2MSL最大报文生存时间,LVS,负载均衡 新年上班第一天,突然遇到一个socket连接No buffer space available的问题, ...

  5. 随手小代码——生成GUID

    GUID stGUID; CString strGUID; if (S_OK==::CoCreateGuid(&stGUID)) { strGUID.Format( TEXT("{% ...

  6. percona教程:MySQL GROUP_CONCAT的使用

    percona有一篇blog: The power of MySQL GROUP_CONCAT 比较详细地介绍了GROUP_CONCAT函数的用法.简单地翻译了一下. 假设你有4名工程师,这周他们为6 ...

  7. 鼠标 mouseover和mouseout事件

    在div内想实现鼠标移入移出效果,最开始的时候是用了jquery的mouseout和mouseover事件来实现的, $('.product).mouseover(function(){ …… }). ...

  8. [No000054] Windows 下Python3.5, NoteBook增强版安装

    接着上周继续,没看的童鞋.请移步: http://www.cnblogs.com/Chary/p/No00004B.html 这里,假设你已经能够看到这个画面了: 接下来,我们继续 给药 : 安装no ...

  9. jmeter(五)Sample之JDBC Request

    jmeter中取样器(Sampler)是与服务器进行交互的单元.一个取样器通常进行三部分的工作:向服务器发送请求,记录服务器的响应数据和记录相应时间信息 有时候工作中我们需要对数据库发起请求或者对数据 ...

  10. ThinkPHP常用配置路径

    //系统常量定义 //去THinkPHP手册中进行查找 echo "<br>"."网站的根目录地址".__ROOT__." "; ...