1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Case 1: 6 3359
树状数组直接应用即可。
#include<iostream> #include<algorithm> #include<math.h> #include<cstdio> #include<cstring> #include<string> using namespace std; const int maxn = 100005; const int low(int x){ return (x&-x); } int a[maxn], n, t, m, i, j; string s; void add(int x,int y) { for (int i = x; i <= n; i += low(i)) a[i] += y; } long long sum(int x) { long long total = 0; for (int i = x; i > 0; i -= low(i)) total += a[i]; return total; } int main(){ while (~scanf("%d", &t)) { int p = 0; while (++p <= t) { cout << "Case " << p << ":" << endl; cin >> n; memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++){ scanf("%d", &m); add(i, m); } while (1) { cin >> s; if (s == "End") break; else { scanf("%d%d", &i, &j); if (s == "Add") add(i, j); if (s == "Sub") add(i, -j); if (s == "Query") cout << sum(j) - sum(i - 1) << endl; } } } } return 0; }
zkw线段树
#include<iostream> #include<algorithm> #include<math.h> #include<cstdio> #include<cstring> #include<string> using namespace std; const int maxn = 100005; int a[2 * maxn], n, t, m, i, j, M; string s; void add(int x, int y) { x = x + M; a[x] += y; for (x >>= 1; x; x >>= 1) a[x] = a[x + x] + a[x + x + 1]; } int find(int l, int r) { int tot = 0; for (l += M - 1, r += M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) tot += a[l ^ 1]; if (r & 1) tot += a[r ^ 1]; } return tot; } int main(){ while (~scanf("%d", &t)) { int p = 0; while (++p <= t) { cout << "Case " << p << ":" << endl; cin >> n; for (M = 1; M < n; M += M); memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++){ scanf("%d", &m); add(i, m); } while (1) { cin >> s; if (s == "End") break; else { scanf("%d%d", &i, &j); if (s == "Add") add(i, j); if (s == "Sub") add(i, -j); if (s == "Query") cout << find(i, j) << endl; } } } } return 0; }学了splay过来练练手
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<functional> using namespace std; typedef unsigned long long ull; typedef long long LL; struct SPLAY { const static int maxn = 1e5 + 10; const static int INF = 0x7FFFFFFF; int ch[maxn][2], F[maxn], U[maxn], C[maxn], A[maxn], sz; int Node(int f, int u, int c) { A[sz] = C[sz] = c; ch[sz][0] = ch[sz][1] = 0; F[sz] = f; U[sz] = u; return sz++; } void clear(){ sz = 1; ch[0][0] = ch[0][1] = C[0] = A[0] = U[0] = F[0] = 0; } void rotate(int x, int k) { int y = F[x]; ch[y][!k] = ch[x][k]; F[ch[x][k]] = y; if (F[y]) ch[F[y]][y == ch[F[y]][1]] = x; F[x] = F[y]; F[y] = x; ch[x][k] = y; C[x] = C[y]; C[y] = C[ch[y][0]] + C[ch[y][1]] + A[y]; } void Splay(int x, int r) { for (int fa = F[r]; F[x] != fa;) { if (F[F[x]] == fa) { rotate(x, x == ch[F[x]][0]); return; } int y = x == ch[F[x]][0], z = F[x] == ch[F[F[x]]][0]; y^z ? (rotate(x, y), rotate(x, z)) : (rotate(F[x], z), rotate(x, y)); } } bool insert(int &x, int u, int v) { if (!x) { x = Node(0, u, v); return false; } int now = 0; for (int i = x; i&&!now; C[i] += v, i = ch[i][U[i] < u]) { if (u == U[i]) { A[i] += v; Splay(i, x); x = i; return true; } if (!ch[i][U[i] < u]) now = ch[i][U[i] < u] = Node(i, u, v); } Splay(now, x); x = now; return false; } void find(int &x, int l, int r) { for (int i = x; i; i = ch[i][U[i] < l - 1]) { if (U[i] == l - 1){ Splay(i, x); x = i; break; } } for (int i = ch[x][1]; i; i = ch[i][U[i] < r + 1]) { if (U[i] == r + 1){ Splay(i, ch[x][1]); break; } } printf("%d\n", C[ch[ch[x][1]][0]]); } }tree; int main() { int n, root, x, T, l, r, t = 0; char s[10]; cin >> T; while (T--) { scanf("%d", &n); tree.clear(); root = 0; for (int i = 1; i <= n; i++) scanf("%d", &x), tree.insert(root, i, x); tree.insert(root, 0, 0); tree.insert(root, n + 1, 0); printf("Case %d:\n", ++t); while (scanf("%s", s), s[0] != 'E') { scanf("%d%d", &l, &r); if (s[0] == 'A') tree.insert(root, l, r); if (s[0] == 'S') tree.insert(root, l, -r); if (s[0] == 'Q') tree.find(root, l, r); } } return 0; }