F. SUM and REPLACE codeforces920f(线段树)

╰+哭是因爲堅強的太久メ 2021-11-01 20:02 388阅读 0赞

知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。
求一个数的因子个数:https://blog.csdn.net/ii0789789789/article/details/79683286

Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:

REPLACE l r — for every replace ai with D(ai);
SUM l r — calculate .

Print the answer for each SUM query.
Input

The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 106) — the elements of the array.

Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it’s SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).

There is at least one SUM query.
Output

For each SUM query print the answer to it.
Example
Input
Copy

7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7

Output
Copy

30
13
4
22

分析:
这又是一颗线段树。我数组一开始开小了wa。一开始不会求因子个数wa。好菜呀。

势能线段树大都是通过限制条件以达到减少多颗子树的搜索。
例如 最大值,次大值的限制。这个题,我们要考虑的限制条件是因子个数,无论多大的树,最大6次操作后,值会变成1。那么我们每个结点都维护一个time值,如果这个结点的访问次数超过了10次,就可以不搜索这个结点的子树了。

  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"math.h"
  4. #include"algorithm"
  5. using namespace std;
  6. typedef long long ll;
  7. int num_factor[1001000];
  8. int n,m,a[5001000],time[5001000];
  9. int val[5001000];
  10. ll sum[5001000];
  11. void Push_up(int id)
  12. {
  13. sum[id] = sum[id << 1] + sum[id << 1 | 1];
  14. return ;
  15. }
  16. void Build_Tree(int id,int l,int r)
  17. {
  18. sum[id] = 0;
  19. time[id] = 0;
  20. val[id] = 0;
  21. if(l == r)
  22. {
  23. sum[id] = a[l];
  24. val[id] = a[l];
  25. return ;
  26. }
  27. int mid = (l + r) >> 1;
  28. Build_Tree(id << 1,l,mid);
  29. Build_Tree(id << 1 | 1,mid + 1,r);
  30. Push_up(id);
  31. }
  32. ll Query_sum(int id,int L,int R,int l,int r)
  33. {
  34. if(l <= L && r >= R)
  35. return sum[id];
  36. int mid = (L + R) >> 1;
  37. ll ans = 0;
  38. if(l <= mid)
  39. ans += Query_sum(id << 1,L,mid,l,r);
  40. if(r > mid)
  41. ans += Query_sum(id << 1 | 1,mid + 1,R,l,r);
  42. return ans;
  43. }
  44. void Update(int id,int L,int R,int l,int r)
  45. {
  46. if(l <= L && r >= R)
  47. {
  48. if(time[id] >= 10)
  49. return ;
  50. time[id] ++;
  51. }
  52. if(L == R)
  53. {
  54. // printf("val[id] = %d num_factor = %d\n",val[id],num_factor[val[id]]);
  55. val[id] = num_factor[val[id]];
  56. sum[id] = val[id];
  57. return ;
  58. }
  59. int mid = (L + R) >> 1;
  60. if(l <= mid)
  61. Update(id << 1,L,mid,l,r);
  62. if(r > mid)
  63. Update(id << 1 | 1,mid + 1,R,l,r);
  64. Push_up(id);
  65. }
  66. ///菜鸡版因子个数 555
  67. void init()
  68. {
  69. num_factor[1] = 1;
  70. num_factor[2] = 2;
  71. num_factor[3] = 2;
  72. for(int i = 4; i <= 1000000; i ++)
  73. {
  74. int n = i;
  75. int num = 1;
  76. for(int j = 2; j * j <= n; j ++)
  77. {
  78. int cnt = 0;
  79. while(n % j == 0)
  80. {
  81. cnt ++; n = n / j;
  82. }
  83. num = num * (cnt + 1);
  84. }
  85. if(n != 1)
  86. {
  87. num = num * 2;
  88. }
  89. num_factor[i] = num;
  90. }
  91. }
  92. int main()
  93. {
  94. init();
  95. scanf("%d%d",&n,&m);
  96. for(int i = 1; i <= n; i ++)
  97. scanf("%d",&a[i]);
  98. Build_Tree(1,1,n);
  99. for(int i = 1; i <= m; i ++)
  100. {
  101. int op,x,y;
  102. scanf("%d%d%d",&op,&x,&y);
  103. if(op == 1)
  104. Update(1,1,n,x,y);
  105. if(op == 2)
  106. {
  107. printf("%lld\n",Query_sum(1,1,n,x,y));
  108. }
  109. }
  110. }

发表评论

表情:
评论列表 (有 0 条评论,388人围观)

还没有评论,来说两句吧...

相关阅读