1 条评论

  • @ 2024-3-25 20:01:09

    前缀和

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int N = 100010;
    
    int a[N], pre[N];
    
    int main(){
    	int n;
    	cin >> n;
    	for(int i = 1; i <= n; i ++ ){
    		cin >> a[i];
    		// 前缀和数组 
    		pre[i] = pre[i - 1] + a[i];
    	}
    	int m;
    	cin >> m;
    	while(m -- ){
    		int l, r;
    		cin >> l >> r;
    		cout << pre[r] - pre[l - 1] << '\n'; 
    	}
    	return 0;
    }
    
    • 1