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.
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
You need to answer all Q commands in order. One answer in a line.
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 4Sample Output
4 55 9 15Hint
The sums may exceed the range of 32-bit integers.
题目分析 : 有两种操作可以去执行,如题目中所说,但是这个题目有一个很坑的地方,就是懒标记的地方,WA了 10 几次,后来才发现了问题 , 发现还是懒标记的地方理解有偏差,对区间进行操作的时候要引入懒标记,懒标记的产生为了什么呢?不就是为了带有懒标记下面的区间我是不用更新的么,但是呢,有懒标记的区间我是已经更新了的,那么在懒标记下传的时候,对于懒标记下面的区间我是只需要更新的内容为上面的懒标记所记录的值,因为自身当前的懒标记所记录的值我是已经更新过的 。
using namespace std; const int eps = 1e6+5; const int maxn = 0x3f3f3f3f; #define ll long long #define lson k<<1 #define rson k<<1|1 int n, m; struct node { int l, r; ll lazy, sum; }t[eps<<2]; void pushup(int k){ t[k].sum = t[lson].sum + t[rson].sum; } void pushdown(int k){ // 本题就是 坑在了这里 !!!! t[lson].sum += 1ll*(t[lson].r - t[lson].l + 1)*t[k].lazy; t[rson].sum += 1ll*(t[rson].r - t[rson].l + 1)*t[k].lazy; t[lson].lazy += t[k].lazy; t[rson].lazy += t[k].lazy; t[k].lazy = 0; } void build(int l, int r, int k){ t[k].l = l; t[k].r = r; t[k].lazy = 0; if (l == r){ scanf("%lld", &t[k].sum); return; } int m = (t[k].l + t[k].r) >> 1; build(l, m, lson); build(m+1, r, rson); pushup(k); } ll query(ll l, ll r, int k){ if (l <= t[k].l && t[k].r <= r){ return t[k].sum; } if (t[k].lazy != 0) pushdown(k); int m = (t[k].l + t[k].r) >> 1; ll ans = 0; if (l <= m) ans += query(l, r, lson); if (r > m) ans += query(l, r, rson); pushup(k); return ans; } void update(ll l, ll r, ll c, int k){ if (l <= t[k].l && t[k].r <= r){ t[k].sum += 1ll*(t[k].r - t[k].l + 1)*c; t[k].lazy += 1ll*c; return; } if (t[k].lazy != 0) pushdown(k); int m = (t[k].l + t[k].r) >> 1; if (l <= m) update(l, r, c, lson); if (r > m) update(l, r, c, rson); pushup(k); } int main() { cin >> n >> m; char s[5]; ll a, b, c; build(1, n, 1); for(int i = 1; i <= m; i++){ scanf("%s", s); if (s[0] == 'Q'){ scanf("%lld%lld", &a, &b); printf("%lld ", query(a, b, 1)); } else { scanf("%lld%lld%lld", &a, &b, &c); update(a, b, c, 1); } } return 0; }