1081 線段樹練習 2
?
?時間限制: 1 s
?空間限制: 128000 KB
?題目等級 : 大師 Master
題目描述?Description
給你N個數,有兩種操作
1:給區間[a,b]的所有數都增加X
2:詢問第i個數是什么?
輸入描述?Input Description
第一行一個正整數n,接下來n行n個整數,再接下來一個正整數Q,表示操作的個數. 接下來Q行每行若干個整數。如果第一個數是1,后接3個正整數a,b,X,表示在區間[a,b]內每個數增加X,如果是2,后面跟1個整數i, 表示詢問第i個位置的數是多少。
輸出描述?Output Description
對于每個詢問輸出一行一個答案
樣例輸入?Sample Input
3
1
2
3
2
1 2 3 2
2 3
樣例輸出?Sample Output
5
數據范圍及提示?Data Size & Hint
數據范圍
1<=n<=100000
1<=q<=100000
?
解題:線段樹的基本操作。。。


1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 100010; 18 struct node { 19 int lt,rt,val; 20 }; 21 node tree[maxn<<2]; 22 void build(int lt,int rt,int v) { 23 tree[v].lt = lt; 24 tree[v].rt = rt; 25 if(lt == rt) { 26 scanf("%d",&tree[v].val); 27 return; 28 } 29 tree[v].val = 0; 30 int mid = (lt + rt)>>1; 31 build(lt,mid,v<<1); 32 build(mid+1,rt,v<<1|1); 33 } 34 void update(int lt,int rt,int v,int val) { 35 if(tree[v].lt >= lt && tree[v].rt <= rt) { 36 tree[v].val += val; 37 return; 38 } 39 if(tree[v].val) { 40 tree[v<<1].val += tree[v].val; 41 tree[v<<1|1].val += tree[v].val; 42 tree[v].val = 0; 43 return; 44 } 45 if(rt >= tree[v<<1|1].lt) update(lt,rt,v<<1|1,val); 46 if(lt <= tree[v<<1].lt) update(lt,rt,v<<1,val); 47 } 48 int query(int lt,int rt,int v) { 49 if(tree[v].lt >= lt && tree[v].rt <= rt) { 50 return tree[v].val; 51 } 52 if(tree[v].val) { 53 tree[v<<1].val += tree[v].val; 54 tree[v<<1|1].val += tree[v].val; 55 tree[v].val = 0; 56 } 57 if(lt <= tree[v<<1].rt) return query(lt,rt,v<<1); 58 if(rt >= tree[v<<1|1].lt) return query(lt,rt,v<<1|1); 59 } 60 int main() { 61 int n,m,op,a,b,x; 62 while(~scanf("%d",&n)) { 63 build(1,n,1); 64 scanf("%d",&m); 65 while(m--) { 66 scanf("%d",&op); 67 if(op == 1) { 68 scanf("%d %d %d",&a,&b,&x); 69 update(a,b,1,x); 70 } else if(op == 2) { 71 scanf("%d",&a); 72 printf("%d\n",query(a,a,1)); 73 } 74 } 75 } 76 return 0; 77 }
?