【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();
	}
};
相关推荐
yonuyeung1 分钟前
代码随想录算法【Day54】
java·数据结构·算法
Ljw...6 分钟前
DeepSeek+Kimi生成高质量PPT
数据库·c++·powerpoint·ppt·deepseek
敲上瘾7 分钟前
基础dp——动态规划
java·数据结构·c++·python·算法·线性回归·动态规划
西猫雷婶25 分钟前
python学智能算法(三)|模拟退火算法:深层分析
算法·机器学习·模拟退火算法
my_styles27 分钟前
2025-alibaba-Sentinel组件
java·开发语言·sentinel
禁默43 分钟前
C++之旅-C++11的深度剖析(1)
开发语言·c++
张有志_1 小时前
STL容器终极解剖:C++ vector源码级实现指南 | 从内存分配到异常安全的全流程避坑
c语言·c++·算法·开源·visual studio
挨代码1 小时前
UE_C++ —— Delegates
c++·ue
繁依Fanyi1 小时前
巧妙实现右键菜单功能,提升用户操作体验
开发语言·前端·javascript·vue.js·uni-app·harmonyos
程序员黄同学1 小时前
解释 Vue 中的虚拟 DOM,如何通过 Diff 算法最小化真实 DOM 更新次数?
开发语言·前端·javascript