[ABC425C] Rotate and Sum Query 题解

思路

对于操作二,多次求 $ \displaystyle \sum_{i=l}^r A_i $,不难想到前缀和。所以先记录 \(A\) 的前缀和。

对于操作一,我们不用按题意模拟,直接记录一共偏移了多少。以后出现操作二时直接根据偏移量输出偏移前 \(l\)\(r\) 的和就可以了。

有些细节见代码注释。

AC code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[400005];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int n,T;
	cin>>n>>T;
	for(int i=1;i<=n;i++) cin>>a[i],a[i+n]=a[i];
	for(int i=1;i<=2*n;i++) a[i]+=a[i-1];//注意!!!这里为了解决求偏移前的l~n和1~r的情况。
	int p=0;//偏移量
	while(T--){
		int op;
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			p=(p+x)%n;//模n防止偏移量>=n
		}
		else{
			int l,r;
			cin>>l>>r;
			cout<<a[r+p]-a[l-1+p]<<"\n";
		}
	}
	return 0;
}
posted @ 2025-09-28 21:05  Frums  阅读(21)  评论(0)    收藏  举报