A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 108903 Accepted: 33919
Case Time Limit: 2000MS
Description
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.
Input
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.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
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
4 55 9 15
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
带lazy的线段树
1 //2017-05-17 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #define ll long long 7 #define mid ((st[id].l+st[id].r)>>1) 8 #define lson (id<<1) 9 #define rson ((id<<1)|1) 10 11 using namespace std; 12 13 const ll N = 100005; 14 ll arr[N]; 15 struct Node{ 16 ll l, r, sum, lazy; 17 }st[N<<2]; 18 19 void build(int id, int l, int r) 20 { 21 st[id].l = l; st[id].r = r; st[id].lazy = 0; 22 if(l == r){ 23 st[id].sum = arr[l]; 24 return; 25 } 26 build(lson, l, mid); 27 build(rson, mid+1, r); 28 st[id].sum = st[lson].sum+st[rson].sum; 29 } 30 31 void push_down(int id) 32 { 33 if(st[id].lazy != 0){ 34 st[lson].lazy += st[id].lazy; 35 st[rson].lazy += st[id].lazy; 36 st[id].sum += (st[id].r-st[id].l+1)*st[id].lazy; 37 st[id].lazy = 0; 38 } 39 return; 40 } 41 42 ll query(int id, int l, int r) 43 { 44 if(st[id].l == l && st[id].r == r)return st[id].sum+(r-l+1)*st[id].lazy; 45 push_down(id); 46 if(r <= mid)return query(lson, l, r); 47 else if(l > mid)return query(rson, l, r); 48 else return query(lson, l, mid)+query(rson, mid+1, r); 49 } 50 51 void update(int id, int l, int r, int w) 52 { 53 if(st[id].l == l && st[id].r == r){ 54 st[id].lazy += w; 55 return; 56 } 57 st[id].sum += (r-l+1)*w; 58 if(r <= mid)update(lson, l, r, w); 59 else if(l > mid)update(rson, l, r, w); 60 else{ 61 update(lson, l, mid, w); 62 update(rson, mid+1, r, w); 63 } 64 } 65 66 int main() 67 { 68 ll n, q; 69 while(scanf("%lld%lld", &n, &q)!=EOF){ 70 for(int i = 1; i <= n; i++) 71 scanf("%lld", &arr[i]); 72 build(1, 1, n); 73 char op[5]; 74 ll a, b, c; 75 while(q--){ 76 scanf("%s", op); 77 if(op[0] == 'Q'){ 78 scanf("%lld%lld", &a, &b); 79 printf("%lld ", query(1, a, b)); 80 }else if(op[0] == 'C'){ 81 scanf("%lld%lld%lld", &a, &b, &c); 82 update(1, a, b, c); 83 } 84 } 85 } 86 87 return 0; 88 }
1 //2018-03-28 2 3 import java.util.*; 4 5 public class Main { 6 7 public static void main(String[] args) { 8 Scanner cin = new Scanner(System.in); 9 10 int n, q; 11 while(cin.hasNext()) { 12 n = cin.nextInt(); 13 q = cin.nextInt(); 14 SegmentTree st = new SegmentTree(n); 15 for(int i = 1; i <= n; i++) 16 st.arr[i] = cin.nextLong(); 17 st.build(1, 1, n); 18 char op; 19 int a, b; 20 long c; 21 while(q-- > 0) { 22 op = cin.next().charAt(0); 23 if(op == 'Q') { 24 a = cin.nextInt(); 25 b = cin.nextInt(); 26 System.out.println(st.query(1, a, b)); 27 }else if(op == 'C') { 28 a = cin.nextInt(); 29 b = cin.nextInt(); 30 c = cin.nextLong(); 31 st.updata(1, a, b, c); 32 } 33 } 34 } 35 } 36 } 37 38 class SegmentTree{ 39 static class Node{ 40 public int l, r; 41 long sum, lazy; 42 Node(int _l, int _r, long _sum, long _lazy){ 43 this.l = _l; 44 this.r = _r; 45 this.sum = _sum; 46 this.lazy = _lazy; 47 } 48 } 49 50 public int n; 51 public long [] arr; 52 public Node [] nodes; 53 54 SegmentTree(int _n){ 55 this.n = _n; 56 arr = new long[n+1]; 57 nodes = new Node[n<<2]; 58 } 59 60 int lson(int id) { 61 return (id<<1); 62 } 63 64 int rson(int id) { 65 return ((id<<1)|1); 66 } 67 68 int mid(int id) { 69 return (nodes[id].l + nodes[id].r)>>1; 70 } 71 void build(int id, int l, int r) { 72 nodes[id] = new Node(l, r, 0, 0); 73 if(l == r) { 74 nodes[id].sum = arr[l]; 75 return; 76 } 77 build(lson(id), l, mid(id)); 78 build(rson(id), mid(id)+1, r); 79 nodes[id].sum = nodes[lson(id)].sum + nodes[rson(id)].sum; 80 } 81 82 void pushDown(int id) { 83 if(nodes[id].lazy != 0) { 84 nodes[lson(id)].lazy += nodes[id].lazy; 85 nodes[rson(id)].lazy += nodes[id].lazy; 86 nodes[id].sum += (nodes[id].r-nodes[id].l+1)*nodes[id].lazy; 87 nodes[id].lazy = 0; 88 } 89 } 90 91 long query(int id, int l, int r) { 92 if(nodes[id].l == l && nodes[id].r == r) 93 return nodes[id].sum+(r-l+1)*nodes[id].lazy; 94 pushDown(id); 95 if(r <= mid(id))return query(lson(id), l, r); 96 else if(l > mid(id))return query(rson(id), l, r); 97 else return query(lson(id), l, mid(id))+query(rson(id), mid(id)+1, r); 98 } 99 100 void updata(int id, int l, int r, long w) { 101 if(nodes[id] == null)return; 102 if(nodes[id].l == l && nodes[id].r == r) { 103 nodes[id].lazy += w; 104 return; 105 } 106 nodes[id].sum += (r-l+1)*w; 107 if(r <= mid(id))updata(lson(id), l, r, w); 108 else if(l > mid(id))updata(rson(id), l, r, w); 109 else { 110 updata(lson(id), l, mid(id), w); 111 updata(rson(id), mid(id)+1, r, w); 112 } 113 } 114 }