HOJ 1867 經理的煩惱 【 樹狀數組 】

來源:互聯網
上載者:User

標籤:

題意:給出一個區間,求這個區間裡面素數的個數

這道題wa了好多次---是因為add操作沒有寫對

每次更新的時候,應該先判斷沒有加上y是不是質數,加上了y是不是質數

如果從質數變成不是質數,那麼add(x,-1)

如果從不是質數變成是質數,那麼add(x,1)

另外還pe了,,printf("\n")就不對,puts("")就對了

---不懂--------

 1 #include<iostream>   2 #include<cstdio>   3 #include<cstring>  4 #include <cmath>  5 #include<stack> 6 #include<vector> 7 #include<map>  8 #include<set> 9 #include<queue> 10 #include<algorithm>  11 using namespace std;12 13 typedef long long LL;14 const int INF = (1<<30)-1;15 const int mod=1000000007;16 const int maxn=1000005;17 18 int a[maxn];19 int c[maxn];//樹狀數組 20 int C,N,M;21 22 int is(int x){23     if( x <= 1) return 0;24     if(x == 2) return 1;25     int tmp = sqrt(x);26     for(int i=2;i<=tmp;i++)27     if(x % i == 0) return 0;28     return 1;29 }30 31 int lowbit(int x){ return x & (-x);}32 33 int sum(int x){34     int ret=0;35     while(x>0){36         ret+=c[x];x-=lowbit(x);37     }38     return ret;39 } 40 41 void add(int x,int d){42     while(x <= maxn){43         c[x]+=d; x+=lowbit(x);44     }45 }46 47 int main(){48     int kase=0;49     while(scanf("%d %d %d",&C,&N,&M) != EOF){50         if( C == 0 && N ==0 && M == 0) break;51         memset(c,0,sizeof(c));52         memset(a,0,sizeof(a));53     54         int x = is(M);55         for(int i=1;i<=maxn;i++) a[i] = M,add(i,x);56         57         printf("CASE #%d:\n",++kase);58         int cmd;59         while(N--){60             scanf("%d",&cmd);61             if(cmd == 0){62                 int x,y;63                 scanf("%d %d",&x,&y);64                 int tmp = a[x];65                 a[x] += y;66                 67             //    printf("tmp = %d  a[%d] = %d\n",tmp,x,a[x]);68                 if(is(a[x])){69                     if(!is(tmp)) add(x,1);70                 }71                 else {72                     if(is(tmp)) add(x,-1);73                 }74             }75             if(cmd == 1){76                 int l,r;77                 scanf("%d %d",&l,&r);78                 printf("%d\n",sum(r) - sum(l-1));79             }80         } 81         puts("");82     }83     return 0;84 }
View Code

 

HOJ 1867 經理的煩惱 【 樹狀數組 】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.