Advertisement
Falak_Ahmed_Shakib

sieve + prime-----2

Jul 25th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. const int N=1e6;
  4.  
  5. int ara[1000010];
  6. int bara[1000010];
  7.  
  8.  
  9. void sieve()
  10. {
  11. ara[0]=1;
  12. ara[1]=1;
  13.  
  14. int i,j,sz=sqrt(N);
  15.  
  16. for(i=2; i<=sz; i++)
  17. {
  18. if(ara[i]==0)
  19. {
  20. for(j=i*2; j<=N; j+=i)
  21. {
  22. ara[j]=1;
  23. }
  24. }
  25. }
  26. }
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32. sieve();
  33.  
  34. int n,sum=0,i,a;
  35. scanf("%d",&n);
  36.  
  37. for(i=1;i<=n;i++)
  38. {
  39. scanf("%d",&a);
  40.  
  41. if(ara[a]==0)
  42. {
  43. sum=sum+1;
  44. bara[i]=sum;
  45. //printf("%d\n",bara[i]);
  46. }
  47. else
  48. {
  49. bara[i]=sum;
  50. //printf("%d\n",bara[i]);
  51. }
  52. }
  53.  
  54.  
  55. int t,b,c,kase;
  56. scanf("%d",&t);
  57. while(t--)
  58. {
  59. scanf("%d",&kase);
  60.  
  61. if(kase==1)
  62. { scanf("%d%d",&b,&c);
  63. printf("%d\n",bara[c]-bara[b-1]);
  64. }
  65.  
  66. else
  67. {
  68.  
  69. int pos,valu;
  70. scanf("%d%d",&pos,&valu);
  71.  
  72. if(bara[pos]==bara[pos-1] && ara[valu]==0 )
  73. {
  74. for(i=pos;i<=n;i++)
  75. {
  76. bara[i]=bara[i]+1;
  77. }
  78. }
  79. else if( bara[pos]>bara[pos-1] && ara[valu]==1)
  80. {
  81. for(i=pos;i<=n;i++)
  82. {
  83. bara[i]=bara[i]-1;
  84. }
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. }
  98.  
  99.  
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. -------------------------------------------------------------------------------------------------------------0--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement