HDU 4267 A Simple Problem with Integers 樹狀數組

來源:互聯網
上載者:User

來源:http://acm.hdu.edu.cn/showproblem.php?pid=4267

題意:給一些數,有兩種操作,一種是在[a,b] 區間內,對(i - a)% k == 0 的加value,另一種操作是詢問某個位置的值。

思路:很容易想到這是一個插線問點的問題,但是在更新值 的時候和平常的更新不同,這道題又增加了一個限制條件。因為k比較小(<=10),餘數的情況也很少,因此我們可以開55棵樹狀數組,用cnt[x][k][mod] 表示x 對k 取餘為mod,這樣在詢問的時候只要迴圈到10就可以 了。而且(i - a) % k == 0即是 i % k == a % k == mod,這樣更新的時候就可以更新(1,b) 和(1,a)了。

代碼:

#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>using namespace std;const int N = 50010;int cnt[N][11][11];int num[N];int inline lowbit(int x){return x & (-x);}void inline update(int x,int k,int mod,int add){while(x > 0){  cnt[x][k][mod] += add;  x -= lowbit(x);}}int inline sum(int x,int a){int s = 0;while(x < N){for(int i = 1; i <= 10; ++i){   s += cnt[x][i][a%i];}x += lowbit(x);}return s;}int main(){//freopen("1.txt","r",stdin);int n;while(scanf("%d",&n) != EOF){   for(int i = 1; i <= n; ++i)   scanf("%d",&num[i]);   memset(cnt,0,sizeof(cnt));   int m;   scanf("%d",&m);   while(m--){      int op,a,b,k,add;  scanf("%d",&op);  if(op == 1){     scanf("%d%d%d%d",&a,&b,&k,&add); update(b,k,a%k,add); update(a-1,k,a%k,-add);  }  else{    scanf("%d",&a);int ans = sum(a,a);printf("%d\n",ans + num[a]);  }   }}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.