题解上说的很清楚了,我照着写的,表示膜拜题解

然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <vector>
  6. using namespace std;
  7. typedef long long LL;
  8. const int N = 2e5+;
  9. int mat[N],n,mx,tmp,lz[N<<],a[N<<];
  10. LL c[N<<],sum,h[N];
  11. vector<int>v[N];
  12. void build(int rt,int l,int r){
  13. if(l==r){
  14. a[rt]=c[rt]=1ll*l;
  15. return;
  16. }
  17. int m=(l+r)>>;
  18. build(rt<<,l,m);
  19. build(rt<<|,m+,r);
  20. c[rt]=c[rt<<]+c[rt<<|];
  21. a[rt]=min(a[rt<<],a[rt<<|]);
  22. }
  23. void down(int rt,int l,int r){
  24. if(!lz[rt])return;
  25. int m=(l+r)>>;
  26. c[rt<<]=1ll*lz[rt]*(m-l+);
  27. c[rt<<|]=1ll*lz[rt]*(r-m);
  28. a[rt<<]=a[rt<<|]=a[rt];
  29. lz[rt<<]=lz[rt<<|]=lz[rt];
  30. lz[rt]=;
  31. }
  32. void modify(int rt,int l,int r,int x,int y){
  33. if(x<=l&&r<=y){
  34. c[rt]=1ll*tmp*(r-l+);
  35. a[rt]=lz[rt]=tmp;
  36. return;
  37. }
  38. down(rt,l,r);
  39. int m=(l+r)>>;
  40. if(x<=m)modify(rt<<,l,m,x,y);
  41. if(y>m)modify(rt<<|,m+,r,x,y);
  42. c[rt]=c[rt<<]+c[rt<<|];
  43. a[rt]=min(a[rt<<],a[rt<<|]);
  44. }
  45. int ask(int rt,int l,int r,int k){
  46. if(l==r)return l;
  47. int m=(l+r)>>;
  48. if(a[rt<<|]<k)ask(rt<<|,m+,r,k);
  49. else ask(rt<<,l,m,k);
  50. }
  51. int main(){
  52. scanf("%d",&n);
  53. for(int i=;i<=n;++i){
  54. int x;scanf("%d",&x);
  55. mat[x]=i;mx=max(mx,x);
  56. }
  57. for(int i=;i<=mx;++i){
  58. for(int j=i;j<=mx;j+=i)
  59. if(mat[j])
  60. v[i].push_back(mat[j]);
  61. }
  62. for(int i=;i<=mx;++i)
  63. if(v[i].size())sort(v[i].begin(),v[i].end());
  64. build(,,n);
  65. sum=1ll*n*(n+);
  66. for(int i=mx;i>=;--i){
  67. h[i]=sum-c[];
  68.  
  69. int k=v[i].size();
  70. if(k<)continue;
  71. tmp=n+;
  72. if(v[i][]+<=n){
  73. modify(,,n,v[i][]+,n);
  74. }
  75. tmp=v[i][k-];
  76. if(a[]<tmp){
  77. int pos=ask(,,n,tmp);
  78. pos=min(pos,v[i][]);
  79. if(pos>v[i][])
  80. modify(,,n,v[i][]+,pos);
  81. }
  82. tmp=v[i][k-];
  83. if(a[]<tmp){
  84. int pos=ask(,,n,tmp);
  85. pos=min(pos,v[i][]);
  86. if(pos>)
  87. modify(,,n,,pos);
  88. }
  89. }
  90. LL ans=;
  91. for(int i=;i<=mx;++i)
  92. ans+=1ll*i*(h[i]-h[i-]);
  93. printf("%I64d\n",ans);
  94. return ;
  95. }

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(数论+线段树)

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

  3. CodeForces 671C - Ultimate Weirdness of an Array

    题意: 给以一个定义, F(l, r) 的值表示序列 A[1:n]的子序列 A[1....(l-1),(r+1)...n] 之中 任意两个数的最大公约数的最大值. 求 Sum=∑i=1N∑j=1N(F ...

  4. Codeforces 1114F Please, another Queries on Array? 线段树

    Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...

  5. Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]

    Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...

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

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

  7. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  8. 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)}$ ...

  9. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

随机推荐

  1. 懒惰的JY--关于遍历

    先上题: [问题描述] 众所周知,JY的百度搜索算法已经练的炉火纯青,任何搜索题都能0.000ms出解. 不幸的是,JY遇到了一道百度搜索算法解决不了的题目,题目是这样的: 给定N个数A[1] A[2 ...

  2. SVM整理

    SVM整理 Last modified: 2015.9.2 1.算法总结 支持向量机是Cortes和Vapnik于1995年首先提出的,它在解决小样本,非线性及高维模式识别中表现出许多特有的优势,并能 ...

  3. Java第一阶段总结

    学习java已经一个多月的时间了,第一阶段总算完成了. 这中间遇到很多问题,通过问同学问学长,收获了很多,但也知道自己和其他同学相差很远.他们java第一阶段只用了不到一个月的时间,而我拖了很长时间, ...

  4. Gnome 插件介绍

    插件:Applications Menuhttps://extensions.gnome.org/extension/6/applications-menu/ TopIconshttps://exte ...

  5. ExtJS4.2学习(10)分组表格控件--GroupingGrid(转)

    鸣谢网址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-17/179.html ------------- ...

  6. [转载]jquery ajax/post/get 传参数给 mvc的action

    jquery ajax/post/get 传参数给 mvc的action 1.ActionResult Test1     2.View  Test1.aspx 3.ajax page 4.MetaO ...

  7. [转载]c# 多线程一个带多个参数的方法

    比如我要线程一个private void subPing(int pre,int end) 我在Thread t=之后应该如何写 用匿名委托吧!那么简单为什么要这样写!t = new Thread(d ...

  8. case class inheritance

    Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...

  9. 179. Largest Number

    题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...

  10. java编写二叉树以及前序遍历、中序遍历和后序遍历 .

    /** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...