A Simple Problem with Integers
Description node You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. ui Input spa The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000. Output code You need to answer all Q commands in order. One answer in a line. 遞歸 Sample Input ip 10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 Sample Output get 4 55 9 15 Hint cmd
The sums may exceed the range of 32-bit integers.
Source string
POJ Monthly--2007.11.25, Yang Yi
|
[Submit] [Go Back] [Status] [Discuss]
/*============================================================================= # FileName: 3468.cpp # Desc: poj 3468 # Author: zhuting # Email: cnjs.zhuting@gmail.com # HomePage: my.oschina.net/locusxt # Version: 0.0.1 # CreatTime: 2013-12-08 11:25:00 # LastChange: 2013-12-08 11:25:00 # History: =============================================================================*/ #include <cstdio> #include <cstdlib> #include <string> #include <cstring> #include <algorithm> #define maxn 100005 class node/*區間結點*/ { public: int l, r; long long sum_, toadd;/*toadd爲待加的值*/ }; int a[maxn] = {0}; node no[maxn << 2]; long long ans = 0; void build(int le, int ri, int id)/*構建*/ { no[id].l = le; no[id].r = ri; no[id].toadd = 0; if (le == ri)/*區間中只有一個點*/ { no[id].sum_ = a[le]; return; } int mid = (le + ri) >> 1; build (le, mid, id << 1);/*遞歸建左右樹*/ build (mid + 1, ri, (id << 1) + 1); no[id].sum_ = no[id << 1].sum_ + no[(id << 1) + 1].sum_; } void ask(int le, int ri, int id)/*詢問區間結點id,在[le, ri]區間的和*/ { if (no[id].l > ri || no[id].r < le)/*區間結點id不在該區間中*/ { return; } if (no[id].l >= le && no[id].r <= ri)/*id被該區間徹底覆蓋,此時不須要計算待加值,直接加上sum_便可*/ { ans += no[id].sum_; return; } /*id與該區間相交*/ if (no[id].toadd)/*有未加項*/ { /*更新兩個子節點*/ int tmp_id = id << 1; no[tmp_id].sum_ += (no[tmp_id].r - no[tmp_id].l + 1) * no[id].toadd; no[tmp_id].toadd += no[id].toadd; ++tmp_id; no[tmp_id].sum_ += (no[tmp_id].r - no[tmp_id].l + 1) * no[id].toadd; no[tmp_id].toadd += no[id].toadd; no[id].toadd = 0; } /*而後再詢問子節點*/ int tmp_id = id << 1; ask(le, ri, tmp_id); ask(le, ri, tmp_id + 1); no[id].sum_ = no[tmp_id].sum_ + no[tmp_id + 1].sum_; return; } void update(int le, int ri, int id, long long delta)/*區間[le, ri]加上delta,對id的影響*/ { /*與上面詢問相似*/ if (no[id].l > ri || no[id].r < le) return; if (no[id].l >= le && no[id].r <= ri) { no[id].sum_ += (no[id].r - no[id].l + 1) * delta;/*id的sum_須要保證最新,id子節點的sum_以後能夠由id的delta算出*/ no[id].toadd += delta; return; } if (no[id].toadd) { int tmp_id = id << 1; no[tmp_id].sum_ += (no[tmp_id].r - no[tmp_id].l + 1) * no[id].toadd; no[tmp_id].toadd += no[id].toadd; ++tmp_id; no[tmp_id].sum_ += (no[tmp_id].r - no[tmp_id].l + 1) * no[id].toadd; no[tmp_id].toadd += no[id].toadd; no[id].toadd = 0; } int tmp_id = id << 1; update(le, ri, tmp_id, delta); update(le, ri, tmp_id + 1, delta); no[id].sum_ = no[tmp_id].sum_ + no[tmp_id + 1].sum_; return; } int main() { int n = 0, q = 0; scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } build(1, n, 1); char cmd; int x = 0, y = 0; long long z = 0; for (int i = 0; i < q; ++i) { scanf("\n%[CQ]%d%d", &cmd, &x, &y); if (cmd == 'C') { scanf("%lld", &z); update(x, y, 1, z); } else if (cmd == 'Q') { ans = 0; ask(x, y, 1); printf("%lld\n", ans); } } return 0; }