"这道题也太水了吧,模拟就行了!"
"数据范围..."
"好像不行呀"
"呜呜~~TLE!"
献上暴力代码!
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N], m;
signed main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
}
cin >> m;
while (m --) {
int l, r;
cin >> l >> r;
int sum = 0;
for (int i = l; i <= r; ++ i) {
sum += a[i];
}
cout << sum << endl;
}
return 0;
}
提交!
什么!
TTLLEE!
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
卡了这么多了
还TLE!
优化!!!!!!!!!!!!!!!!!!!
(前缀和)
由此可见: 区间和可以通过sum[r] - sum[l - 1]来快速访问
上代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N], m, sum[N];
signed main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
cin >> m;
while (m --) {
int l, r;
cin >> l >> r;
cout << sum[r] - sum[l - 1] << endl;
}
return 0;
}
A C !!!!!!!!!!!
标签:洛谷,cout,int,B3612,深进,cin,sync,tie,sum From: https://blog.csdn.net/2301_77879303/article/details/140847936