AOJ DSL_2_E Range Add Query (RAQ)
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≤s≤t≤n
- 1≤i≤n
- 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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- leetcode笔记:Range Sum Query - Mutable
一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- [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 ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [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 ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- range for query
static void range_test(Args _args) { Query Query; QueryRun QueryRun ...
随机推荐
- Linux(Centos)系统上搭建SVN以及常见错误解答
本文主要介绍怎样在Centos上搭建SVN,文章内容比较基础,适合小白用户学习. 1.Linux版本Centos 6.5 查看linux版本命令: cat /etc/issue 2.查看本机上是否已经 ...
- SQL Server 2000:快速清除日志文件的方法
通过文章 SQL Server中“数据收缩”详解 和 SQLServer删除log文件和清空日志的方法 可以整理出一种快速删除数据库日志的方法,即 第一步:清空日志文件里的数据: 第二步:收缩日志文件 ...
- Centos 安装jdk1.8
我是根据右边链接进行安装的 ,但是第一步不同噢.http://www.cnblogs.com/spiders/archive/2016/09/06/5845727.html 1.下载rpm安装文件. ...
- laravel5源码讲解整理
来源:http://yuez.me/laravel-yuan-ma-jie-du/?utm_source=tuicool&utm_medium=referral 目录 入口文件 index.p ...
- 无需FQ,自建本地CDN,秒上StackOverFlow!
StackOverflow是一个面向程序员的技术问答平台.可是在不FQ的情况下,浏览StackOverflow是一件让人极不舒服的事情,常常需要等待数十秒页面才慢慢显示出来.本文我教大家一种能够流畅地 ...
- 基于Simple Image Statistics(简单图像统计,SIS)的图像二值化算法。
这是个简单的算法,是全局二值算法的一种,算法执行速度快. 算法过程简单描述如下: 对于每一个像素,做如下处理 1.计算当前像素水平和垂直方向的梯度. (two gradients are calcul ...
- ArrayList 保证多线程安全
一:使用synchronized关键字 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List<Map<String,Obje ...
- Winform listview控件、 容器控件
1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设置行和列之间是否显示网格 ...
- [MVC]如何删除文章内容中的图片
1.实现代码 if (!string.IsNullOrWhiteSpace(entity.Content)) { var immgList = TextHelper.GetImgUrlList(ent ...
- 除非Microsoft FTP 服务(FTPSVC)正在运行,否则无法启动FTP站点。服务目前已停止
ftp站点就建成了,试下启动,右击站点,"管理ftp站点"-"启动".如果启动不了,出现“除非Microsoft FTP 服务(FTPSVC)正在运行,否则无法 ...