题意:

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

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

思路:

英文题解

大致解释一下:

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

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

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

Code:

#include <bits/stdc++.h>
#define lson l , m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
const LL maxn = 200000 + 131;
const LL MaxN = 200000;
struct node {
LL Max, Min;
LL Sum, Flag;
}; node Node[maxn<<2];
LL H[maxn], A[maxn], Idx[maxn];
LL L1[maxn], L2[maxn], R1[maxn], R2[maxn]; ///SegmentTree
void PushUp(LL rt) {
Node[rt].Max = max(Node[rt<<1].Max, Node[rt<<1|1].Max);
Node[rt].Min = min(Node[rt<<1].Min, Node[rt<<1|1].Min);
Node[rt].Sum = Node[rt<<1].Sum + Node[rt<<1|1].Sum;
}
void PushDown(LL l, LL r, LL rt) {
LL m = (l + r) >> 1;
if(Node[rt].Flag)
{
Node[rt<<1].Flag = Node[rt<<1].Max = Node[rt<<1].Min = Node[rt].Flag;
Node[rt<<1|1].Flag = Node[rt<<1|1].Max = Node[rt<<1|1].Min = Node[rt].Flag;
Node[rt<<1].Sum = Node[rt].Flag * (m - l + 1);
Node[rt<<1|1].Sum = Node[rt].Flag * (r - m);
Node[rt].Flag = 0;
}
}
void Build(LL l, LL r, LL rt) {
Node[rt].Flag = 0;
if(l == r) {
Node[rt].Max = Node[rt].Min = Node[rt].Sum = l;
return ;
}
LL m = (l + r) >> 1;
Build(lson), Build(rson);
PushUp(rt);
}
void Update_section(LL L, LL R, LL val, LL l, LL r, LL rt) {
if(L > R) return ;
if(Node[rt].Min >= val) return ;
if(L <= l and r <= R and Node[rt].Max <= val) {
Node[rt].Flag = val;
Node[rt].Min = Node[rt].Max = val;
Node[rt].Sum = LL(r - l + 1) * LL(val);
return ;
}
PushDown(l, r, rt);
LL m = (l + r) >> 1;
if(L <= m) Update_section(L, R, val, lson);
if(R > m) Update_section(L, R, val, rson);
PushUp(rt);
}
/// Solve
int main() {
std::ios::sync_with_stdio(false);
LL n;
cin >> n;
for(LL i = 1; i <= n; ++i) {
cin >> A[i];
Idx[A[i]] = i;
}
/// Get l(1, b1) -> bk-1, (b1,b2) -> bk, (b2, n) -> n+1
for(LL i = 1; i <= MaxN; ++i)
{
for(LL j = i; j <= MaxN; j +=i)
if(Idx[j])
{
if(L1[i] == 0 or L1[i] > Idx[j]) L2[i] = L1[i], L1[i] = Idx[j];
else if(L2[i] == 0 or L2[i] > Idx[j]) L2[i] = Idx[j];
if(R1[i] < Idx[j]) R2[i] = R1[i], R1[i] = Idx[j];
else if(R2[i] < Idx[j]) R2[i] = Idx[j];
}
}
/// Build Tree
Build(1,n,1);
for(LL i = MaxN; i > 0; --i)
{
if(L1[i] != R1[i])
{
Update_section(1,L1[i],R2[i], 1, n, 1);
Update_section(L1[i]+1, L2[i], R1[i], 1, n, 1);
Update_section(L2[i]+1, n, n+1, 1, n, 1);
}
H[i] = LL(n*(n+1)) - Node[1].Sum;
//cout << H[i] << endl;
}
LL Ans = 0;
for(LL i = 1; i < MaxN; ++i)
Ans += i * (H[i+1]-H[i]);
cout << Ans <<endl;
return 0;
}

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. 时间插件datepicker(jQuery-UI,bootstrap)和jquery-steps的冲突解决。。。

    日期插件初始化:  $('.prelease_time').flatpickr(); let contentSteps = $("#content_form").steps({ h ...

  2. python正则匹配示例

    text="山东省临沂市兰山区 市委大院中区21号楼4单元 276002 奥特曼1号 18254998111" #匹配手机号 m=re.findall(r"1\d{10} ...

  3. Redis读取出错,JSON序列化的问题

    报错 org.springframework.web.util.NestedServletException: Request processing failed; nested exception ...

  4. Django基础(路由、视图、模板)

    目录导航 Django 路由控制 Django 视图层 Django 模版层 Django 路由控制 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用 ...

  5. 虚拟机中使用centos7搭建ftp服务器

    应用场景 本地windows作为客户端,虚拟机CentOS7作为服务器端,搭建FTP服务器,本地访问虚拟机实现文件的上传下载.如何在虚拟机安装centos7就不在赘述. 1.在centos7上安装vs ...

  6. python全栈开发中级班全程笔记(第二模块、第四章)(常用模块导入)

    python全栈开发笔记第二模块 第四章 :常用模块(第二部分)     一.os 模块的 详解 1.os.getcwd()    :得到当前工作目录,即当前python解释器所在目录路径 impor ...

  7. 从线性模型(linear model)衍生出的机器学习分类器(classifier)

    1. 线性模型简介 0x1:线性模型的现实意义 在一个理想的连续世界中,任何非线性的东西都可以被线性的东西来拟合(参考Taylor Expansion公式),所以理论上线性模型可以模拟物理世界中的绝大 ...

  8. python try exception finally记录

    try exception finally中,finally下的语句块始终会执行 测试finally代码 def test_try_exception(a, b): '''测试异常捕获语句''' re ...

  9. python双端队列-collection模块

    双端队列(double-ended queue,或者称deque)在需要按照元素增加的顺序来移除元素时非常有用.其中collection模块,包括deque类型. 使用实例:

  10. webpack配置less

    webpack4.0把webpack.config.js隐藏起来了,需要先暴露出来,在webpack修改配置