hdu 1166 地兵布阵 ( 树状数组-初步) (以前 AC 的代码今天交超时!不知道何故!)
来源:互联网 发布:崇州行知中学 编辑:程序博客网 时间:2024/05/18 03:52
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>using namespace std;const int N = 50005;int n, T, k;int B[N];int Lowbit(int x) { return x & (-x);}void Insert(int i, int x) { while(i <= n) { B[i] += x; i = i + Lowbit(i); }} // 得到 Bnlong long GetSum(int i) { long long sum = 0; while(i > 0) { sum += B[i]; i = i - Lowbit(i); } return sum;}int main() { int e = 0, i; scanf("%d", &T); while(T--) { cout << "Case " << ++e << ":" << endl; scanf("%d", &n); memset(B, 0, sizeof(B)); for(i = 1; i <= n; i++) { scanf("%d", &k); Insert(i, k); } char str[10]; scanf("%s", str); while(strcmp(str, "End") != 0) { int x, y; scanf("%d%d", &x, &y); if(strcmp(str, "Query") == 0) { cout << GetSum(y) - GetSum(x-1) << endl; } else if(str[0] == 'A') { Insert(x, y); } else if(str[0] == 'S') { Insert(x, (-y)); } scanf("%s", str); } } return 0;}