当前位置: 首页 > news >正文

【Codeforces】CF 2009 F

Firefly’s Queries

#前缀和 #数据结构 #数学

题目描述

Firefly is given an array a a a of length n n n. Let c i c_i ci denote the i i i’th cyclic shift ∗ ^{\text{∗}} of a a a. She creates a new array b b b such that b = c 1 + c 2 + ⋯ + c n b = c_1 + c_2 + \dots + c_n b=c1+c2++cn where + + + represents concatenation † ^{\text{†}} .

Then, she asks you q q q queries. For each query, output the sum of all elements in the subarray of b b b that starts from the l l l-th element and ends at the r r r-th element, inclusive of both ends.

∗ ^{\text{∗}} The x x x-th ( 1 ≤ x ≤ n 1 \leq x \leq n 1xn) cyclic shift of the array a a a is a x , a x + 1 … a n , a 1 , a 2 … a x − 1 a_x, a_{x+1} \ldots a_n, a_1, a_2 \ldots a_{x - 1} ax,ax+1an,a1,a2ax1. Note that the 1 1 1-st shift is the initial a a a.

† ^{\text{†}} The concatenation of two arrays p p p and q q q of length n n n (in other words, p + q p + q p+q) is p 1 , p 2 , . . . , p n , q 1 , q 2 , . . . , q n p_1, p_2, ..., p_n, q_1, q_2, ..., q_n p1,p2,...,pn,q1,q2,...,qn.

输入格式

The first line contains t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n and q q q ( 1 ≤ n , q ≤ 2 ⋅ 1 0 5 1 \leq n, q \leq 2 \cdot 10^5 1n,q2105) — the length of the array and the number of queries.

The following line contains n n n integers a 1 , a 2 , . . . , a n a_1, a_2, ..., a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 6 1 \leq a_i \leq 10^6 1ai106).

The following q q q lines contain two integers l l l and r r r ( 1 ≤ l ≤ r ≤ n 2 1 \leq l \leq r \leq n^2 1lrn2) — the left and right bounds of the query.

Note that the large inputs may require the use of 64-bit integers.

It is guaranteed that the sum of n n n does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105 and the sum of q q q does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出格式

For each query, output the answer on a new line.

样例 #1

样例输入 #1

5
3 3
1 2 3
1 9
3 5
8 8
5 5
4 8 3 2 4
1 14
3 7
7 10
2 11
1 25
1 1
6
1 1
5 7
3 1 6 10 4
3 21
6 17
2 2
1 5
1 14
9 15
12 13
5 3
4 9 10 10 1
20 25
3 11
20 22

样例输出 #1

18
8
1
55
20
13
41
105
6
96
62
1
24
71
31
14
44
65
15

解法

解题思路

可以发现,每次移动会改变相对位置,但是不会改变大小之和,而相对位置其实就是一个环。

我们可以把数组复制一遍变成环,化环为链,计算一遍前缀和。

而每次询问相当于询问固定的几个环,加上一个不完整的环,这个不完整的环就可以使用之前计算的前缀和来完成。

代码

void solve() {int n,q;cin >> n >> q;vector<int>a(n + 1), prefix(2 * n + 1);for (int i = 1; i <= n; ++i) {cin >> a[i];prefix[i] = prefix[i - 1] + a[i];}int s = prefix[n];for (int i = 1; i <= n; ++i) {prefix[n + i] = prefix[n + i - 1] + a[i];}auto cal = [&](int r)->int{int len = r / n, x = r % n;int L = len + 1, R = L + x - 1;int sum = len * s + prefix[R] - prefix[L - 1];return sum;};while (q--) {int l, r;cin >> l >> r;int res = cal(r);res -= cal(l-1);std::cout << res << "\n";}
}signed main() {ios::sync_with_stdio(0);std::cin.tie(0);std::cout.tie(0);int t = 1;cin >> t;while (t--) {solve();}
};
http://www.lryc.cn/news/463526.html

相关文章:

  • GTP4聊天记录中letax保存为word
  • vscode调试编译找不到gcc,只有cl,但是检查cmd是对的,控制面板的路径也更改了
  • 空间解析几何5-空间圆到平面的距离【附MATLAB代码】
  • [已解决] pycharm添加本地conda虚拟环境 + 配置解释器 - pycharm找不到conda可执行文件
  • SENT - Single Edge Nibble Transmission for Automotive
  • 2024年软件设计师中级(软考中级)详细笔记【7】面向对象技术(下)23种设计模式(分值10+)
  • 未来人工智能的发展对就业市场的影响 人工智能在生活中的相关
  • Oracle EBS 中财务模块
  • 基于SSM公廉租房维保系统的设计
  • 【AI大模型】深入Transformer架构:解码器部分的实现与解析
  • 前端html js css 基础巩固3
  • 如在下载自己的需要的rmp包呢
  • Android TextView实现一串文字特定几个字改变颜色
  • 桃子叶片病害分类检测数据集(猫脸码客 第221期)
  • Vue--》掌握自定义依赖引入的最佳实践
  • repo 命令大全详解(第十四篇 repo overview)
  • 【设计模式】深入理解Python中的抽象工厂设计模式
  • 网站建设完成后,多久需要升级迭代一次
  • 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字
  • Vue基本学习2
  • 创作者等级权益说明
  • 基于SpringBoot+Vue+uniapp微信小程序的校园反诈骗微信小程序的详细设计和实现(源码+lw+部署文档+讲解等)
  • 统一修改UI库样式的几种方式
  • ICM20948 DMP代码详解(88)
  • 字节跳动实习生投毒自家大模型细节曝光 影响到底有多大?
  • 【路径规划】蚁群算法优化bp神经网络回归预测
  • 如何在OceanBase中新增系统变量及应用实践
  • Olap数据处理
  • Tailwind Starter Kit 一款极简的前端快速启动模板
  • 物联网智能家居环境监测系统