标签:ati ext ++ mit ica simple return strong nod
线段树区间更新模板题,加个延迟标记即可。
注意:区间大小是r-l+1,query,更新时要更新子节点,同时注意+=与=!
discuss里面有数据,wa的可以看看。。。
代码:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <cstdlib> 10 #include <cstring> 11 #include <sstream> 12 #include <iostream> 13 #include <algorithm> 14 #include <functional> 15 using namespace std; 16 #define rep(i,a,n) for (int i=a;i<n;i++) 17 #define per(i,a,n) for (int i=n-1;i>=a;i--) 18 #define all(x) (x).begin(),(x).end() 19 #define pb push_back 20 #define mp make_pair 21 #define lson l,m,rt<<1 22 #define rson m+1,r,rt<<1|1 23 typedef long long ll; 24 typedef vector<int> VI; 25 typedef pair<int, int> PII; 26 const ll MOD = 1e9 + 7; 27 const int inf = 0x3f3f3f3f; 28 //head 29 #define maxn 500010 30 struct node{ 31 int left, right; 32 long long val, lazy; 33 node() {} 34 node(int l, int r, long long v, long long la): left(l), right(r), val(v), lazy(la){} 35 }; 36 node tree[maxn]; 37 int a[maxn], n; 38 39 void build(int l, int r, int root){ 40 tree[root] = node(l, r, 0, 0); 41 if(l == r) 42 tree[root].val = a[l]; 43 else{ 44 int mid = (l + r) / 2; 45 build(l, mid, root << 1); 46 build(mid + 1, r, root << 1 | 1); 47 tree[root].val = tree[root << 1].val + tree[root << 1 | 1].val; 48 } 49 } 50 void update(int ul, int ur, int v, int l, int r, int k){ 51 if(ul == l && ur == r){ 52 tree[k].lazy += v; 53 return; 54 } 55 if(ul > r || ur < l) 56 return; 57 58 tree[k].val += tree[k].lazy * (tree[k].right - tree[k].left + 1); 59 tree[k << 1].lazy += tree[k].lazy; 60 tree[k << 1 | 1].lazy += tree[k].lazy; 61 tree[k].lazy = 0; 62 63 tree[k].val += v * (ur - ul + 1); 64 int mid = (l + r) / 2; 65 if(ur <= mid){ 66 update(ul, ur, v, l, mid, k << 1); 67 } 68 else if(ul <= mid){ 69 update(ul, mid, v, l, mid, k << 1); 70 update(mid + 1, ur, v, mid + 1, r, k << 1 | 1); 71 } 72 else{ 73 update(ul, ur, v, mid + 1, r, k << 1 | 1); 74 } 75 } 76 long long query(int ql, int qr, int l, int r, int k){ 77 if(ql == l && qr == r){ 78 tree[k].val += tree[k].lazy * (tree[k].right - tree[k].left + 1); 79 tree[k << 1].lazy += tree[k].lazy; 80 tree[k << 1 | 1].lazy += tree[k].lazy; 81 tree[k].lazy = 0; 82 return tree[k].val; 83 } 84 85 86 tree[k].val += tree[k].lazy * (tree[k].right - tree[k].left + 1); 87 tree[k << 1].lazy += tree[k].lazy; 88 tree[k << 1 | 1].lazy += tree[k].lazy; 89 tree[k].lazy = 0; 90 91 int mid = (l + r) / 2; 92 if(qr <= mid){ 93 return query(ql, qr, l, mid, k << 1); 94 } 95 else if(ql <= mid){ 96 return query(ql, mid, l, mid, k << 1) + query(mid + 1, qr, mid + 1, r, k << 1 | 1); 97 } 98 else 99 return query(ql, qr, mid + 1, r, k << 1 | 1); 100 } 101 102 int main(){ 103 memset(a, 0, sizeof(a)); 104 memset(tree, 0 ,sizeof(tree)); 105 int q; 106 scanf("%d %d", &n, &q); 107 for(int i = 0; i < n; i++) 108 scanf("%lld", &a[i]); 109 build(0, n - 1, 1); 110 while(q--){ 111 char ch; 112 int tma, tmb; 113 scanf(" %c %d %d", &ch, &tma, &tmb); 114 tma--; tmb--; 115 if(ch == ‘Q‘){ 116 //debug 117 //for(int i = 0; i < 28; i++) printf("%d %d %d\n", i, tree[i].val, tree[i].lazy); 118 printf("%lld\n", query(tma, tmb, 0, n - 1, 1)); 119 } 120 else if(ch == ‘C‘){ 121 int tmc; 122 scanf("%d", &tmc); 123 update(tma, tmb, tmc, 0, n - 1, 1); 124 //debug 125 //for(int i = 0; i < 28; i++) printf("%d %d %d\n", i, tree[i].val, tree[i].lazy); 126 } 127 //debug 128 //if(q == 3){ 129 //for(int i = 0; i < 28; i++) printf("%d %d %d\n", i, tree[i].val, tree[i].lazy); 130 //} 131 } 132 }
题目:
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 110849 | Accepted: 34520 | |
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
POJ 3468 A Simple Problem with Integers
标签:ati ext ++ mit ica simple return strong nod
原文地址:http://www.cnblogs.com/bolderic/p/7141487.html