题意:

  1. 给以一个定义, Fl r 的值表示序列 A[1:n]的子序列
  2. A[1....(l-1),(r+1)...n] 之中 任意两个数的最大公约数的最大值。

求 Sum=∑i=1N∑j=1N(F(i,j)),(i≠j)

思路:

英文题解

大致解释一下:

  1. H[i] 表示 F(l, r) <= i 区间 lr 的个数。
  2. V[A[i]] = { b1, b2, .... bk }, 其中 A[i] % bx == 0
  3. 还有一个next 数组
  4. next[j] = k, 表示 F(l, k) <= i, k 尽可能的小。
  5. 当然会有 k 不存在的时候, next[j] = n+1;(为什么, 看下面)
  6. 这时候可以知道 :

H[i]=∑j=1N(n−next[j]+1)

  1. 右端为 next[j] 左端则有 n-next[j]+1种选择。

Code:

  1. #include <bits/stdc++.h>
  2. #define lson l , m, rt<<1
  3. #define rson m+1, r, rt<<1|1
  4. using namespace std;
  5. typedef long long LL;
  6. const LL maxn = 200000 + 131;
  7. const LL MaxN = 200000;
  8. struct node {
  9. LL Max, Min;
  10. LL Sum, Flag;
  11. };
  12. node Node[maxn<<2];
  13. LL H[maxn], A[maxn], Idx[maxn];
  14. LL L1[maxn], L2[maxn], R1[maxn], R2[maxn];
  15. ///SegmentTree
  16. void PushUp(LL rt) {
  17. Node[rt].Max = max(Node[rt<<1].Max, Node[rt<<1|1].Max);
  18. Node[rt].Min = min(Node[rt<<1].Min, Node[rt<<1|1].Min);
  19. Node[rt].Sum = Node[rt<<1].Sum + Node[rt<<1|1].Sum;
  20. }
  21. void PushDown(LL l, LL r, LL rt) {
  22. LL m = (l + r) >> 1;
  23. if(Node[rt].Flag)
  24. {
  25. Node[rt<<1].Flag = Node[rt<<1].Max = Node[rt<<1].Min = Node[rt].Flag;
  26. Node[rt<<1|1].Flag = Node[rt<<1|1].Max = Node[rt<<1|1].Min = Node[rt].Flag;
  27. Node[rt<<1].Sum = Node[rt].Flag * (m - l + 1);
  28. Node[rt<<1|1].Sum = Node[rt].Flag * (r - m);
  29. Node[rt].Flag = 0;
  30. }
  31. }
  32. void Build(LL l, LL r, LL rt) {
  33. Node[rt].Flag = 0;
  34. if(l == r) {
  35. Node[rt].Max = Node[rt].Min = Node[rt].Sum = l;
  36. return ;
  37. }
  38. LL m = (l + r) >> 1;
  39. Build(lson), Build(rson);
  40. PushUp(rt);
  41. }
  42. void Update_section(LL L, LL R, LL val, LL l, LL r, LL rt) {
  43. if(L > R) return ;
  44. if(Node[rt].Min >= val) return ;
  45. if(L <= l and r <= R and Node[rt].Max <= val) {
  46. Node[rt].Flag = val;
  47. Node[rt].Min = Node[rt].Max = val;
  48. Node[rt].Sum = LL(r - l + 1) * LL(val);
  49. return ;
  50. }
  51. PushDown(l, r, rt);
  52. LL m = (l + r) >> 1;
  53. if(L <= m) Update_section(L, R, val, lson);
  54. if(R > m) Update_section(L, R, val, rson);
  55. PushUp(rt);
  56. }
  57. /// Solve
  58. int main() {
  59. std::ios::sync_with_stdio(false);
  60. LL n;
  61. cin >> n;
  62. for(LL i = 1; i <= n; ++i) {
  63. cin >> A[i];
  64. Idx[A[i]] = i;
  65. }
  66. /// Get l(1, b1) -> bk-1, (b1,b2) -> bk, (b2, n) -> n+1
  67. for(LL i = 1; i <= MaxN; ++i)
  68. {
  69. for(LL j = i; j <= MaxN; j +=i)
  70. if(Idx[j])
  71. {
  72. if(L1[i] == 0 or L1[i] > Idx[j]) L2[i] = L1[i], L1[i] = Idx[j];
  73. else if(L2[i] == 0 or L2[i] > Idx[j]) L2[i] = Idx[j];
  74. if(R1[i] < Idx[j]) R2[i] = R1[i], R1[i] = Idx[j];
  75. else if(R2[i] < Idx[j]) R2[i] = Idx[j];
  76. }
  77. }
  78. /// Build Tree
  79. Build(1,n,1);
  80. for(LL i = MaxN; i > 0; --i)
  81. {
  82. if(L1[i] != R1[i])
  83. {
  84. Update_section(1,L1[i],R2[i], 1, n, 1);
  85. Update_section(L1[i]+1, L2[i], R1[i], 1, n, 1);
  86. Update_section(L2[i]+1, n, n+1, 1, n, 1);
  87. }
  88. H[i] = LL(n*(n+1)) - Node[1].Sum;
  89. //cout << H[i] << endl;
  90. }
  91. LL Ans = 0;
  92. for(LL i = 1; i < MaxN; ++i)
  93. Ans += i * (H[i+1]-H[i]);
  94. cout << Ans <<endl;
  95. return 0;
  96. }

