【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 1≤x≤n) 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+1...an,a1,a2...ax−1. 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 1≤t≤104) --- 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 1≤n,q≤2⋅105) --- 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 1≤ai≤106).

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 1≤l≤r≤n2) --- 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 2⋅105 and the sum of q q q does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.

输出格式

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

解法

解题思路

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

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

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

代码

cpp 复制代码
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();
	}
};
相关推荐
程序员Xu13 分钟前
【OD机试题解法笔记】连续出牌数量
笔记·算法·深度优先
CoovallyAIHub26 分钟前
单目深度估计重大突破:无需标签,精度超越 SOTA!西湖大学团队提出多教师蒸馏新方案
深度学习·算法·计算机视觉
CoovallyAIHub29 分钟前
从FCOS3D到PGD:看深度估计如何快速搭建你的3D检测项目
深度学习·算法·计算机视觉
偷偷的卷1 小时前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
北京地铁1号线1 小时前
Zero-Shot(零样本学习),One-Shot(单样本学习),Few-Shot(少样本学习)概述
人工智能·算法·大模型
大白的编程日记.1 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
网小鱼的学习笔记1 小时前
python中MongoDB操作实践:查询文档、批量插入文档、更新文档、删除文档
开发语言·python·mongodb
C语言小火车1 小时前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针
Q_Q5110082851 小时前
python的保险业务管理与数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
亮1111 小时前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven