#ifdef unix
#define LLD "%lld"
#else
#define LLD "%I64d"
#endif
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#define LL long long
#define maxn 100000
#define INF 1 << 30
#define For(i, n) for (int i = 1; i <= n; i++)
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
int n, m, a[maxn];
struct BIT {
int lowbit(int x) {return x & -x;}
void modify(int x, int data) {while (x <= n) a[x] += data, x += lowbit(x);}
int sum(int x) {
int tot = 0;
while (x > 0) tot+=a[x], x -= lowbit(x);
return tot;
}
}bit;
int main() {
int x, y;
memset(a, 0, sizeof(a));
scanf("%d%d", &n, &m);
For(i, n) {
scanf("%d", &x);
bit.modify(i, x);
}
For(i, m) {
scanf("%d%d", &x, &y);
printf("%d\n", bit.sum(y) - bit.sum(x - 1));
}
return 0;
}
BIT树状数组
最新推荐文章于 2023-09-18 22:23:18 发布