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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

hdu 4267 多维树状数组

發布時間:2023/12/2 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 4267 多维树状数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:有一個序列

"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)

分析轉自:http://www.cnblogs.com/Griselda/archive/2012/09/15/2686133.html

感覺是樹狀數組了

但是 樹狀數組是單點更新 而題目要求是在a b 內每隔 k 個數更新..

如果用for循環對a b 內每隔 k 個位置的數更新就會很費時間..

所以有一個方法就是 按 k 值 i%k 值還有 i/k+1 值把每隔 k 個位置的值都分組..即按 k 值和 i%k 值建樹狀數組?

到時候維護其中一棵 樹狀數組就好了..

?

維護的方法是在c[ k ][a%k][(i-a)/k+1] + 1 在c[ k ][a % k ][ (a/k) + (b-a)/k + 2 ] - 1..

?

關于建成的樹狀數組..

更新時有55種情況
1,2,3,4,5......
1,3,5,7,9......
2,4,6,8,10....
1,4,7,10,13...
2,5,9,12,15...
3,6,10,13,16...
.
.
.
10,20,30,40,50...

所以用55個樹狀數組.

?

建出來的樹并不是普通的1,2,3,4,這樣的,而是有著間隔的,其實也就是建造了許多樹狀數組

?

1 #include<stdio.h> 2 3 #include<queue> 4 #include<iostream> 5 #include<algorithm> 6 #include<string.h> 7 const int MAXN=50020; 8 9 10 int c[12][12][MAXN]; 11 int n; 12 13 int lowbit(int x) 14 { 15 return x&(-x); 16 } 17 18 void update(int t1,int t2,int i,int val) 19 { 20 while(i<=n) 21 { 22 c[t1][t2][i]+=val; 23 i+=lowbit(i); 24 } 25 26 } 27 int sum(int t1,int t2,int i) 28 { 29 30 int s=0; 31 while(i>0) 32 { 33 s+=c[t1][t2][i]; 34 i-=lowbit(i); 35 } 36 return s; 37 } 38 int num[MAXN]; 39 int main() 40 { 41 #ifndef ONLINE_JUDGE 42 freopen("1.in","r",stdin); 43 #endif 44 int m; 45 while(scanf("%d",&n)!=EOF) 46 { 47 for(int i=0;i<n;i++)scanf("%d",&num[i]); 48 for(int i=0;i<12;i++) 49 for(int j=0;j<12;j++) 50 for(int k=0;k<MAXN;k++) 51 c[i][j][k]=0; 52 scanf("%d",&m); 53 int a,b,k,q; 54 int t; 55 while(m--) 56 { 57 scanf("%d",&t); 58 if(t==1) 59 { 60 scanf("%d%d%d%d",&a,&b,&k,&q); 61 a--; 62 b--; 63 int num=(b-a)/k; 64 int s=a%k-1; 65 update(k,s,a/k+1,q); //s是該數組的開頭數字,a/k+1是結尾的數字 66 update(k,s,a/k+num+2,-q); 67 } 68 else 69 { 70 scanf("%d",&a); 71 a--; 72 int ss=num[a]; 73 for(int i=1;i<=10;i++) 74 { 75 ss+=sum(i,a%i-1,a/i+1); 76 } 77 printf("%d\n",ss); 78 } 79 } 80 } 81 return 0; 82 }

?

轉載于:https://www.cnblogs.com/cnblogs321114287/p/4343583.html

總結

以上是生活随笔為你收集整理的hdu 4267 多维树状数组的全部內容,希望文章能夠幫你解決所遇到的問題。

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