敌兵布阵 HDU - 1166
多组输入,注意清除tr数组
维护一个前缀数组,耗时有点大
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 5e4 + 5;
int t, n;
int sum[maxn], num[maxn];
char str[10];
int main() {
// freopen("in.txt", "r", stdin);
scanf("%d", &t);
for (int kase = 1; kase <= t; kase++) {
memset(sum, 0, sizeof(sum));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", num + i);
sum[i] = sum[i - 1] + num[i];
}
int a, b;
printf("Case %d:\n", kase);
while (scanf("%s", str) && str[0] != 'E') {
scanf("%d%d", &a, &b);
if (str[0] == 'Q') {
int res = sum[b] - sum[a - 1];
printf("%d\n", res);
continue;
}
else if (str[0] == 'A') {
for (int i = a; i <= n; i++) {
sum[i] += b;
}
}
else if (str[0] == 'S') {
for (int i = a; i <= n; i++) {
sum[i] -= b;
}
}
}
}
return 0;
}
树状数组模板
#include<bits/stdc++.h>
using namespace std;
#define sc(n) scanf("%c",&n)
#define sd(n) scanf("%d",&n)
#define pd(n) printf("%d\n", (n))
#define sdd(n,m) scanf("%d %d",&n,&m)
#define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z)
#define pdd(n,m) printf("%d %d\n",n, m)
#define ms(a,b) memset(a,b,sizeof(a))
#define mod(x) ((x)%MOD)
#define lowbit(x) (x & (-x))
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
const int MOD = 10000007;
const int inf = 0x3f3f3f3f;
const int maxn = 5e5 + 100;
int n, tr[maxn], t;
void update(int x, int val) {
while (x <= n) {
tr[x] += val;
x += lowbit(x);
}
}
int getsum(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
//freopen("in.txt", "r", stdin);
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int N; cin >> N;
for (int K = 1; K <= N; ++K) {
ms(tr, 0);
cin >> n;
for (int i = 1; i <= n; ++i)cin >> t, update(i, t);
printf("Case %d:\n", K);
string s; int a, b;
while (cin >> s && s != "End") {
cin >> a >> b;
if (s == "Query")cout << getsum(b) - getsum(a - 1) << endl;
if (s == "Add") update(a, b);
if (s == "Sub") update(a, -b);
}
}
return 0;
}
线段树
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int sum[maxn * 4], A[maxn];
void PushUp(int rt) {
sum[rt] = sum[rt * 2] + sum[rt * 2 + 1];
}
void Build(int l, int r, int rt) {
if (l == r) {
sum[rt] = A[l];
return;
}
int m = (l + r) / 2;
Build(l, m, rt * 2);
Build(m + 1, r, rt * 2 + 1);
PushUp(rt);
}
void Update(int L, int c, int l, int r, int rt) {
if (r == l) {
sum[rt] += c;
return;
}
int m = (l + r) / 2;
if (L <= m) Update(L, c, l, m, rt * 2);
else Update(L, c, m + 1, r, rt * 2 + 1);
PushUp(rt);
}
int Query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return sum[rt];
}
int m = (l + r) / 2;
int ans = 0;
if (L <= m) ans += Query(L, R, l, m, rt * 2);
if (R > m) ans += Query(L, R, m + 1, r, rt * 2 + 1);
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
char c;
int T;
int n, m, a, b;
char str[10];
scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
printf("Case %d:\n", kase);
scanf("%d", &n);
memset(sum, 0, sizeof(sum));
memset(A, 0, sizeof(A));
for (int i = 1; i <= n; i++) scanf("%d", A + i);
Build(1, n, 1);
while (scanf("%s", str) && str[0] != 'E') {
scanf("%d%d", &a, &b);
if (str[0] == 'Q') {
printf("%d\n", Query(a, b, 1, n, 1));
}
else if (str[0] == 'A') {
Update(a, b, 1, n, 1);
}
else if (str[0] == 'S') {
Update(a, -b, 1, n, 1);
}
}
}
return 0;
}