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

【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.kler.cn/news/357217.html

相关文章:

  • 【云原生】Docker 部署 Nacos使用详解
  • DFS算法经典题目: Leetcode 51.N皇后
  • 工厂生成中关于WiFi的一些问题
  • 026 elasticsearch文档管理(添加、修改、删除、批处理)-Java原生客户端
  • 力扣1011.在D天内送达包裹的能力
  • Day13-数据库服务架构集群
  • 零基础入门人工智能,如何利用AI工具提升你的学习效率?
  • 父母教养方式测试:理解与优化家庭教育的关键
  • 基于Matlab车牌识别课程设计报告
  • C#里使用PerformLayout,强制控件将布局逻辑应用于其所有子控件。
  • pandas-使用技巧
  • 用Aconvert.com将MOBI文件转换为PDF:一步步指南
  • Mysql中表字段VARCHAR(N)类型及长度的解释
  • void关键字
  • ubuntu 20.04 网卡启用后,只有ipv6 没有 ipv4 无法上网
  • Linux_c 有名管道练习
  • Facebook Marketplace为什么无法使用的原因?
  • 树莓派刷入OpenWrt后扩容overlay的方法
  • 诺贝尔物理学奖与机器学习、神经网络:一场跨时代的融合与展望
  • Spring容器详解:BeanFactory和ApplicationContext的不同