BIT树状数组
模板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifdef unix 2 #define LLD "%lld" 3 #else 4 #define LLD "%I64d" 5 #endif 6 #include <cstdio> 7 #include <cstring> 8 #include <cstdlib> 9 #include <cmath> 10 #include <algorithm> 11 #include <iostream> 12 #include <vector> 13 #include <stack> 14 #include <queue> 15 #include <map> 16 #define LL long long 17 #define maxn 100000 18 #define INF 1 << 30 19 #define For(i, n) for (int i = 1; i <= n; i++) 20 #define rep(i, j, k) for (int i = j; i <= k; i++) 21 #define lson l, mid, rt << 1 22 #define rson mid + 1, r, rt << 1 | 1 23 using namespace std; 24 int n, m, a[maxn]; 25 struct BIT { 26 int lowbit(int x) {return x & -x;} 27 void modify(int x, int data) {while (x <= n) a[x] += data, x += lowbit(x);} 28 int sum(int x) { 29 int tot = 0; 30 while (x > 0) tot+=a[x], x -= lowbit(x); 31 return tot; 32 } 33 }bit; 34 int main() { 35 int x, y; 36 memset(a, 0, sizeof(a)); 37 scanf("%d%d", &n, &m); 38 For(i, n) { 39 scanf("%d", &x); 40 bit.modify(i, x); 41 } 42 For(i, m) { 43 scanf("%d%d", &x, &y); 44 printf("%d\n", bit.sum(y) - bit.sum(x - 1)); 45 } 46 return 0; 47 }
相关题目
1、 BZOJ 2743: [HEOI2012]采花
题目:http://61.187.179.132/JudgeOnline/problem.php?id=2743
题解,代码:http://www.cnblogs.com/joker0429/archive/2013/02/10/2909874.html
I come, I see, I conquer!