【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();
	}
};
相关推荐
Max_uuc2 分钟前
【C++ 硬核】打破嵌入式 STL 禁忌:利用 std::pmr 在“栈”上运行 std::vector
开发语言·jvm·c++
吃杠碰小鸡2 分钟前
高中数学-数列-导数证明
前端·数学·算法
故事不长丨3 分钟前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
long3163 分钟前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
近津薪荼4 分钟前
dfs专题4——二叉树的深搜(验证二叉搜索树)
c++·学习·算法·深度优先
牵牛老人5 分钟前
【Qt 开发后台服务避坑指南:从库存管理系统开发出现的问题来看后台开发常见问题与解决方案】
开发语言·qt·系统架构
熊文豪13 分钟前
探索CANN ops-nn:高性能哈希算子技术解读
算法·哈希算法·cann
froginwe1114 分钟前
Python3与MySQL的连接:使用mysql-connector
开发语言
熊猫_豆豆30 分钟前
YOLOP车道检测
人工智能·python·算法
灵感菇_32 分钟前
Java HashMap全面解析
java·开发语言