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 ...
随机推荐
- android 内存溢出与内存泄露
内存溢出就是软件运行需要的内存,超出了java虚拟机给他分配的可用的最大内存 内存泄露就是在缓存图片文字等等的时候,没有关闭流所导致的内存泄露
- 在Asp.Net MVC中实现计算页面执行时间及简单流量统计
引用www.rsion.com.dll进您的asp.net MVC项目本人不才,源代码中有详细说明,查看demo修改HomeController public class HomeController ...
- Redhat Server 5.7 安装配置PHP
PHP的简介 PHP于1994年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf 为了要维护个人网页而制作的一个简单的用Perl语言编写的程序.这些工具程序用来显示 Rasm ...
- SSRS ReportServer Database 的Blocking问题
我们监控SQL SERVER数据库的阻塞情况时,老是收到在SSRS 里面出现SQL阻塞情况,刚开始由于事情多,没有太关注ReportServerTempDB里面的会话阻塞情况,但是老是出现这种频繁阻塞 ...
- WebAPi的可视化输出模式(RabbitMQ、消息补偿相关)——所有webapi似乎都缺失的一个功能
最近的工作我在做一个有关于消息发送和接受封装工作.大概流程是这样的,消息中间件是采用rabbitmq,为了保证消息的绝对无丢失,我们需要在发送和接受前对消息进行DB落地.在发送前我会先进行DB的插入, ...
- 监控mysql各种选项
安装mysql之后,需要对mysql服务进行监控. nagios开源自带的check_mysql 对 mysql 的slave 机监控倒是不错.但是对数据库主机监控就略显不足了. 使用一个监控 ...
- 搭建自己的PHP框架心得(三)
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- WPF Telerik TreeListView样式设计
Telerik控件 TreeListView 修改其中样式 1.添加TreeListView控件 <telerik:RadTreeView x:Name="ObjecTreeView& ...
- css 使容器宽度适应内容宽
p{ width:intrinsic; width: -moz-max-content; width: -webkit-max-content; float:right; width:auto; /* ...
- 报表软件JS开发引用HTML DOM的windows对象
HTML DOM是W3C标准(是HTML文档对象模型的英文缩写,Document Object Model for HTML). HTML DOM定义了用于HTML的一些列标准的对象,以及访问和处理H ...