hdu 1166 赤裸裸的树状数组

题目没啥说的,赤裸裸的树状数组

/*
* hdu1166/win.cpp
* Created on: 2011-9-6
* Author : ben
*/
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
using namespace std;

const int MAXN = 50100;
int N, M;
int array[MAXN];

inline
int lowbit(int x) {
return x & (x ^ (x - 1));
}

int sum(int n) {
int ret = 0;
for(int i = n; i > 0; i -= lowbit(i)) {
ret
+= array[i];
}
return ret;
}

void update(int index, int value) {
for (int i = index; i <= N; i += lowbit(i)) {
array[i]
+= value;
}
}

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

void work() {
char op[20];
int a, b, T, temp;
scanf(
"%d", &T);
for (int t = 1; t <= T; t++) {
printf(
"Case %d:\n", t);
scanf(
"%d", &N);
memset(array,
0, sizeof(array));
for (int i = 1; i <= N; i++) {
scanf(
"%d", &temp);
update(i, temp);
}
while (scanf("%s", op) == 1) {
if (strcmp(op, "End") == 0) {
break;
}
scanf(
"%d%d", &a, &b);
if (strcmp(op, "Query") == 0) {
printf(
"%d\n", sum(b) - sum(a - 1));
}
else if (strcmp(op, "Add") == 0) {
update(a, b);
}
else {
update(a,
-b);
}
}
}
}
原文地址:https://www.cnblogs.com/moonbay/p/2168484.html