您的位置:首页 > 其它

hdu 1166 赤裸裸的树状数组

2011-09-06 10:54 381 查看
题目没啥说的,赤裸裸的树状数组

/*
* 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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: