zoukankan      html  css  js  c++  java
  • HOJ 1867 经理的烦恼 【 树状数组 】

    题意:给出一个区间,求这个区间里面素数的个数

    这道题wa了好多次---是因为add操作没有写对

    每次更新的时候,应该先判断没有加上y是不是质数,加上了y是不是质数

    如果从质数变成不是质数,那么add(x,-1)

    如果从不是质数变成是质数,那么add(x,1)

    另外还pe了,,printf(" ")就不对,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:
    ",++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
    ",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
    ",sum(r) - sum(l-1));
    79             }
    80         } 
    81         puts("");
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    刚听完CSDN总裁蒋涛先生的学术报告
    WinForm下屏幕截图程序的实现
    .NET4.5 Async 与 Async Targeting Pack区别
    WP8中的Tiles
    WP8中调用APP的方式
    安装Win8后必做的优化
    如何将项目从WP7升级到WP8
    ActiveWriter集成到VS.NET的NHibernate(ActiveRecord)对象可视化设计工具
    概述CSLA.NET 3.6 (Overview of CSLA .NET 3.6 for Windows and Silverlight)
    SQL Server BI Step by Step 1 准备
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4594680.html
Copyright © 2011-2022 走看看