Range Add Query
题目描述:
You are given an integer sequence of length
For each
Here, a sequence of length
- Choose an integer
such that and an integer (possibly negative). Add to each of the elements .
It is guaranteed that
- All values in the input are integers.
输入描述:
输出描述:
Print
样例1:
input:
7 3
3 -1 1 -2 2 0 5
2
1 6
2 7
output:
Yes
No
The sequence
-
First, do the operation with
, making . -
Next, do the operation with
, making . -
Finally, do the operation with
, making .
Thus, the first line should contain Yes.
On the other hand, for the sequence
样例2:
input:
20 4
-19 -66 -99 16 18 33 32 28 26 11 12 0 -16 4 21 21 37 17 55 -19
5
13 16
4 11
3 12
13 18
4 10
output:
No
Yes
No
Yes
No
AC代码:
#include <bits/stdc++.h> using namespace std; // 这个题可以通过一般形式来找规律 // 假设K = 3,有七个数A, B, C, D, E, F, G // 假设N = 3,因为要让所有数都变为0,所以可以先让A变为0 // 所以操作后就变成0,B - A, C - A, 若是一个good数组,则此时B - A = 0 = C - A,得A = B = C // 假设N = 4,一次操作后为0, B - A, C - A, D,第二次操作后为0, 0, C - B, A + D - B // 此时可得C - B = 0 = A + D - B,即A + D = B = C // 同理可得N = 5时,A + D = C = B + E // 通过观察不难发现只要对于N个数,其不同下标i对K取模相等的数的和相同,那么就可以是一个good数组 // 或者不用找规律的方式来想 // 依旧是从前往后依次操作,如果是good数组,那么最后肯定会全是0,否则最后肯定会有某些数不是0 // 可以发现每次操作都会将下标对K取模为0 ~ K - 1这K个数大小同时改变,且改变的数值大小相同 // 那么也就是说不同下标i对K取模相等的数的和的大小也是同时发生改变,且改变数值大小相同 // 所以在最后一次操作的过程中,如果是good数组,那么最后所有数都变成0,和也变成0 // 如果最后某些数不是0,那么这些数的坐标i对K取模相等的数的和也不会是0 // 那么根据第16行分析可知,只有可能是因为和的大小和其他和的大小不同导致的 // 所有可得必须是下标i对K取模相等的数的和全相等,才会是good数组 int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, k, q; cin >> n >> k; vector<int> a(n); for(int i = 0; i < n; i ++) { cin >> a[i]; // 运用前缀和,将对K取模相等的加起来 if(i >= k) a[i] += a[i - k]; } cin >> q; while(q --) { int l, r; cin >> l >> r; l --, r --; vector<int> b(k); for(int i = r; r - i + 1 <= k; i --) b[i % k] = a[i]; // 减去区间之外的 for(int i = l - 1; i >= 0 && l - i <= k; i --) b[i % k] -= a[i]; // 只有k个数,且这k个数都(与b[0])相等 if(b == vector<int> (k, b[0])) cout << "Yes\n"; else cout << "No\n"; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】