首页 > 其他分享 >luoguP8218 【深进1.例1】求区间和

luoguP8218 【深进1.例1】求区间和

时间:2024-07-21 08:57:49浏览次数:7  
标签:__ 10 le ch 深进 luoguP8218 endif 区间 include

【深进1.例1】求区间和

题目描述

给定 $n$ 个正整数组成的数列 $a_1, a_2, \cdots, a_n$ 和 $m$ 个区间 $[l_i,r_i]$,分别求这 $m$ 个区间的区间和。

对于所有测试数据,$n,m\le10^5,a_i\le 10^4$

输入格式

第一行,为一个正整数 $n$ 。

第二行,为 $n$ 个正整数 $a_1,a_2, \cdots ,a_n$

第三行,为一个正整数 $m$ 。

接下来 $m$ 行,每行为两个正整数 $l_i,r_i$ ,满足$1\le l_i\le r_i\le n$

输出格式

共 $m$ 行。

第 $i$ 行为第 $i$ 组答案的询问。

样例 #1

样例输入 #1

4
4 3 2 1
2
1 4
2 3

样例输出 #1

10
5

提示

样例解释:第 $1$ 到第 $4$ 个数加起来和为 $10$。第 $2$ 个数到第 $3$ 个数加起来和为 $5$。

对于 $50 %$ 的数据:$n,m\le 1000$;

对于 $100 %$ 的数据:$1 \le n, m\le 10^5$,$1 \le a_i\le 10^4$

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#if __cplusplus >= 201703L
#include <any>
#include <charconv>
// #include <execution>
#include <filesystem>
#include <optional>
#include <memory_resource>
#include <string_view>
#include <variant>
#endif

#if __cplusplus > 201703L
#include <barrier>
#include <bit>
#include <compare>
#include <concepts>
#if __cpp_impl_coroutine
# include <coroutine>
#endif
#include <latch>
#include <numbers>
#include <ranges>
#include <span>
#include <stop_token>
#include <semaphore>
#include <source_location>
#include <syncstream>
#include <version>
#endif
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x*f;
}
int n, m, a[100050], s[100050];
int main() {
	n = read();
	for (int i = 1; i <= n; i++)
		s[i] = s[i - 1] + (a[i] = read());
	m = read();
	for (int i = 1; i <= m; i++) {
		int l = read(), r = read();
		cout << s[r] - s[l - 1] << endl;
	}
	return 0;
}

标签:__,10,le,ch,深进,luoguP8218,endif,区间,include
From: https://www.cnblogs.com/mcr130102/p/18314149

相关文章

  • 如何计算给定区间内c-vine copula的联合分布函数的概率?
    我已经使用6-dimensional库和Python计算了pyvinecopulib中的联合分布函数,并将其存储在变量cop中。如何计算numpy这样的区间上的概率?(0.5<F1(X1)<0.6,0.5<F2(X2)<0.6,0.5<F3(X3)<0.6,0.5<F4(X4)<0.6,0.5<F5(X......
  • 代码随想录算法训练营第33天 | 贪心4:452. 用最少数量的箭引爆气球、435. 无重叠区间
    代码随想录算法训练营第33天|贪心4:452.用最少数量的箭引爆气球、435.无重叠区间、763.划分字母区间452.用最少数量的箭引爆气球https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/description/代码随想录https://programmercarl.com/0452.用最......
  • 代码随想录day 31 用最少数量的箭引爆气球 | 无重叠区间 | 划分字母区间
    用最少数量的箭引爆气球用最少数量的箭引爆气球解题思路先根据数组中的第一个参数进行排序,之后通过记录最小右区间来判断是否重叠或者进入下个重叠区。贪心的思想是有重叠就尽可能地进行重叠,从而达到局部最优知识点重叠区间,贪心心得学会了如何判断和找寻重叠区间的方法无......
  • 线段树(原理、构造和区间查询,例题:Balanced Lineup)
    概念原理    线段树是分治法和二叉树的结合,二叉树上的节点都是根据分治得到的。节点所表示的,也就是线段,可以是区间和、最值或者是其他的,,每次分治,左右子树各一半,每个节点的值代表了以它为根的子树上所有节点的值。通过线段树,大区间的解可以从小区间的解合并而来。构......
  • P3373 【模板】线段树 2(区间乘+加操作,先乘后加原则)
    题目来源:https://www.luogu.com.cn/problem/P3373//题意:对区间[l,r]可以乘法,加法操作,查询和操作。//思路:既有乘法又有加法,肯定是要有两个标记。纯加法和纯乘法操作是很简单的,但是既有乘法又有加法涉及到先乘后加和先加后乘的顺序。//////所以现在是如何将先加后成也可以......
  • 时间区间按月计算使用天数
    selectt3.*,casewhenday_start>month_currentandday_end<month_lastthenTIMESTAMPDIFF(DAY,day_start,day_end)+1--'中间,结束时间-开始时间'whenday_start>month_currentthenTIMESTAMPDIFF(DAY,day_start,month_......
  • 题解:P10724 [GESP202406 七级] 区间乘积
    思路看到\(a_i\)很小,不难想到状压一类的东西。考虑把每个数的质因数当做二进制位,这个二进制位的\(1/0\)代表含有这个质因数的奇偶,再做一个异或前缀和,显然完全平方数的质因子个数一定为偶数,根据异或的性质,两个相同的数异或才为\(0\)所以只需要找到异或前缀和中相同的数的个......
  • 将一串数字中的连续数字写成区间
    Excel某单元格有排序好的整数序列组成的字符串,但序列有空缺,导致不连续:1,2,4,6,7,8,9,13,14,15,16,17,18,25,26,27,28,29,30,31,32要求找出每个连续的序列,用短横写成区间的形式,区间之间用逗号分隔。1-2,4,6-9,13-18,25-32使用SPLXLL=spl("=?.split@pc().group@i(~!=~[-1......
  • 题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]
    CodeForces835DD.Palindromiccharacteristicstimelimitpertest:3secondsmemorylimitpertest:256megabytes*inputstandardinputoutputstandardoutputPalindromiccharacteristicsofstring\(s\)withlength\(|s|\)isasequenceof\(|s|\)in......
  • 用API实现商品sku抓取字段展示-淘宝sku区间价展示逻辑和规则分析
    有卖家问我:我的链接里面有5个sku,都是不同的价格,为什么消费者看到的不是最低价呢?这是因为淘宝平台商品价格的展示规则发生了变化,存在SKU区间价的产品,现在在搜索结果页面的曝光已经不是默认显示最低sku价了。现在平台展示逻辑主要有3点:①平台会结合着消费者的千人千面进行不......