A 夹娃娃
题目地址:
基本思路:
这题比较简单,算是签到题。我们直接计算出前缀和然后每次的查询区间和就行了。
参考代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define IO std::ios::sync_with_stdio(false) #define ll long long #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, _) memset(s, _, sizeof(s)) #define pb push_back #define pii pair <int, int> #define mp(a, b) make_pair(a, b) #define INF (int)1e18 inline int read() { int x = 0, neg = 1; char op = getchar(); while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); } while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); } return neg * x; } inline void print(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } const int maxn = 1e5 + 10; int n,k,sum[maxn]; signed main() { IO; n = read(), k = read(); for (int i = 1; i <= n; ++i) { int x = read(); sum[i] = sum[i - 1] + x; } while (k--) { int l = read(), r = read(); int res = sum[r] - sum[l - 1]; print(res); puts(""); } return 0; }