日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeVS 1081 线段树练习 2

發布時間:2023/12/13 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeVS 1081 线段树练习 2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 } View Code

?

轉載于:https://www.cnblogs.com/crackpotisback/p/4066537.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的CodeVS 1081 线段树练习 2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。