×
解除綁定,重新設定系統帳號的密碼
您的系統帳號 ID:
您的系統帳號:
您的帳號暱稱:
設定新密碼:
設定新密碼:
×
請輸入要加入的「課程代碼」
請向開設課程的使用者索取「課程代碼」
分類題庫
解題動態
排行榜
討論區
競賽區
登入
註冊
發表新討論
#54200: C++ 使用前綴和
hch980506@gmail.com
(申有娜我老婆)
學校:
國立臺南高級工業職業學校
編號:
294649
×
傳送站內訊息
傳給:
主題:
內容:
來源:
[36.236.252.39]
註冊時間:
2024-11-28 01:12:33
最後登入時間:
2025-12-10 14:34:08
e346.
區間和練習
| From: [210.70.138.14] | 發表日期: 2025-12-14 15:36
把cin,cout 改scanf,printf會快一些
#include <cstdio>
#include <vector>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> num(n);
vector<long long> pre(n);
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
if (i == 0) {
pre[0] = num[0];
continue;
}
pre[i] = pre[i - 1] + num[i];
}
int q;
scanf("%d", &q);
while (q--) {
int l, r;
scanf("%d %d", &l, &r);
long long total = 0;
if (l == 1)
total = pre[r - 1];
else
total = pre[r - 1] - pre[l - 2];
printf("%lld\n", total);
}
}