CodeForces 671C - Ultimate Weirdness of an Array的更多相关文章

  1. Codeforces 671C - Ultimate Weirdness of an Array(线段树维护+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 *2800 的 DS,不过还是被我自己想出来了 u1s1 这个 D1C 比某些 D1D 不知道难到什么地方去了 首先碰到这类问题我们肯定考 ...

  2. codeforces 671C Ultimate Weirdness of an Array 线段树+构造

    题解上说的很清楚了,我照着写的,表示膜拜题解 然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn) #include <stdio.h> #inc ...

  3. Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)

    看见$a_i\leq 200000$和gcd,就大概知道是要枚举gcd也就是答案了... 因为答案是max,可以发现我们很容易算出<=i的答案,但是很难求出单个i的答案,所以我们可以运用差分的思 ...

  4. 【CodeForces】671 C. Ultimate Weirdness of an Array

    [题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...

  5. Ultimate Weirdness of an Array CodeForces - 671C (gcd,线段树)

    大意: 定义一个数列的特征值为两个数gcd的最大值, $f(l,r)$表示数列删除区间$[l,r]$的元素后剩余元素的特征值, 求$\sum_{i=1}^n\sum_{j=i}^n{f(i,j)}$ ...

  6. CF671C. Ultimate Weirdness of an Array

    n<=200000个<=200000的数问所有的f(i,j)的和,表示去掉区间i到j后的剩余的数字中任选两个数的最大gcd. 数论日常不会.. 先试着计算一个数组:Hi表示f(l,r)&l ...

  7. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  8. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  9. Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配

    题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...

随机推荐

  1. flutter 自定义主题切换

    1. 定义local_srorage.dart文件 使用Flutter第三方插件shared_preferences实现存储键值对信息 相关shared_preferences插件可参考: flutt ...

  2. 佳文赏析:How to uninstall Linux

    来源:https://www.dedoimedo.com/computers/linux-uninstall.html This article was suggested to me by a re ...

  3. 二、Redis安装

    一.下载Redis: 下载地址:https://github.com/MSOpenTech/redis/releases. 由于redis并不支持window系统,而window版本的redis的是由 ...

  4. document对象获取例子

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. Nginx LOG阶段里log模块

    L68 log_format 指令 syntax : name [escape =default|josn|none] string "...."; default : combi ...

  6. [LOJ3084][GXOI/GZOI2019]宝牌一大堆——DP

    题目链接: [GXOI/GZOI2019]宝牌一大堆 求最大值容易想到$DP$,但如果将$7$种和牌都考虑进来的话,$DP$状态不好设,我们将比较特殊的七小对和国士无双单独求,其他的进行$DP$. 观 ...

  7. jmeter笔记(8)--关联

    关联是jmeter中比较重要的一个点,在测试过程中有些数据是经常发生变化的,要获取这些数据,就需要使用关联,Jmeter可以通过“后置处理器”中的“正则表达式提取器”来处理关联.. 正则表达式提取器 ...

  8. Sublime Text 3 安装简记

    1.下载:( Sublime Text Version 3.1.1 Build 3176 ) https://www.sublimetext.com/3 2.安装Package Control: &q ...

  9. python之路day10-命名空间和作用域、函数嵌套,作用域链、闭包

    楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return mbigger = my_max(10,20)print ...

  10. Linux系统网络编程中TCP通讯socket--send导致进程被关闭

    https://blog.csdn.net/dsanmux/article/details/52083403 https://blog.csdn.net/u011425939/article/deta ...