Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
树状数组#include <cstdio> const int MAXN = 100000; template <class T> class fenwick { public: T a[MAXN + 1], b[MAXN + 1]; int lowbit(int x) { return x & (-x); } void update_a(int pos, T val) { for (int i = pos; i >= 1; i -= lowbit(i)) a[i] += val; } void update_b(int pos, T val) { for (int i = pos; i <= MAXN; i += lowbit(i)) b[i] += val; } T query_a(int pos) { T ret = 0; for (int i = pos; i <= MAXN; i += lowbit(i)) ret += a[i]; return ret; } T query_b(int pos) { T ret = 0; for (int i = pos; i >= 1; i -= lowbit(i)) ret += b[i]; return ret; } void add(int pos, T val) { update_a(pos, val); update_b(pos, val * pos); } T query(int pos) { return query_a(pos) * pos + query_b(pos - 1); } void add(int l, int r, T val) { add(r, val); if (l > 1) add(l - 1, -val); } T query(int l, int r) { return query(r) - (l > 1 ? query(l - 1) : 0); } }; int main() { static fenwick<long long> tree; int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { int val; scanf("%d", &val); tree.add(i, i, val); } for (int i = 1; i <= m; i++) { char op[2]; scanf("%s", op); if (op[0] == 'C') { int l, r, val; scanf("%d%d%d", &l, &r, &val); tree.add(l, r, val); } else if (op[0] == 'Q') { int l, r; scanf("%d%d", &l, &r); printf("%I64d\n", tree.query(l, r)); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator