当前位置:   article > 正文

算法课程笔记——如何倍增

算法课程笔记——如何倍增

快速幂

读入量大于1e5不要用cin读入,要用也要关闭同步流

第i个o次方的父亲


 

  1. #include<bits/stdc++.h>usingnamespacestd;
  2. #definemaxn 110000#definell long longintn, a[maxn], f[maxn][40];
  3. intquery(intl, intr){
  4. intk = (int)(log((r - l + 1) * 1.0) / log(2.0));
  5. returnmin(f[l][k], f[r - (1<< k) + 1][k]);
  6. }
  7. intmain(){
  8. scanf("%d", &n);
  9. for(inti = 1; i <= n; ++ i )
  10. scanf("%d", &a[i]), f[i][0] = a[i];
  11. for(intj = 1; j <= (int)(log(n * 1.0) / log(2.0)); ++ j )
  12. for(inti = 1; i + (1<< j) - 1<= n; ++ i )
  13. f[i][j] = min(f[i][j - 1], f[i + (1<< (j - 1))][j - 1]);
  14. intq;
  15. scanf("%d", q);
  16. while(q -- )
  17. {
  18. intl, r;
  19. scanf("%d%d", &l, &r);
  20. printf("%d\n", query(l, r));
  21. }
  22. return0;
  23. }

  1. #include<bits/stdc++.h>usingnamespacestd;
  2. #definemaxn 110000#definell long longintn, a[maxn], f[maxn][40];
  3. intquery(intl, intr){
  4. intk = (int)(log((r - l + 1) * 1.0) / log(2.0));
  5. returnmin(f[l][k], f[r - (1<< k) + 1][k]);
  6. }
  7. intmain(){
  8. scanf("%d", &n);
  9. for(inti = 1; i <= n; ++ i )
  10. scanf("%d", &a[i]), f[i][0] = a[i];
  11. for(intj = 1; j <= (int)(log(n * 1.0) / log(2.0)); ++ j )
  12. for(inti = 1; i + (1<< j) - 1<= n; ++ i )
  13. f[i][j] = min(f[i][j - 1], f[i + (1<< (j - 1))][j - 1]);
  14. intq;
  15. scanf("%d", q);
  16. while(q -- )
  17. {
  18. intl, r;
  19. scanf("%d%d", &l, &r);
  20. printf("%d\n", query(l, r));
  21. }
  22. return0;
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/528199
推荐阅读
相关标签
  

闽ICP备14008679号