Submission #69371186


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

/////////////////// メイン ///////////////////

int main () {
  
  /////////////////// 前入力 ///////////////////
  
  int n, q;
  cin >> n >> q;

  vector<long long> a(n);
  for (int i=0; i<n; i++) {
    cin >> a.at(i);
  }

  /////////////////// 前処理 ///////////////////

  // 普通の累積和
  vector<long long> b0(n+1);
  partial_sum(a.begin(),a.end(),b0.begin()+1);

  // i倍の累積和
  for (int i=0; i<n; i++) {
    a.at(i) *= i;
  }
  vector<long long> b1(n+1);
  partial_sum(a.begin(),a.end(),b1.begin()+1);

  // i^2倍の累積和
  for (int i=0; i<n; i++) {
    a.at(i) *= i;
  }
  vector<long long> b2(n+1);
  partial_sum(a.begin(),a.end(),b2.begin()+1);

  /////////////////// ループ ///////////////////

  for (int loop=0; loop<q; loop++) {
    
    //////////////////// 入力 ////////////////////

    int l, r;
    cin >> l >> r;
    l--;

    //////////////// 出力変数定義 ////////////////

    long long result = 0;

    //////////////////// 処理 ////////////////////

    // Σ a[i]*(r-i)*{i-(l-1)} = Σ a[i]*{(-1)*i^2+(l+r-1)*i-r*(l-1)} を求める

    // 2次の部分
    result -= b2.at(r)-b2.at(l);

    // 1次の部分
    result += (b1.at(r)-b1.at(l))*(l+r-1);

    // 0次の部分
    result -= (b0.at(r)-b0.at(l))*r*(l-1);

    //////////////////// 出力 ////////////////////

    cout << result << endl;
    
  }

  /////////////////// 後処理 ///////////////////



  //////////////////// 終了 ////////////////////

  return 0;

}

Submission Info

Submission Time
Task E - Sum of Subarrays
User wightou
Language C++ 23 (gcc 12.2)
Score 475
Code Size 1624 Byte
Status AC
Exec Time 475 ms
Memory 15448 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 1
AC × 16
Set Name Test Cases
Sample sample00.txt
All sample00.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt
Case Name Status Exec Time Memory
sample00.txt AC 1 ms 3520 KiB
testcase00.txt AC 1 ms 3484 KiB
testcase01.txt AC 343 ms 3624 KiB
testcase02.txt AC 373 ms 5584 KiB
testcase03.txt AC 244 ms 5012 KiB
testcase04.txt AC 212 ms 8652 KiB
testcase05.txt AC 187 ms 11112 KiB
testcase06.txt AC 103 ms 4572 KiB
testcase07.txt AC 343 ms 5708 KiB
testcase08.txt AC 471 ms 14820 KiB
testcase09.txt AC 475 ms 14820 KiB
testcase10.txt AC 475 ms 14832 KiB
testcase11.txt AC 473 ms 14816 KiB
testcase12.txt AC 471 ms 14820 KiB
testcase13.txt AC 474 ms 14820 KiB
testcase14.txt AC 417 ms 15448 KiB