5 条题解

  • 1
    @ 2024-4-6 17:28:28
    n = int(input())
    ls = [0]+ list(map(int, input().split()))
    sumls = [0]*(n+1)
    for i in range(1,n+1):
        sumls[i] = sumls[i-1]+ls[i]
    
    k = int(input())
    for i in range(k):
        l,r = map(int, input().split())
        print(sumls[r]-sumls[l-1])
    
    • 0
      @ 2024-5-25 17:16:04
      from itertools import accumulate
      
      n = int(input())
      arr = list(map(int, input().split()))
      k = int(input())
      
      S = [0] + list(accumulate(arr))
      
      for _ in range(k):
          l, r = map(int, input().split())
          print(S[r] - S[l - 1])
      
      
      • 0
        @ 2024-4-9 23:47:34
        #include<bits/stdc++.h>
        using namespace std;
        
        const int N=1e5+10;
        int n,q;
        int arr[N];
        int prefinx[N];
        int main()
        {
        	cin>>n;
        	for(int i=1;i<=n;i++){
        		cin>>arr[i];
        	}
        	for(int i=1;i<=n;i++){
        		prefinx[i]=prefinx[i-1]+arr[i]; 
        	}
        	cin>>q;
        	while(q--){
        		int l,r;
        		cin>>l>>r;
        		cout<<prefinx[r]-prefinx[l-1]<<endl;
        	}
        	return 0;	
        } 
        
        • 0
          @ 2024-4-8 22:01:52

          帮忙看看有什么可以优化嘛👀️

          //用全能头
          #include <bits/stdc++.h>
          using namespace std;
          
          int main()
          {
              //输入数组长度n,cir再赋一遍
              int n, cir;
              cin >> n;
              cir = n;
              //初始化数组number、sum还有累计和add
              //最后前缀和减需要i-1位所以index0拿来做被减头
              int number[n] = {0};
              int sum[n] = {0};
              sum[0] = 0;
              int add = 0;
              //循环输入数组,注意sum从第二开始赋值
              while(cir)
              {
                  cin >> number[n-cir];
                  add += number[n-cir];
                  sum[n-cir+1] = add;
                  cir--;
              }
              //输入多少组测试times
              int times = 0;
              cin >> times;
              //输入左右索引l,r
              int l, r;
              //运算后按格式输出
              while(times)
              {
                  cin >> l;
                  cin >> r;
                  cout << sum[r] - sum[l-1] << endl;
                  times--;
              }
          }
          
          • 0
            @ 2024-4-7 20:16:38
            #include<bits/stdc++.h>
            using namespace std;
            const int N = 1e5+10;
            int n,x;
            int a[N],sum[N];
            
            int main(){
            	cin.tie(0);
            	cout.tie(0);
            	
            	
            	cin>>n;
            	for(int i=1; i<=n; i++){
            		cin>>a[i]; 
            	}
            	
            	sum[1] = a[1];
            	for(int i=2; i<=n; i++){
            		sum[i]=a[i]+sum[i-1];
            	}
            	
            	cin>>x;
            	while(x--){
            		int l,r;
            		cin>>l>>r;
            		cout<<sum[r]-sum[l-1]<<endl;
            	}
            	
            	return 0;
            }
            
            • 1

            信息

            ID
            87
            时间
            1000ms
            内存
            256MiB
            难度
            4
            标签
            递交数
            307
            已通过
            139
            上传者